Sqlite Insert语句未运行

时间:2013-01-12 19:49:41

标签: javascript sqlite

我有以下代码供我的sqlite存储来自相机的名称和图像。我从Eclipse控制台知道我得到了名称和图像路径。 console.log(addRecord)获取名称和图像路径,但我在事务行和queryDB函数上得到“无法执行未定义的SQL”消息,所以我认为我的插入语句出错了?此外,imageURI的数据类型文本是否正确?任何帮助赞赏。

   function onDeviceReady() {
    var db = window.openDatabase("database", "1.0", "Profiles", 5000);
    if(db) {
console.log('The device is ready');
db.transaction(populateDB, errorCB, successCB, insertRecord); // only do stuff if db exists
}
else{
console.log('There is a problem');
}   
   function populateDB(tx) {
   tx.executeSql('DROP TABLE IF EXISTS USERS');//get rid of this once working?
   tx.executeSql('CREATE TABLE IF NOT EXISTS USERS (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, username VARCHAR NOT NULL, imagePath TEXT)');
   console.log('The table USERS is created');
   }
  //Insert the details
function insertRecord(tx) {
       userName = document.getElementById('userName').value;
       imagePath;
       var addRecord = 'INSERT INTO USERS (username, imagePath) VALUES ("' + userName + '","' + imagePath + '")';
       console.log(addRecord);
       tx.executeSql(addRecord, [userName, imagePath], queryDB, errorCB);
}

 // Query the database
function queryDB(tx) {
       var getUsers = "SELECT * FROM USERS ORDER BY id ASC', [], querySuccess, errorCB";
       db.transaction(function (tx) {
       tx.executeSql(getUsers, [], querySuccess, queryFailure);
    }, errorCB, successCB);
}

// Query the success callback

function querySuccess(tx, results) {
        console.log("You are in the querysuccess function");
}

// Transaction error callback

function errorCB(err) {
        console.log("Error processing SQL: " + err.code);
}

function queryFailure(err) {
      console.log("Error processing SQL: " + err.code);
}
// Transaction success callback

function successCB() {
      console.log('The db and table are working');
 }

1 个答案:

答案 0 :(得分:1)

标识符queryFailure未定义。添加功能:

function queryFailure(tx, results) {
    console.log("You are in the queryFailure function");
}

一旦运行,您将看到调用queryFailure函数,因为查询中有错误。这样:

var getUsers = "SELECT * FROM USERS ORDER BY id ASC', [], querySuccess, errorCB";

应该是:

var getUsers = "SELECT * FROM USERS ORDER BY id ASC";