在phonegap存储中创建2个或更多表

时间:2012-08-06 12:48:58

标签: sqlite cordova

如何在phonegap存储数据库中创建两个或多个表?它适用于一个表,但是如果你尝试创建另一个表,则返回Error处理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);
}

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分: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]);    
        }
    });