1

How can I insert variable value in Sqlite, I am unable to find the issue with my code, If I use insert statement as follows it is working and values are inserted,

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

If I try to insert as follows the values are not inserted,

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

does not work for the query like,

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

I am using TEXT as datatype for all columns, the input value size is in between 1-100. What is the maximum size for TEXT data type? Any one can suggest how to insert variables in insert query ?

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 Answer 1

3

add id attribute

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]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.