SQLite - 打开存在的数据库的问题

时间:2016-03-01 16:10:47

标签: android sqlite cordova

我正在使用我的cordova android项目运行cordova s​​qlite-ext插件(​​https://github.com/litehelpers/cordova-sqlite-ext),并试图打开一个预先存在的sqlite .db。我一直看到下面的错误,说它正在打开我的数据库,但没有找到表格。

我决定将数据库完全重命名为" wibblewobble"看看它是否会产生未知错误,因为wibblewobble不存在,但我仍然得到相同的结果。

任何使用sqlite插件的人都可以解释为什么这会出现在我的logcat中并且我的内容没有出现?为什么我收到假的错误,说明数据库正在打开时显然不是?

 03-01 10:59:22.210 850-911/cafr.b.appfinder W/PluginManager: THREAD WARNING: exec() call to SQLitePlugin.open blocked the main thread for 71ms. Plugin should use CordovaInterface.getThreadPool().
 03-01 10:59:22.510 850-850/cafr.b.appfinder I/chromium: [INFO:CONSOLE(106)] "new transaction is waiting for open operation", source: file:///android_asset/www/plugins/cordova-sqlite-ext/www/SQLitePlugin.js (106)
 03-01 10:59:23.780 850-850/cafr.b.appfinder I/chromium: [INFO:CONSOLE(80)] "DB opened: wibblewobble.db", source: file:///android_asset/www/plugins/cordova-sqlite-ext/www/SQLitePlugin.js (80)
 03-01 10:59:24.240 850-911/cafr.b.appfinder W/PluginManager: THREAD WARNING: exec() call to SQLitePlugin.backgroundExecuteSqlBatch blocked the main thread for 64ms. Plugin should use CordovaInterface.getThreadPool().
 03-01 10:59:25.880 850-903/cafr.b.appfinder E/SQLiteLog: (1) no such table: MainDatabase
 03-01 10:59:25.890 850-903/cafr.b.appfinder W/System.err: android.database.sqlite.SQLiteException: no such table: MainDatabase (code 1): , while compiling: SELECT DISTINCT Category FROM `MainDatabase`
 03-01 10:59:25.890 850-903/cafr.b.appfinder W/System.err:     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)

1 个答案:

答案 0 :(得分:1)

我也有这个问题并通过使用CordovaPlugin类解决了它,我的建议是使用插件通过扩展CordovaPlugin类来创建你自己:

class YourClass extends CordovaPlugin{
     public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
           //Your Code here...
           switch(action){
                 case "yourAction":
                         //Your Logic to work with SQLite....
                 break;
           }
     }
}

on Last但不是lst,在YourClass中的config.xmlxml输入res,并使用YourClass对此类进行ajax调用将名称命名为动作。

从您的网页调用该类的方法。

function doTask(){
var success = function(message) {
                    document.getElementById('testResult').innerHTML = message;
                };
                var error = function(message) {
                    document.getElementById('testResult').innerHTML = message;
                };

YourVariable.createEvent(database_Name,query_string, success, error);
}

创建.js文件并输入以下代码:

var YourVariable = {
    createEvent: function(title, location, notes, startDate, endDate, successCallback, errorCallback) {
        cordova.exec(
            successCallback, // success callback function
            errorCallback, // error callback function
            'YourClass', // mapped to our native Java class called "YourClass"
            'yourAction', // with this action name
            [{                  // and this array of custom arguments to create our entry
                "title": title,
                "description": notes,
                "eventLocation": location,
                "startTimeMillis": startDate,
                "endTimeMillis": endDate
            }]
        );
     }
}