【问题标题】:SQLite - Problems opening a db that existsSQLite - 打开存在的数据库时出现问题
【发布时间】:2016-03-01 16:10:47
【问题描述】:

我正在使用我的 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)

【问题讨论】:

    标签: android sqlite cordova


    【解决方案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;
               }
         }
    }
    

    在最后但不是最后,在res 中的xml 下的config.xml 中输入YourClass,并使用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
                }]
            );
         }
    }
    

    【讨论】:

    • 您能详细说明一下吗?我对 CordovaPlugin 类了解不多。感谢您的建议,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2014-06-13
    • 1970-01-01
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    • 2011-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多