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=" "; }}
时间: 2024-12-28 22:45:53