如何在 Android 设备中存储数据库?
我创建了一个包含表的 sqlite 应用程序。在该应用程序中,我从表中检索了数据。问题是我如何使用我的数据库在 Android 设备中加载该应用程序。我已经尝试过,但没有找到答案。在我的应用程序中,我使用了以下代码。
try{
String destpath = "/data/data/"+getPackageName()+"/database/mmts1.db";
File f = new File(destpath);
if(!f.exists())
{
copyDb(getResources().getAssets().open("mmts1.db"),new FileOutputStream(destpath));
}
}
catch(FileNotFoundException e)
{
Log.e("FileNotFoundException",e.getMessage());
}
catch(IOException e)
{
Log.e("IoException",e.getMessage());
}
private void copyDb(InputStream open, FileOutputStream fileOutputStream) throws IOException{
// TODO Auto-generated method stub
byte [] buffer = new byte[1024];
int length;
while((length=open.read(buffer))>0)
{
fileOutputStream.write(buffer,0,length);
}
open.close();
fileOutputStream.close();
}
i have created an sqlite application that contain tables. in that application i have retrieved data from tables. the problem is how can i load that application in android device with my database. i have tried but i didn't found answer. in my application i have used the below code.
try{
String destpath = "/data/data/"+getPackageName()+"/database/mmts1.db";
File f = new File(destpath);
if(!f.exists())
{
copyDb(getResources().getAssets().open("mmts1.db"),new FileOutputStream(destpath));
}
}
catch(FileNotFoundException e)
{
Log.e("FileNotFoundException",e.getMessage());
}
catch(IOException e)
{
Log.e("IoException",e.getMessage());
}
private void copyDb(InputStream open, FileOutputStream fileOutputStream) throws IOException{
// TODO Auto-generated method stub
byte [] buffer = new byte[1024];
int length;
while((length=open.read(buffer))>0)
{
fileOutputStream.write(buffer,0,length);
}
open.close();
fileOutputStream.close();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
SQLiteAssetHelper
将数据库与您的应用程序打包。Use
SQLiteAssetHelper
to package a database with your application.