我是android开发的新手。现在我正在尝试使用 sqlite db。我使用 sqlite manager 创建了一个数据库 sqlite 文件。我已经尝试了以下代码,它在模拟器中运行良好,但是如果我在设备中发布应用程序崩溃,即,它会显示一条警报消息应用程序 CheckMobileForDatabase 已意外停止,我的 sdk 版本是 2.2(8)
private void StoreDatabase() {
File DbFile=new File("data/data/com.sqldemo/databases/idua1");
if(DbFile.exists())
{
System.out.println("file already exist ,No need to Create");
}
else
{
try
{
DbFile.createNewFile();
System.out.println("File Created successfully");
InputStream is = this.getAssets().open("idua1.sqlite");
FileOutputStream fos = new FileOutputStream(DbFile);
byte[] buffer = new byte[1024];
int length=0;
while ((length = is.read(buffer))>0)
{
fos.write(buffer, 0, length);
}
System.out.println("File succesfully placed on sdcard");
//Close the streams
fos.flush();
fos.close();
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}