javascript - How to insert multiple rows with a parameter? -
i trying insert multiple rows in sqlite using ionic framework. single row insert in working fine.
even if run
insert categories (category_id, category_name,category_type) values (1,"test",1),(2,"test again", 2);
this working fine. when try create dynamic string gives me error "could not prepare statement (1 near "?": syntax error)".
.success((function (result) { var query = "insert categories (category_id, category_name,category_type) values ?"; var data = []; result.foreach(function (category) { data.push([category.id, category.category_name, category.category_type]); }); $cordovasqlite.execute(db, query,[data]).then(function (res) { console.log("inserted"); }, function (err) { console.dir(err); });
add multiple parameters insert, in test query (the first mentioned), pass arguments 1 dimensional array:
.success((function (result) { var query = "insert categories (category_id, category_name,category_type) values "; var data = []; var rowargs = []; result.foreach(function (category) { rowargs.push("(?, ?, ?)"); data.push(category.id); data.push(category.category_name); data.push(category.category_type); }); query += rowargs.join(", "); $cordovasqlite.execute(db, query,[data]).then(function (res) { console.log("inserted"); }, function (err) { console.dir(err); });
this code produce query like:
insert categories (category_id, category_name,category_type) values (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?);
and data array of size 12, every 3 entries in array 1 row of data inserted.
those numbers example , depend on size of result
.
Comments
Post a Comment