【问题标题】:Pre-populated sqlite database issue in cordova android app科尔多瓦 android 应用程序中预填充的 sqlite 数据库问题
【发布时间】:2013-09-16 16:07:30
【问题描述】:

我正在尝试使用 Cordova 创建 'Offline Dictionary Android App。我已经使用SQLite database browser 创建了 sqlite 数据库。数据库文件是test.db,它有一个表test,其中有两个字段'_id' 是INTEGER PRIMARY KEY 和'value' 是TEXT。还插入了一些记录。

我试过这些都没有运气:

  1. import/export to android sqlite database
  2. Can't access pre populated SQLite database using PhoneGap/Cordova in Android


我已经能够收集到以下代码。 我希望我的第三次尝试能够成功,所以我在这里寻求专家的帮助。

我做了什么

我将test.db sqlite 数据库文件放在我的android 项目的assets 目录中。我有 MainActivity.java 文件,其中包含以下代码:

MainActivity.java

package com.example.sqlite;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.os.Bundle;
import android.util.Log;

import org.apache.cordova.*;

public class MainActivity extends DroidGap {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.loadUrl("file:///android_asset/www/index.html");

        try{
            System.out.println("----------Copying Database -----------");
                copyDataBase("test.db");

        } catch (IOException ioe){
            System.out.println("----------Error Copying Database -----------");
            ioe.printStackTrace();

        }


    }

    private void copyDataBase(String dbname) throws IOException {
        // Open your local db as the input stream
        String pkg = this.getApplicationContext().getPackageName();
        InputStream myInput = this.getAssets().open(dbname);

        // Path to the just created empty db
        String outFileName = "/data/data/"+ pkg + "/databases/" + dbname;

        // Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);


        // transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }
        // Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();

    }

}

script.js 文件包含以下脚本:

script.js

(function($, undefined){

    document.addEventListener('deviceready', onDeviceReady, false);

    function onDeviceReady(){

        var db = window.openDatabase(
            'test.db',
            '1.0.0',
            'Test Database',
            64 * 1024
        );

        db.transaction(function(tx){

            tx.executeSql(
                'SELECT * FROM test', [], function(tx, result){

                    console.log("Query Success");
                    console.log('Total Rows :' + result.rows.length);

                },function(tx, error) {
                    console.log('An Error Occured (Qry) : ' + error.message);
                }
            )

        }, function(error){

            console.log('An Error Occured : ' + error.message);

        }, function(){
            console.log("Transaction Success");
        })

    }

})(jQuery);

我已经在emulatorphysical device 中启动了我的应用程序,并得到了相同的错误响应,即“没有这样的表:测试”。我找到了test.db insdie data/data/MY_PACKAGE/databases/。我使用 eclipse pulled 那个数据库文件并在 SQLite Database browser 中打开,并且有 test 表,其中包含我填充的一些记录。

我正在使用 cordova 2.6.0 并在 android 4.2 上测试我的应用程序。我的设备没有植根(如果不影响则忽略)。

可视化场景的屏幕截图:

为什么即使有表格,我也会收到“没有这样的表格”错误?

请帮忙。

【问题讨论】:

  • 等待响应直到 1 小时。还没有有用的手。

标签: android database sqlite cordova


【解决方案1】:

只需检查您是否已正确完成数据库中的连接,然后创建一个表,插入一些数据,然后从 Cordova 和数据库浏览器中选择您在表中放置的内容。

也许你不需要放.db,先试试这段代码

var db = window.openDatabase(
        'test',
        '1.0.0',
        'Test Database',
        64 * 1024
    );

db.transaction(function populateDB(tx) {

    tx.executeSql('DROP TABLE IF EXISTS DEMO');
    tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)');
    tx.executeSql('INSERT INTO DEMO (id, data) VALUES (1, "First row")');
    tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');

},function errorCB(tx, err) {

    alert("Error processing SQL: "+err);

},function successCB() {

    alert("success!");

});

【讨论】:

  • 我正在尝试使用预先填充的数据库,其中包含 databasetable 和一些记录。我不能使用cordova 来创建table 并插入记录,因为我有100000 多条记录。我尝试了 'test' 而不是 'test.db' 但得到了同样的错误。
  • 我的意思是尝试预先填充以检查您是否正在使用所需的数据库。仅使用填充数据来检查它。
  • 无论有没有.db 扩展,我都会遇到同样的错误,而error.message 也是undefined,这使得调试变得困难,请问您有什么想法吗
  • 尝试我在答案中添加的代码,也尝试删除数据库的参数大小。
  • check if you are using the database you want 真的很有帮助。我将databases 更改为app_database,似乎错误no such table 不再存在。感谢您的有用评论。是的,现在不需要test.db test 正在做这项工作。现在是 12:30 AM,睡觉,晚安。
【解决方案2】:

我完全测试了您的代码。我解决了用js代码写完整路径:

var db = window.openDatabase(
        '/data/data/com.example.myapp/databases/test.db',
        '1.0.0',
        'Test Database',
        64 * 1024
    );

..在重新运行项目之前清理项目。

希望对您有所帮助。

问候

【讨论】:

    猜你喜欢
    • 2015-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-02
    • 2020-12-05
    相关资源
    最近更新 更多