0

我正在使用 Phonegap 在我的项目中实现 Sqlite db。我知道问题与我的插入语句有关,因为当我将数据库成功指向我的查询并最终指向我的 querysuccess 函数时,我收到查询成功并且添加的行数为 0 的警报。我尝试过并尝试过这是各种不同的方法,但无济于事。(如果有明显的错误,那是因为我在自学。)

   function insertRecord(dbtx, userName, imagePath){
db = window.openDatabase("database", "1.0", "Profiles", 5000);
userName=document.getElementById('userName').value;
imagePath;
var insertRec="INSERT INTO USERS (username, imagePath) values ("+userName+", "+imagePath+")";
    db.transaction(function(dbtx){dbtx.executeSql(insertRec, [], queryDB, insFail);
    });
    }
function insFail(){
    alert('The insert is still failing');
}
   ....
   function successCB() {
    db = window.openDatabase("Database", "1.0", "Profiles", 5000);
    db.transaction(insertRecord, errorCB);// IF I PUT THE QUERYDB HERE INSTEAD OF INSERTRECORD IT WORKS BUT OBVIOUSLY THE RETURN IS ALWAYS 0
    console.log('The db and table are working');

这是我要插入的按钮

   <input id="saveProfile" type="button" value="Save" onClick="insertRecord(dbtx, userName, imagePath);">
4

2 回答 2

2

通过使用参数化查询,您可以避免此类错误和可能的sql 注入

var insertRec="INSERT INTO USERS (username, imagePath) values (?, ?)";

db.transaction(function(dbtx){
    dbtx.executeSql(insertRec, [userName, imagePath], queryDB, insFail);
});
于 2013-01-13T17:03:05.470 回答
1

您需要像这样添加撇号:

var insertRec="INSERT INTO USERS (username, imagePath) 
               values ('"+userName+"', '"+imagePath+"')";

并防止多个空白插入简单地将您的插入语句包装在:

if(userName != '' && imagePath != ''){
    // Your code
}
于 2013-01-13T16:51:30.973 回答