sqlite - 如何使用 react-native-sqlite-storage 将多行插入到表中?
问题描述
在 react-native-sqlite-storage 的测试中(参见https://github.com/andpor/react-native-sqlite-storage/blob/master/test/index.ios.promise.js#L141-L149)我发现:
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);');
有没有办法通过传递一个数组来用一个语句来做到这一点?
解决方案
假设 myDb 是您的连接变量:
const myDb=SQLite.openDatabase({name:"yourDb", createFromLocation:"yourDB.db"}, ()=>console.log('DB opened'), (err)=>alert('error : ' + err));
如果您想在“Employees”表中插入三行:
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');
}
}
);
});
祝你好运!
推荐阅读
- r - 将带有 k 格式数字的因子列转换为数字而不丢失任何数据
- javascript - 防止引导表单使用“Enter”提交
- elasticsearch - 使用非规范化或连接字段对弹性搜索数据库建模
- java - Android/Java,如何在某些字段中处理带有换行符和逗号的csv文件?
- python - 在 Django 和 MySQL 中为每个模型/表设置排序规则和字符集
- python - AttributeError:“NoneType”对象没有属性“next”
- mediawiki - 如何在不登录的情况下使用域用户名?
- firefox - Firefox中不同的Referer Header设置导致网站损坏
- quill - Quill中图像标签的数据属性
- php - 为什么我的 Wordpress 网站上的所有链接都显示主页?