3

如何在 phonegap 存储数据库中创建两个或多个表?它适用于一个表,但如果您尝试创建另一个表,则会返回 Error processing SQL:1; (

  var db = window.openDatabase("Database", "1.0", "Order database", 1000000);
  document.addEventListener("deviceready", onDeviceReady, false);


function onDeviceReady() {
    db.transaction(populateDB, errorCB, successCB);
}




function populateDB(tx) {

    tx.executeSql('DROP TABLE IF EXISTS ORDER');
    tx.executeSql('CREATE TABLE IF NOT EXISTS ORDER (id INTEGER PRIMARY KEY, status)');
    tx.executeSql('INSERT INTO ORDER (id, status) VALUES (1, "new" )');
    tx.executeSql('INSERT INTO ORDER (id, status) VALUES (2, "done" )');
    tx.executeSql('INSERT INTO ORDER (id, status) VALUES (3, "new" )');
    tx.executeSql('INSERT INTO ORDER (id, status) VALUES (4, "undone" )');


    tx.executeSql('DROP TABLE IF EXISTS CLIENT');
    tx.executeSql('CREATE TABLE IF NOT EXISTS CLIENT (id INTEGER PRIMARY KEY, name, addr, mail, phone)');
    tx.executeSql('INSERT INTO CLIENT (id, name, addr, mail, phone) VALUES (1, "Kentavr", "Kiev", "Kentavr@i.ua", "044-454-34-34")');
    tx.executeSql('INSERT INTO CLIENT  (id, name, addr, mail, phone) VALUES (2, "ООО New Step ", "Kiev", "Newstep@i.ua", "044-433-222-24")');
    tx.executeSql('INSERT INTO CLIENT  (id, name, addr, mail, phone) VALUES (3, "NEXT", "Lvov", "next@i.ua", "033-1-3434-24")');
    tx.executeSql('INSERT INTO CLIENT  (id, name, addr, mail, phone) VALUES (4, "Briz", "Lvov", "brizxt@i.ua", "033-1-1111-24")'); 
    tx.executeSql('INSERT INTO CLIENT  (id, name, addr, mail, phone) VALUES (5, "Brand", "Lvov", "brhjjt@i.ua", "033-1-1001-24")'); 




}


function queryDB(tx) {
      tx.executeSql('SELECT * FROM CLIENT WHERE addr="Lvov" ', [], querySuccess, errorCB);
}
4

2 回答 2

2

您不能创建表ORDER,因为它是 SQL 命令的一部分 :) 重命名您的表,您的程序将正常工作。

于 2012-08-07T08:24:53.270 回答
1
Please try like this...

    var sql1 = 'CREATE TABLE IF NOT EXISTS ORDER (id INTEGER PRIMARY KEY, status)';
    tx.executeSql(sql1,[], function (tx, results) {
        var test =  new Array();
        test[0]='INSERT INTO ORDER (id, status) VALUES (1, "new" )';
        test[1]='INSERT INTO ORDER (id, status) VALUES (2, "done" )';
        test[2]='INSERT INTO ORDER (id, status) VALUES (3, "new" )';                                                                 
        for( i in test ) {   
            tx.executeSql(test[i]); 
        }
    });

var sql2 = 'CREATE TABLE IF NOT EXISTS CLIENT (id INTEGER PRIMARY KEY, name, addr, mail, phone)';
    tx.executeSql(sql2,[], function (tx, results) {
        var test2 =  new Array();
        test2[0]='INSERT INTO CLIENT (id, name, addr, mail, phone) VALUES (1, "Kentavr", "Kiev", "Kentavr@i.ua", "044-454-34-34")';
        test2[1]='INSERT INTO CLIENT (id, name, addr, mail, phone) VALUES (1, "Kentavr", "Kiev", "Kentavr@i.ua", "044-454-34-34")';
        test2[2]='INSERT INTO CLIENT (id, name, addr, mail, phone) VALUES (1, "Kentavr", "Kiev", "Kentavr@i.ua", "044-454-34-34")';                                                          
        for( i in test2 ) {   
            tx.executeSql(test2[i]);    
        }
    });
于 2012-08-06T13:04:58.523 回答