繁体   English   中英

将外部数据库导入应用程序并在android中使用它

[英]import external db into app and use it in android

我是android开发新手。 现在我试图使用sqlite db。 我使用sqlite manager创建了一个数据库sqlite文件。 我通过/ data / data / packagename / dbname将它导入到项目中,它在模拟器中工作正常,但如果我在设备中发布应用程序崩溃,我不知道发生了什么以及为什么会发生。 有什么建议吗?

您不能以这种方式使用外部数据库。 当您将其导入项目时,并不意味着它在之后的所有设备中都可用(假设您使用了DDMS)。 这意味着DB仅可用于该特定仿真器。 请按照以下链接了解如何将外部数据库添加到您的应用程序..

http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

private void StoreDatabase() {
     File DbFile=new File("data/data/packagename/DBName.sqlite");
     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("DBName.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中使用自己的数据库,这将非常有用。

http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

在本教程中,您必须将数据库放在项目的assets文件夹中,数据库将自动转移到您设备的数据库文件夹中。

有关帮助程序库,请参阅https://github.com/jgilfelt/android-sqlite-asset-helper以解决此问题。

(我没有亲自使用过这个图书馆,但我昨天在搜索其他内容时遇到过它。但它似乎可以做你需要的东西)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM