phoneGap Android - Populate database once “Error processing SQL:1”

喜你入骨 提交于 2019-12-06 14:36:30

问题


I am trying to avoid populate the database if the database exists but when I try to cancel this line in my code I get "Error processing SQL:1"

tx.executeSql('DROP TABLE IF EXISTS DEMO');

this is my whole populate function

 function populateDB(tx) {
        tx.executeSql('DROP TABLE IF EXISTS DEMO');
        tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, word TEXT NOT NULL');
        tx.executeSql('INSERT INTO DEMO (id, word) VALUES ("1", "Apple")');
        tx.executeSql('INSERT INTO DEMO (id, word) VALUES ("2", "Orange")');

    }

回答1:


Your function has set of issues which are corrected below. It should work now.

    function populateDB(tx) {
        isTableExists(tx, "DEMO", function(status) {
            if (!status) {
                alert("table not exist, creating one");
                tx.executeSql('CREATE TABLE DEMO (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, kword TEXT NOT NULL, eword TEXT NOT NULL, pronoun TEXT NOT NULL, level INTEGER NOT NULL)');
                tx.executeSql('INSERT INTO DEMO (kword, eword, pronoun, level) VALUES ("Apple", "", "", 1)');
                tx.executeSql('INSERT INTO DEMO (kword, eword, pronoun, level) VALUES ("Orange", "", "", 2 )');
            } else {
                alert("table exist, dropping for test");
                tx.executeSql('DROP TABLE DEMO');
            }
        });
    }

    function isTableExists(tx, tableName, callback) {
        tx.executeSql('SELECT * FROM DEMO', [], function(tx, resultSet) {
            if (resultSet.rows.length <= 0) {
                callback(false);
            } else {
                callback(true);
            }
        }, function(err) {
            callback(false);
        });
    }



回答2:


Why dont you use

CREATE TABLE IF NOT EXISTS DEMO  (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, kword TEXT NOT NULL, eword TEXT NOT NULL, pronoun TEXT NOT NULL, level INTEGER NOT NULL)


来源:https://stackoverflow.com/questions/10942971/phonegap-android-populate-database-once-error-processing-sql1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!