将现有的SQLite数据库和阅读数据库、SQLite

2023-09-07 03:16:52 作者:农夫叁湶丿有点甜

在哪里放置现有sqllite数据库中的Andr​​oid文件夹结构?它是绘制文件夹或文件夹的布局?

Where to place existing sqllite database in android folder structure? Is it drawable folder or layout folder?

我没有找到这方面的任何解决方案。

I am not finding any solution for this.

任何帮助将AP preciated。

Any help will be appreciated.

推荐答案

你应该把它放在资产文件夹中。这种方式可以确保它会连接到您的apk。这是你如何从资产文件夹中的数据库文件复制到您的工作目录:

you should put it in the assets folder. This way you can make sure it will be attached to your apk. this is how you can copy the database file from the assets folder to your working directory:

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(outFileName);

//transfer bytes from the inputfile to the 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();

}

现在从目录中读取数据库:

now to read the database from the directory:

 public void openDataBase() throws SQLException{

//Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

}