gpt4 book ai didi

java - android中导入数据库的问题

转载 作者:行者123 更新时间:2023-11-30 04:11:59 24 4
gpt4 key购买 nike

我是 android 开发的新手。现在我正在尝试使用 sqlite 数据库。我使用 sqlite 管理器创建了一个数据库 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();
}
}
}

最佳答案

这可能是由于您的 Android 设备中存储了以前版本的数据库,请尝试从您的设备中删除以前的数据库或更改代码中的数据库版本,以便调用 onupgrade

关于java - android中导入数据库的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10722143/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com