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")');
}
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);
});
}
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)