首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用react-native-sqlite-storage将多行插入到表中?

如何使用react-native-sqlite-storage将多行插入到表中?
EN

Stack Overflow用户
提问于 2019-08-29 21:12:04
回答 1查看 2.9K关注 0票数 3

在react-native-sqlite-storage的测试中(参见https://github.com/andpor/react-native-sqlite-storage/blob/master/test/index.ios.promise.js#L141-L149),我发现:

代码语言:javascript
复制
tx.executeSql('INSERT INTO Employees (name, office, department) VALUES ("Sylvester Stallone", 2,  4);');
tx.executeSql('INSERT INTO Employees (name, office, department) VALUES ("Elvis Presley", 2, 4);');
tx.executeSql('INSERT INTO Employees (name, office, department) VALUES ("Leslie Nelson", 3,  4);');
tx.executeSql('INSERT INTO Employees (name, office, department) VALUES ("Fidel Castro", 3, 3);');
tx.executeSql('INSERT INTO Employees (name, office, department) VALUES ("Bill Clinton", 1, 3);');
tx.executeSql('INSERT INTO Employees (name, office, department) VALUES ("Margaret Thatcher", 1, 3);');
tx.executeSql('INSERT INTO Employees (name, office, department) VALUES ("Donald Trump", 1, 3);');
tx.executeSql('INSERT INTO Employees (name, office, department) VALUES ("Dr DRE", 2, 2);');
tx.executeSql('INSERT INTO Employees (name, office, department) VALUES ("Samantha Fox", 2, 1);');

有没有一种方法可以通过传递数组来用一条语句做到这一点呢?

EN

回答 1

Stack Overflow用户

发布于 2019-10-21 08:41:18

假设myDb是您的连接变量:

代码语言:javascript
复制
const myDb=SQLite.openDatabase({name:"yourDb", createFromLocation:"yourDB.db"}, ()=>console.log('DB opened'), (err)=>alert('error : ' + err));

如果要在"Employees“表中插入三行:

代码语言:javascript
复制
myDb.transaction((tx)=>{
  tx.executeSql(        
    'INSERT INTO Employees (name,office,departement) VALUES (?,?,?),(?,?,?),(?,?,?)',
    ['Sylvester Stallone',2,4,'Elvis Presley',2,4,'Leslie Nelson',3,4],
    (tx, results) => {               
      if (results.rowsAffected > 0 ) {
        console.log('Insert success');              
      } else {
        console.log('Insert failed');
      }
    }
  );
});

祝好运!

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57711001

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档