将现有的SQLite数据库和阅读 [英] placing existing sqlite database and reading

查看:146
本文介绍了将现有的SQLite数据库和阅读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在哪里放置现有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);

}

这篇关于将现有的SQLite数据库和阅读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆