在Sqlite中插入查询的问题?(变量插入)

时间:2014-04-02 07:34:34

标签: javascript sqlite cordova android-sqlite sql-insert

如何在Sqlite中插入变量值,我无法找到我的代码的问题,如果我使用如下的insert语句它正在工作并插入值,

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]);


 });

 });

1 个答案:

答案 0 :(得分: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]);