3

我有一个 Phonegap (2.1.0) 应用程序,它 onDeviceready 创建一个数据库并用信息填充一个表。

在 Chrome 上本地运行(使用 Ripple 模拟器)可以工作。正在根据需要创建和填充表。

在我的 Android 设备上安装 build .apk 后,我的 Eclipse logcat 显示:

    sqlite returned: error code = 14, msg = cannot open file at line 27712 of [8609a15dfa], db=/data/data/<project>/databases/webview.db

    sqlite returned: error code = 14, msg = os_unix.c: open() at line 27712 - "" errno=2 path=/CachedGeoposition.db, db=/data/data/<project>/databases/webview.db

根据这里的这篇文章,我相信- 可以忽略。

但是 - 我在 logcat 中也注意到了这个错误:

    sqlite returned: error code = 1, msg = no such table: latest_events, db=/data/data/<project>/databases/webview.db

我还 - 通过 adb shell - 确认没有创建数据库:这里:/data/data/com.application/databases。或在这里:/data/data/com.application/app_databases

所以 - 我的代码:

 if (!window.openDatabase) {
        doMessage('Databases are not supported on this device. Sorry','error');
        return;
    }else{
        consoleLog('all good for storage');
        var db;
        var shortName = 'MyDB';
        var version = '1.0';
        var displayName = 'MyDB';
        var maxSize = 102400;

        function errorHandler(transaction, error) {consoleLog('Error: ' + error.message + ' code: ' + error.code);}

        function nullHandler(){};
        db = window.openDatabase(shortName, version, displayName,maxSize);


            consoleLog('starting table creation');
            db.transaction(function(tx){
                tx.executeSql( 'CREATE TABLE IF NOT EXISTS latest_events (id integer PRIMARY KEY AUTOINCREMENT,EventID integer,EventLocation text,EventName text,EventDateFrom varchar,EventTime timestamp,EventPresentedBy varchar,EventVenue varchar,EventScript text,RequireRSVP varchar)',[],nullHandler,errorHandler);
                db.transaction(function(tx){
                    tx.executeSql('SELECT count(id) as RowCount FROM device_info ', [],
                        function(tx, result) {
                            if (result != null && result.rows != null) {
                                for (var i = 0; i < result.rows.length; i++) {
                                    var row = result.rows.item(i);
                                    consoleLog('rowcount: '+row.RowCount);
                                    if(row.RowCount==0){
                                        tx.executeSql('INSERT INTO device_info (device_name, device_platform, device_uuid, device_os_ver, date_last_used) VALUES (?,?,?,?,?)',[device.name, device.platform, device.uuid, device.version, window.bowman_config.siteDate],nullHandler,errorHandler);
                                        //doMessage('device info row added','notice');
                                    }
                                }
                            }
                        },errorHandler);
                },errorHandler,successCallBack('2'));
                //doMessage('device info row added','notice');
            },errorHandler,successCallBack('1'));
}

为了增加我的麻烦 - 在我的 logcat 上,我确实看到了 console.log 输出“一切都适合存储”,以及“开始创建表”消息。

My errorHandler functions are not returning anything and my successCallBack functions are triggered...but no DB created.

Thanks for the help.

4

3 回答 3

0

When you pass in successCallBack("2") and successCallBack("1") then you are actually invoking them directly so you may be getting false positives on whether or not success has actually been called. You should provide two separate success call backs or just in-line some functions that call console.log("1") for instance.

于 2012-11-13T19:18:44.020 回答
0

Today this issue cost me 3 hours. What I tried:

  • Rewriting the copy database code.
  • Deleting the app from the emulator / device
  • Wiping emulator(s)
  • Cleaning eclipse
  • Changing file permissions
  • Validate a working SQLite database file

I solved the problem by copying the code from a shared Dropbox account to another location and refactoring the code in the Android Manifest and java files with another package name.

The application runs beautifully now, i.e. nothing wrong with the code, but somewhere it's muxed up by Dropbox.

于 2012-12-27T16:18:50.463 回答
0

I broke the nested functions up into single functions and 'chained' them based on their success or fail. It's actually been a lot simpler than I thought. RTFM it seemed. Thanks for all the help.

simplified:

var db = window.openDatabase("TheApp", "1.0", "The App", 50000000);
db.transaction(queryDB, errorCB, successCB);

// Query the database   //
function queryDB(tx) {
    //tx.executeSql('SELECT * FROM table", [], querySuccess, errorCB);
}

function querySuccess(tx, results) {
    //do more functions here
}

function errorCB(err) {
    console.log("Error processing SQL: "+err.code);
}
于 2013-02-10T15:36:40.050 回答