WebSQL数据库创建,然后删除自己

时间:2013-11-04 19:03:36

标签: javascript

尝试创建webSQL数据库。已经排了很长一段时间了,并尝试了很多不同的东西,没有得到任何地方。把它归结为一个硬编码功能。通过观看Chrome中的开发者工具,它似乎是最初创建数据库,并立即删除它。

任何人都知道我哪里出错了?对Javascript来说很新,所以它可能很小。

感谢。

<script>

    function writeToDatabase(){
    var my_array = new Array();
    my_array[0] = "23 November 2013";
    my_array[1] = "3:00 AM";
    my_array[2] = "Go to the doctor for yearly checkup";
    my_array[3] = "Doctor visit";
    var db = openDatabase('events_db', '1.0', 'DB for storing event details', 2 * 1024 * 1024);
    db.transaction(function (tx) {
     tx.executeSql('CREATE TABLE IF NOT EXISTS EVENTS (id integer primary key autoincrement, date, time, description, title)');
     tx.executeSql('INSERT INTO EVENTS (date, time, description, title) VALUES (?, ?, ?, ?)', my_array);
    });
    window.alert("done");
    db.transaction(function (tx) {

    tx.executeSql('SELECT * FROM EVENTS', [], function (tx, results) {
     var len = results.rows.length, i;
     msg = "<p>Found rows: " + len + "</p>";

     for (i = 0; i < len; i++){
       window.alert(results.rows.item(i).description);
      }
    }, null);
    });

    }

</script>

1 个答案:

答案 0 :(得分:1)

我认为你需要执行CREATE - INSERT - SELECT操作作为每个操作的回调(在db.transaction中):

tx.executeSql('CREATE TABLE IF NOT EXISTS EVENTS (id integer primary key autoincrement, date, time, description, title)', [], function(tx) {
  tx.executeSql('INSERT INTO EVENTS (date, time, description, title) VALUES (?, ?, ?, ?)', my_array, function(tx) {
    tx.executeSql('SELECT * FROM EVENTS', [], function (tx, results) {
      ...
    });
  });
});

虽然这看起来很糟糕,但这是WebSQL的工作方式。