Websql不创建表

时间:2015-05-04 05:08:43

标签: sqlite web-sql

 db = window.openDatabase("javascript.db", "1.0", "My app", 2 * 1024 * 1024);
  db.transaction(function(tx){
      tx.executeSql("CREATE TABLE topic (topic_id TEXT PRIMARY KEY NOT NULL, topic_name TEXT UNIQUE NOT NULL, 'topic description' TEXT)");
      tx.executeSql("INSERT INTO 'topic' VALUES('IV','Interview','This quiz contains questions asked during most interviews.')");
      tx.executeSql("INSERT INTO 'topic' VALUES('AN','Angularjs','Questions relating to Angularjs')");
      tx.executeSql("INSERT INTO 'topic' VALUES('JQ','Jquery','Questions relating to Jquery')");
      tx.executeSql("INSERT INTO 'topic' VALUES('XT','Extra','Extra questions.')");
      tx.executeSql("INSERT INTO 'topic' VALUES('XP','Expert','This is the fourth stage.')");
      tx.executeSql("INSERT INTO 'topic' VALUES('AD','Advance','This is the third stage.')");
      tx.executeSql("INSERT INTO 'topic' VALUES('IM','Intermediate','This is the second stage.')");
      tx.executeSql("INSERT INTO 'topic' VALUES('BG','Beginner','You should start here.')");
      tx.executeSql("CREATE TABLE question (question_id INTEGER PRIMARY KEY, question_code TEXT UNIQUE, question TEXT NOT NULL, option1 TEXT NOT NULL, option2 TEXT NOT NULL, option3 TEXT, option4 TEXT, hint TEXT)");
      tx.executeSql("INSERT INTO 'question' VALUES(1,'BG0101','quetion quetion quetion quetion quetion quetion quetion1','option1','option2','option3','1000','hint1')");
      tx.executeSql("INSERT INTO 'question' VALUES(2,'BG0102','quetion quetion quetion quetion quetion quetion quetion2','option1','option2','option3','0100','hint2')");
      tx.executeSql("INSERT INTO 'question' VALUES(3,'BG0103','quetion quetion quetion quetion quetion quetion quetion3','option1','option2','option3','0010','hint3')");
      tx.executeSql("INSERT INTO 'question' VALUES(4,'BG0104','quetion quetion quetion quetion quetion quetion quetion4','option1','option2','option3','0001','hint4')");
      tx.executeSql("INSERT INTO 'question' VALUES(5,'BG0105','quetion quetion quetion quetion quetion quetion quetion5','option1','option2','option3','1100','hint5')"); 
      tx.executeSql("INSERT INTO 'question' VALUES(6,'BG0106','quetion quetion quetion quetion quetion quetion quetion6','option1','option2','option3','0110','hint6')");
      tx.executeSql("INSERT INTO 'question' VALUES(7,'BG0107','quetion quetion quetion quetion quetion quetion quetion7','option1','option2','option3','0011','hint7')");
      tx.executeSql("INSERT INTO 'question' VALUES(8,'BG0108','quetion quetion quetion quetion quetion quetion quetion8','option1','option2','option3','1001','hint8')");
      tx.executeSql("INSERT INTO 'question' VALUES(9,'BG0109','quetion quetion quetion quetion quetion quetion quetion9','option1','option2','option3','1010','hint9')");
      tx.executeSql("INSERT INTO 'question' VALUES(10,'BG0110','quetion quetion quetion quetion quetion quetion quetion10','option1','option2','option3','0101','hint10')");
      tx.executeSql("INSERT INTO 'question' VALUES(11,'BG0111','quetion quetion quetion quetion quetion quetion quetion11','option1','option2','option3','1110','hint11')");
      tx.executeSql("INSERT INTO 'question' VALUES(12,'BG0112','quetion quetion quetion quetion quetion quetion quetion12','option1','option2','option3','0111','hint12')");
      tx.executeSql("INSERT INTO 'question' VALUES(13,'BG0113','quetion quetion quetion quetion quetion quetion quetion13','option1','option2','option3','1011','hint13')"); 
      tx.executeSql("INSERT INTO 'question' VALUES(14,'BG0114','quetion quetion quetion quetion quetion quetion quetion14','option1','option2','option3','1101','hint14')");
      tx.executeSql("INSERT INTO 'question' VALUES(15,'BG0115','quetion quetion quetion quetion quetion quetion quetion15','option1','option2','option3','1111','hint15')"); 
      tx.executeSql("INSERT INTO 'question' VALUES(16,'BG0116','quetion quetion quetion quetion quetion quetion quetion16','option1','option2','option3','0011','hint16')");
      tx.executeSql("INSERT INTO 'question' VALUES(17,'BG0117','quetion quetion quetion quetion quetion quetion quetion17','option1','option2','option3','1001','hint17')"); 
      tx.executeSql("INSERT INTO 'question' VALUES(18,'BG0118','quetion quetion quetion quetion quetion quetion quetion18','option1','option2','option3','1010','hint18')");
      tx.executeSql("INSERT INTO 'question' VALUES(19,'BG0119','quetion quetion quetion quetion quetion quetion quetion19','option1','option2','option3','0101','hint19')"); 
      tx.executeSql("INSERT INTO 'question' VALUES(20,'BG0120','quetion quetion quetion quetion quetion quetion quetion20','option1','option2','option3','1000','hint20')");
      tx.executeSql("CREATE TABLE answer (question_id INTEGER REFERENCES question (question_id) ON DELETE CASCADE ON UPDATE CASCADE,question_code TEXT REFERENCES question (question_code) ON DELETE CASCADE ON UPDATE CASCADE, answer TEXT, answer_code INTEGER)"); 
      tx.executeSql("INSERT INTO 'answer' VALUES(1,'BG0101','remove if not needed',1000,'explian1')");
      tx.executeSql("INSERT INTO 'answer' VALUES(2,'BG0102','remove if not needed',100,'explian2')"); 
      tx.executeSql("INSERT INTO 'answer' VALUES(3,'BG0103','remove if not needed',10,'explian3')");
      tx.executeSql("INSERT INTO 'answer' VALUES(4,'BG0104','remove if not needed',1,'explian4')"); 
      tx.executeSql("INSERT INTO 'answer' VALUES(5,'BG0105','remove if not needed',1100,'explian5')");
      tx.executeSql("INSERT INTO 'answer' VALUES(6,'BG0106','remove if not needed',110,'explian6')"); 
      tx.executeSql("INSERT INTO 'answer' VALUES(7,'BG0107','remove if not needed',11,'explian7')");
      tx.executeSql("INSERT INTO 'answer' VALUES(8,'BG0108','remove if not needed',1001,'explian8')");
      tx.executeSql("INSERT INTO 'answer' VALUES(9,'BG0109','remove if not needed',1010,'explian9')");
      tx.executeSql("INSERT INTO 'answer' VALUES(10,'BG0110','remove if not needed',101,'explian10')"); 
      tx.executeSql("INSERT INTO 'answer' VALUES(11,'BG0111','remove if not needed',1110,'explian11')");
      tx.executeSql("INSERT INTO 'answer' VALUES(12,'BG0112','remove if not needed',111,'explian12')");
      tx.executeSql("INSERT INTO 'answer' VALUES(13,'BG0113','remove if not needed',1011,'explian13')");
      tx.executeSql("INSERT INTO 'answer' VALUES(14,'BG0114','remove if not needed',1101,'explian14')"); 
      tx.executeSql("INSERT INTO 'answer' VALUES(15,'BG0115','remove if not needed',1111,'explian15')");
      tx.executeSql("INSERT INTO 'answer' VALUES(16,'BG0116','remove if not needed',11,'explian16')");
      tx.executeSql("INSERT INTO 'answer' VALUES(17,'BG0117','remove if not needed',1001,'explian17')");
      tx.executeSql("INSERT INTO 'answer' VALUES(18,'BG0118','remove if not needed',1010,'explian18')"); 
      tx.executeSql("INSERT INTO 'answer' VALUES(19,'BG0119','remove if not needed',101,'explian19')");
      tx.executeSql("INSERT INTO 'answer' VALUES(20,'BG0120','remove if not needed',1000,'explian20')");
      tx.executeSql("CREATE TABLE image (question_id INTEGER REFERENCES question (question_id) ON DELETE CASCADE ON UPDATE CASCADE, img_src TEXT)");
      tx.executeSql("CREATE TABLE user (topic_id TEXT REFERENCES topic (topic_id) ON DELETE CASCADE ON UPDATE CASCADE, attempt INTEGER, score INTEGER)"); 
      tx.executeSql("CREATE UNIQUE INDEX question_code ON question (question_code)"); 



  });

上面的代码创建了一个名为javascriot.db的数据库,但它没有创建表和行可能是什么问题? 我是websql的新手,不知道如何调试?有人可以更正我的代码并告诉我如何调试它吗?

1 个答案:

答案 0 :(得分:1)

WebSQL调用是异步的,因此假设您的SQL语法正确,问题是您尝试在创建表之前插入数据。您需要做的是在第一次调用中创建表,并通过成功回调插入数据。如果出现问题,错误回调将为您提供信息。

一般语法(在WebSQL spec中描述):

executeSql(sqlStatement, optional arguments, optional callback, optional errorCallback);

在你的情况下:

tx.executeSql("CREATE TABLE topic (topic_id TEXT PRIMARY KEY NOT NULL, topic_name TEXT UNIQUE NOT NULL, 'topic description' TEXT)", null, 
    function (transaction, sqlResultSet) {     
        tx.executeSql("INSERT INTO 'topic' VALUES('IV','Interview','This quiz contains questions asked during most interviews.')", null, null, 
            function(transaction, error) {
                console.log('error on insert: ' + error.message);
            });
       // more inserts into 'topic' table ...
    }, function (transaction, error) {
        console.log('error on table create: ' + error.message);
    });

// more create table/insert