Where should I put a sqlite file in source android app
本问题已经有最佳答案,请猛点这里访问。
我是一个新的Android开发人员。
我有一个现有的SQLite数据库。
我的问题是:我应该把它放在我的源代码中?
而且:我想在安装时将其复制到设备上。
请帮忙
谢谢
你做了什么。
您必须将外部sqlite数据库文件放在assets文件夹中并复制到SD卡中并进行访问。
例如,
只创建一个名为的类
DBConnect.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | package com.appgiudeextra.Database; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; import com.appguideextra.Items.MasterItem; public class DBConnect extends SQLiteOpenHelper { public int GetCursor; // ****************** Declare all the global variable // ****************************// private Context myContext; public String DB_PATH ="data/data/com.xyz/databases/"; // path // of // your // datbase public static String DB_NAME ="xyz.sqlite";// your database name static String ASSETS_DB_FOLDER ="db"; private SQLiteDatabase db; public DBConnect(Context context, String db_name) { super(context, db_name, null, 2); if (db != null && db.isOpen()) close(); this.myContext = context; DB_NAME = db_name; try { createDataBase(); openDataBase(); } catch (IOException e) { // System.out.println("Exception in creation of database :"+ // e.getMessage()); e.printStackTrace(); } } public void createDataBase() throws IOException { boolean dbExist = checkDataBase(); if (dbExist) { // System.out.println("Database Exist"); } else { this.getReadableDatabase(); try { copyDatabase(); } catch (IOException e) { throw new Error("Error copying database"); } } } private void copyDatabase() throws IOException { InputStream input = myContext.getAssets().open(DB_NAME); String outputFileName = DB_PATH + DB_NAME; OutputStream output = new FileOutputStream(outputFileName); byte[] buffer = new byte[1024]; int length; while ((length = input.read(buffer)) > 0) { output.write(buffer, 0, length); } // Close the streams output.flush(); output.close(); input.close(); // System.out.println(DB_NAME +"Database Copied !"); } @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } public void openDataBase() throws SQLException { // Open the database String myPath = DB_PATH + DB_NAME; db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); } public boolean isOpen() { if (db != null) return db.isOpen(); return false; } @Override public synchronized void close() { if (db != null) db.close(); super.close(); } private boolean checkDataBase() { SQLiteDatabase checkDB = null; try { String myPath = DB_PATH + DB_NAME; // System.out.println("My Pathe is:-" + myPath); // System.out.println("Open"); checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); // System.out.println("checkDB value:" + checkDB); // System.out.println("My Pathe is:-" + myPath); } catch (Exception e) { // database does't exist yet. } if (checkDB != null) { // System.out.println("Closed"); checkDB.close(); // System.out.println("My db is:-" + checkDB.isOpen()); } return checkDB != null ? true : false; } public Cursor execCursorQuery(String sql) { Cursor cursor = null; try { cursor = db.rawQuery(sql, null); GetCursor = cursor.getCount(); Log.i("Inside execCursorQuery try", sql); } catch (Exception e) { Log.i("Inside execCursorQuery exception", e.getMessage()); } return cursor; } public void execNonQuery(String sql) { try { db.execSQL(sql); // Log.d("SQL", sql); } catch (Exception e) { // Log.e("Err", e.getMessage()); } finally { // closeDb(); } } } |
并将此类访问到您的Activity。
1 | My question is: where should I put it in my source? |
您可以将数据库放入应用程序的assets文件夹中,然后将其复制到SD卡中并使用它。
尝试下面的代码将数据库从assets文件夹复制到SD卡。
DataBaseHelper.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | public class DataBaseHelper extends SQLiteOpenHelper { private Context mycontext; //private String DB_PATH = mycontext.getApplicationContext().getPackageName()+"/databases/"; private static String DB_NAME ="(datbasename).sqlite";//the extension may be .sqlite or .db public SQLiteDatabase myDataBase; /*private String DB_PATH ="/data/data/" + mycontext.getApplicationContext().getPackageName() +"/databases/";*/ public DataBaseHelper(Context context) throws IOException { super(context,DB_NAME,null,1); this.mycontext=context; boolean dbexist = checkdatabase(); if (dbexist) { //System.out.println("Database exists"); opendatabase(); } else { System.out.println("Database doesn't exist"); createdatabase(); } } public void createdatabase() throws IOException { boolean dbexist = checkdatabase(); if(dbexist) { //System.out.println(" Database exists."); } else { this.getReadableDatabase(); try { copydatabase(); } catch(IOException e) { throw new Error("Error copying database"); } } } private boolean checkdatabase() { //SQLiteDatabase checkdb = null; boolean checkdb = false; try { String myPath = DB_PATH + DB_NAME; File dbfile = new File(myPath); //checkdb = SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READWRITE); checkdb = dbfile.exists(); } catch(SQLiteException e) { System.out.println("Database doesn't exist"); } return checkdb; } private void copydatabase() throws IOException { //Open your local db as the input stream InputStream myinput = mycontext.getAssets().open(DB_NAME); // Path to the just created empty db String outfilename = DB_PATH + DB_NAME; //Open the empty db as the output stream OutputStream myoutput = new FileOutputStream("/data/data/(packagename)/databases /(datbasename).sqlite"); // transfer byte to inputfile to outputfile byte[] buffer = new byte[1024]; int length; while ((length = myinput.read(buffer))>0) { myoutput.write(buffer,0,length); } //Close the streams myoutput.flush(); myoutput.close(); myinput.close(); } public void opendatabase() throws SQLException { //Open the database String mypath = DB_PATH + DB_NAME; myDataBase = SQLiteDatabase.openDatabase(mypath, null, SQLiteDatabase.OPEN_READWRITE); } public synchronized void close() { if(myDataBase != null) { myDataBase.close(); } super.close(); } } |
和
1 | I want copy it to device upon installation. |
在您的启动器活动中创建
1 | DataBaseHelper dbhelper=new DataBaseHelper(MyActivity.this); |
将database.sqlite文件放在项目目录的asset文件夹中,然后使用它进行检索
链接。
你可以把你的数据库放在assets文件夹中,然后将这个数据库复制到sdcard并从sdcard中检索数据库:
复制数据库:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public static void copyDataBase(Context mActivity) throws IOException { InputStream myInput = new FileInputStream(new File("/data/data/" + mActivity.getPackageName() +"/databases/" +"aman36.sqlite")); File files = new File("/sdcard/files/"); files.mkdirs(); String outFileName ="/sdcard/files/aman36.sqlite"; OutputStream myOutput = new FileOutputStream(outFileName); byte[] buffer = new byte[1024]; int bufferLength; while ((bufferLength = myInput.read(buffer)) > 0) { myOutput.write(buffer, 0, bufferLength); } myOutput.flush(); myOutput.close(); myInput.close(); } |
并且您可以访问您的路径来检索数据库 - > sdcard-> files-> aman36.sqlite