2014-04-02 70 views
1

我怎樣才能SQLite中插入變量的值,我無法找到問題,我的代碼,如果我使用INSERT語句如下這是工作和值插入,問題與SQLite中插入查詢?(可變插入)

tx.executeSql('INSERT INTO Locationlog (Coordinates, Location, Category, Address) VALUES ("manju", "kishor", "ramesh", "ram")'); 

如果我嘗試插入如下值沒有被插入,

tx.executeSql('INSERT INTO Locationlog (Coordinates, Location, Category, Address) VALUES (?,?,?,?)',[Latlongs,Location,Category,Address]); 

不用於查詢像工作,

tx.executeSql('INSERT INTO Locationlog (Coordinates, Location, Category, Address) VALUES ('+Latlongs+', '+Location+', '+Category+', '+Address+')'); 

我對所有列使用TEXT作爲數據類型,輸入值大小在1-100之間。 TEXT數據類型的最大大小是多少?任何人都可以建議如何在插入查詢中插入變量?

var Latlongs = $("#defendLatlongs").val(); 
    var Location = $("#defendLocation").val(); 
    var Category = $("#defendcategory").val(); 
    var Address = $("#defendAddress").val(); 

    var db = window.sqlitePlugin.openDatabase({name: "MYDB"}); 
    db.transaction(function (tx) { 
    tx.executeSql('CREATE TABLE IF NOT EXISTS Locationlog (Coordinates TEXT, Location TEXT, Category TEXT, Address TEXT)'); 
    }); 
    db.transaction(function (tx){ 
    tx.executeSql('INSERT INTO Locationlog (Coordinates, Location, Category, Address) VALUES (?,?,?,?)',[Latlongs,Location,Category,Address]); 


}); 

}); 

回答

3

附加id屬性

var db = window.sqlitePlugin.openDatabase({name: "MYDB"}); 
db.transaction(function (tx) { 
tx.executeSql('CREATE TABLE IF NOT EXISTS Locationlog (id INTEGER PRIMARY KEY AUTOINCREMENT,Coordinates TEXT, Location TEXT, Category TEXT, Address TEXT)'); 
}); 
db.transaction(function (tx){ 
tx.executeSql('INSERT INTO Locationlog (Coordinates, Location, Category, Address) VALUES (?,?,?,?)', [Latlongs,Location,Category,Address]);