问题
I am working with node js application and database is sqlite3 (2.15.8). I am using sqlite3 module . In this I have to insert rows in order.
I have table tests in which rows should be inserted as the same order in which they coming in loop.
My code is as follows:
To create Table:
GLOBAL.db1 = new sqlite3.Database(APPDATA+'/xyz/mydb.db');
var check;
db1.serialize(function() {
db1.run("CREATE TABLE IF NOT EXISTS tests (id INTEGER NOT NULL PRIMARY KEY , parent_name varchar(255), test_name varchar(255), test_alias varchar(255), sequences varchar(55), duration varchar(55), prog_id int(11), org_id int(11), test_createdby int(11), test_created varchar(20), test_modified varchar(11), test_status int(11))");
});
To Insert into Tables
try{
db1.run("INSERT INTO programs (`prog_name`, `prog_exercises`, `prog_orgid`, `prog_createdby`, `prog_created`, `prog_modified`, `prog_status`) VALUES (?,?,?,?,?,?,?)",program_name, JSON.stringify(step2PostedData), org_id, createdby, timestamp, timestamp, 0,function(err){
// as it is single record insertion so inserting good obviously.
if(err){
throw err;
}else{
for (i in step2PostedData.testsInfo) {
var c = 0;
for(j in step2PostedData.testsInfo[i]){
var obj = Object.keys(step2PostedData.testsInfo[i])[c];
console.log(obj); // here I am getting correct order of test names in console
db1.prepare("INSERT INTO `tests` ( `parent_name`,`test_name`, `test_alias`, `sequences`, `duration`, `prog_id`, `org_id`, `test_createdby`, `test_created`, `test_modified`, `test_status`) VALUES(?,?,?,?,?,?,?,?,?,?,?)").run('', obj, step2PostedData.testsInfo[i][j].alias, step2PostedData.testsInfo[i][j].sequences, step2PostedData.testsInfo[i][j].duration, this.lastID, org_id, createdby, timestamp, timestamp, 0);
// Insertion is going in loop but order of insertion of rows is not same as per required
c++;
}
}
}
});
}catch(ex){
throw ex;
}
But, when I am getting result in table then order of rows inserted in tests table are not same as the same order in which I am sending into loop. Can anyone please suggest me what should be changed there?
Thank you!
回答1:
There is no order in a relation
There is no order in a relation (i.e. in what people usually incorrectly call a "table") because relation is a set, not a list or array.
If people didn't incorrectly call relations "tables" then no one would expect an order from them. Unfortunately they do which leads to countless misconceptions that are so common that I wrote an article:
- What if I told you there are no tables in relational databases?
In short - don't expect any order from any set (this includes relations). If you want an order, use sort in SQL. If you want a specific order, add an integer value that you can sort to get the order that you need.
(Or don't use relational databases - there are other types of databases that support sorted structures like arrays. Relational databases don't support arrays - at least they shouldn't - and there is no implicit order in any relation.)
来源:https://stackoverflow.com/questions/41805789/sqlite3-is-not-inserting-multiple-rows-in-order