sqlite - sqlite3 - insert - javascript object as values - Stack Overflow

What is the easiest solution to use a javascript object as values for a sqlite3 insert? The following c

What is the easiest solution to use a javascript object as values for a sqlite3 insert? The following code does not work.

const values = {
  name: 'John',
  age: 34,
  language: 'english'
};

db.run('INSERT INTO tablename VALUES (?)', values, (err) => {
  if (err) {
    console.log(err);
  } else {
    console.log('success');
  }
});

What is the easiest solution to use a javascript object as values for a sqlite3 insert? The following code does not work.

const values = {
  name: 'John',
  age: 34,
  language: 'english'
};

db.run('INSERT INTO tablename VALUES (?)', values, (err) => {
  if (err) {
    console.log(err);
  } else {
    console.log('success');
  }
});
Share Improve this question asked Oct 29, 2017 at 10:28 tomoletomole 1,0063 gold badges15 silver badges37 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

First of all you need to write the SQL correctly. To insert into the columns name, age, language, you need to write the SQL like this:

INSERT INTO tablename (name, age, language) VALUES (?, ?, ?)

And pass the values of the 3 columns as parameters.

db.run('INSERT INTO tablename (name, age, language) VALUES (?, ?, ?)', [values['name'], values['age'], values['language']]), (err) => { ... });

Or if the property names in the JavaScript object correspond directly to the column names, then you can generate the correct SQL string dynamically to have more flexibility:

const cols = Object.keys(values).join(", ");
const placeholders = Object.keys(values).fill('?').join(", ");
db.run('INSERT INTO tablename (' + cols + ') VALUES (' + placeholders + ')', Object.values(values)), (err) => { ... });

Try ?,?,? and Obj.values()
code.

const values = {
  name: 'John',
  age: 34,
  language: 'english'
};
paramString = "?";
for (var i = 0; i < Object.keys(values).length -1 ; i ++) paramString += ",?";
// db.run('INSERT INTO tablename VALUES ('+paramString + ')', Object.values(values));
 console.log('INSERT INTO tablename VALUES ('+paramString + ')',Object.values(values));

In case you're ing from React Native environment, inserting objects to SQLite works perfectly fine on iOS, and you even don't have to restructure the object.

However, for SQLite on Android behaves differently. Trying to insert an objects lags (at least my Android simulator) it so much, and there was no single response from the SQL transaction. In addition to that, I had to pass into the parameters values from the destructured object, because doing so in the SQL query, like in the accepted answer, didn't work for me.

It's a really strange behaviour, but I've also noticed the app has started working way faster on the Android simulator after I've removed inserting the object to the SQLite.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744272586a4566173.html

相关推荐

  • sqlite - sqlite3 - insert - javascript object as values - Stack Overflow

    What is the easiest solution to use a javascript object as values for a sqlite3 insert? The following c

    8天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信