在Android 中调用sqlite数据库
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Android 中调用sqlite数据库相关的知识,希望对你有一定的参考价值。
1在android studio 工程中gradle文件夹右击新建assets文件夹。将建好的sqlite数据库导入其中。
2在主activity中判断app是否是第一次启动调用如下方法:
/**检查APP是否为第一次启动*/
private int CheckFirstActivate(){
/*设定数据库转移状态*/
int SetSQLiteDatabase_state = 0;
/*检查APP是否为第一次启动&转移数据库*/
SharedPreferences sharedPreferences = getSharedPreferences("Activate_State", Context.MODE_PRIVATE);
Boolean User_first_activate_state = sharedPreferences.getBoolean("FIRST",true);
if(User_first_activate_state){
sharedPreferences.edit().putBoolean("FIRST",false).apply();
AppDatabase appDatabase = new AppDatabase();
SetSQLiteDatabase_state = appDatabase.SetSQLiteDatabase(getApplicationContext());
return SetSQLiteDatabase_state;
}else{
return SetSQLiteDatabase_state;
}
}
----------------------
public class AppDatabase {
public int SetSQLiteDatabase(Context context){
String DB_PATH = "/data/data/com.example.administrator.search/databases/";
String DB_NAME = "testdb.db";
try{
/*检查路径是否存在,不存在就构成路径*/
File file = new File(DB_PATH);
if(!file.exists()){
file.mkdir();
}
/*以asset文件夹下的db文件作为输入流*/
InputStream inputStream = context.getAssets().open(DB_NAME);
/*生成程序根目录下的数据库放置路径*/
String outFileName = DB_PATH + DB_NAME;
/*产生输出流*/
OutputStream outputStream = new FileOutputStream(outFileName);
/*设置byte并进行转换*/
byte[] buffer = new byte[8192];
int lenhth;
while((lenhth = inputStream.read(buffer))>0){
outputStream.write(buffer,0,lenhth);
}
/*关闭IO流*/
outputStream.flush();
outputStream.close();
inputStream.close();
}catch (IOException e){
e.printStackTrace();
return 0;
}
return 1;
}
}
----------------
3写一个自定义工具类,用来调用数据库利用sql语句找到要获取的数据
public class GetTreeObject {
public GetTreeObject() {
}
//静态类用于调用数据库并返回TreeKePu表对象。
public static TreeKePu treeKePu(String s) {
String sql_statement = s;
String DB_PATH = "/data/data/com.example.administrator.search/databases/";
String DB_NAME = "testdb.db";
String DB_URL = DB_PATH + DB_NAME;
SQLiteDatabase sqLiteDatabase = SQLiteDatabase.openOrCreateDatabase(DB_URL, null);
Cursor cursor = sqLiteDatabase.rawQuery(sql_statement, null);
TreeKePu treeKePu = new TreeKePu();
if (cursor.moveToFirst()) {
try {
treeKePu.setTid(cursor.getInt(cursor.getColumnIndex("Tid")));
treeKePu.setTstyle(cursor.getString(cursor.getColumnIndex("Tstyle")));
treeKePu.setTtype(cursor.getString(cursor.getColumnIndex("Ttype")));
treeKePu.setTname(cursor.getString(cursor.getColumnIndex("Tname")));
treeKePu.setTMoral(cursor.getString(cursor.getColumnIndex("TMoral")));
treeKePu.setThabit(cursor.getString(cursor.getColumnIndex("Thabit")));
treeKePu.setTlooktype(cursor.getString(cursor.getColumnIndex("Tlooktype")));
treeKePu.setTdiscribe(cursor.getString(cursor.getColumnIndex("Tdiscribe")));
treeKePu.setTalias(cursor.getString(cursor.getColumnIndex("Talias")));
} catch (Exception e) {
e.printStackTrace();
}
}
cursor.close();
sqLiteDatabase.close();
return treeKePu;
}
------
public class TreeKePu {
public int getTid(){
return Tid;
}
public String getTtype(){return Ttype;}
public String getTname() {
return Tname;
}
public String getTalias() {
return Talias;
}
public String getTstyle() {
return Tstyle;
}
public String getTMoral() {
return TMoral;
}
public String getThabit() {
return Thabit;
}
public String getTlooktype() {
return Tlooktype;
}
public String getTdiscribe() {
return Tdiscribe;
}
public void setTid(int tid) {
Tid = tid;
}
public void setTdiscribe(String tdiscribe) {
Tdiscribe = tdiscribe;
}
public void setTlooktype(String tlooktype) {
Tlooktype = tlooktype;
}
public void setThabit(String thabit) {
Thabit = thabit;
}
public void setTMoral(String TMoral) {
this.TMoral = TMoral;
}
public void setTstyle(String tstyle) {
Tstyle = tstyle;
}
public void setTalias(String talias) {
Talias = talias;
}
public void setTname(String tname) {
Tname = tname;
}
public void setTtype(String ttype) {
Ttype = ttype;
}
private int Tid;
private String Ttype;
private String Tname;
private String Talias;
private String Tstyle;
private String TMoral;
private String Thabit;
private String Tlooktype;
private String Tdiscribe;
{
Ttype=" ";
Talias=" ";
TMoral=" ";
}
}
以上是关于在Android 中调用sqlite数据库的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 android 中的 SQLite 扩展类在数据库中插入值?
当sqlite android片段中的数据更改或删除时如何刷新recyclerview?
如何在片段内的 recylerview 列表中显示 SQLite 数据库数据?
java.lang.NullPointerException: Attempt to invoke virtual method ‘int android.database.sqlite异常(代码片段