【问题标题】:Can't access pre populated SQLite database using PhoneGap/Cordova in Android无法在 Android 中使用 PhoneGap/Cordova 访问预填充的 SQLite 数据库
【发布时间】:2013-08-02 10:13:54
【问题描述】:

我们正在尝试使用 PhoneGap/Cordova 创建一个移动应用程序。我们需要使用预填充的 SQLite 数据库来发布这个应用程序。我们可以使用以下代码复制数据库。但是,当我们尝试从应用程序中复制的数据库中访问表时,我们会收到“没有这样的表”错误。任何人都可以帮助我们找出原因吗?

我们使用以下代码将数据库复制到 data/data/package_name/databases 文件夹。

   try
    {
        File dbFile = getDatabasePath("Bee_dict.db");
        if(!dbFile.exists()){
            this.copy("Bee_dict.db",dbFile.getAbsolutePath());
        }
     }
     catch (Exception e)
     {
     e.printStackTrace();
     }


//And our copy function:

   void copy(String file, String folder) throws IOException
    {
     File CheckDirectory;
     CheckDirectory = new File(folder);


     String parentPath = CheckDirectory.getParent();

     File filedir = new File(parentPath);
     if (!filedir.exists()) {
         if (!filedir.mkdirs()) {
             return;
         }
     }

        InputStream in = this.getApplicationContext().getAssets().open(file);
        File newfile = new File(folder);
        OutputStream out = new FileOutputStream(newfile);


        byte[] buf = new byte[1024];
        int len; while ((len = in.read(buf)) > 0) out.write(buf, 0, len);
        in.close(); out.close();
    }

index.html 下面的代码用于打开和访问数据库

 function onDeviceReady()
 {
  db = window.openDatabase("Bee_dict", "1.0", "Bee_dict", 20000);
  tx.executeSql('SELECT * FROM data WHERE word_id = ?', [1], querySuccess_definition, errorCB);
 }

Cordova 版本 - 2.9

谢谢。

【问题讨论】:

    标签: android sqlite cordova android-sqlite


    【解决方案1】:

    首先,可以尝试下一个数据库文件名:

    0000000000000001.db
    

    对于加载文件:

    File dbFile = getDatabasePath(".0000000000000001db");
    

    DB文件需要在下一个路径:

    yourProyect/assets/0000000000000001.db
    

    我推荐使用“SQLitePlugin”:

    SQLitePlugin GitHub

    在我使用的“onDeviceReady()”函数中:

    if(!dbCreated){
        db = window.sqlitePlugin.openDatabase("0000000000000001", "1.0", "My Database", -1);
    }
    

    【讨论】:

    【解决方案2】:

    我遇到了同样的问题,但上面的答案对我没有帮助,所以我写下我的经验,它可能会有所帮助。

    我通常使用名为:me.rahul.plugins.sqlDB at https://github.com/an-rahulpandey/cordova-plugin-dbcopy 的 Cordova 插件

    这是一个插件,专注于将文件从 Cordova www 文件夹复制到 Android/iphone 上的正确文件夹。

    你必须先安装插件,使用:

    $ cordova plugin add https://github.com/an-rahulpandey/cordova-plugin-dbcopy.git
    

    然后在设备就绪事件上:

    function onDeviceReady() {
        console.log(">device is ready");
        window.plugins.sqlDB.copy("mydb.sqlite", copySuccess, copyError);
    }
    

    【讨论】:

      【解决方案3】:

      要访问预填充的数据库,您应该首先将数据库文件复制到 www 目录中。

      安装此插件DB-Copy plugin并将数据库从www目录复制到设备中,使用复制插件,然后使用Sqlite-storage plugin访问数据库。

      PS。 www 目录中的数据库副本是需要的,因为每个操作系统都有不同的位置来存储数据库...

      【讨论】:

        猜你喜欢
        • 2016-04-12
        • 2012-10-19
        • 2012-02-22
        • 2015-11-24
        • 1970-01-01
        • 2016-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多