使用React Native在Sqlite中插入多条记录
我尝试使用SQLite在React Native中插入多行 代码如下:使用React Native在Sqlite中插入多条记录,sqlite,react-native,expo,Sqlite,React Native,Expo,我尝试使用SQLite在React Native中插入多行 代码如下: rows = responseJson.rows; for (i = 0; i < rows.length; i++) { row=rows[i]; query = `insert into ComuniUserAccountSync values ( ${row.IDComuniUserAccountSync}, ${row
rows = responseJson.rows;
for (i = 0; i < rows.length; i++) {
row=rows[i];
query = `insert into ComuniUserAccountSync values (
${row.IDComuniUserAccountSync},
${row.IdAzienda},
${row.IdComune},
${row.IdUserAccount},
'${row.DescrizioneComune}',
'${row.DateLastUpdateMaster}'
)`;
db.transaction(
tx => {
tx.executeSql(query, [], (a,b) =>
console.log("!OK!!", JSON.stringify(b)), (a, b) =>
console.log("!ERROR!!", a, b)
)
}
);
}
!!好啊{“rowsAffected”:0,行:{“{U数组”:[{“IDComuniUserAccountSync”:72},{“IDComuniUserAccountSync”:72},{“IDComuniUserAccountSync”:72},{“IDComuniUserAccountSync”:72}
有什么帮助吗??
最大值插入类别(arrCateData){
让keys=Object.keys(arrCateData[0])
让arrValues=[]
var len=arrCateData.length;
for(设i=0;i{
this.initDB().then((db)=>{
数据库事务((tx)=>{
tx.executeSql(“插入到类别(“+keys+”)值“+字符串(arrValues))。然后(([tx,results])=>{
决心(结果);
});
})。然后((结果)=>{
这个数据库(db);
}).catch((错误)=>{
控制台日志(err);
});
}).catch((错误)=>{
控制台日志(err);
});
});
}
此代码可能会帮助您我只是将数组传递到函数中,它将根据您的要求插入数据jusr更改数据您是否尝试在循环中使用async/Wait来确保所有查询都已执行?我对sqlite有一些问题,我创建了insert-fetch-delete函数,但出于某些原因,它没有执行I don’我不工作,你能帮我吗?
db.transaction(
tx => {
tx.executeSql("select IDComuniUserAccountSync from ComuniUserAccountSync", [], (a,b) =>
console.log("!OK!", JSON.stringify(b)), (a,b) =>
console.log("!ERROR!!", JSON.stringify(b))
);
}
);
insertCategories(arrCateData){
let keys = Object.keys(arrCateData[0])
let arrValues = []
var len = arrCateData.length;
for (let i = 0; i < len; i++) {
arrCateData[i].image = `"${arrCateData[i].image}"`;
arrCateData[i].thumbnail = `"${arrCateData[i].thumbnail}"`;
arrCateData[i].name = `"${arrCateData[i].name}"`;
arrCateData[i].path = `"${arrCateData[i].path}"`;
arrValues.push("(" + Object.values(arrCateData[i]) +")");
}
// console.log(arrValues)
return new Promise((resolve) => {
this.initDB().then((db) => {
db.transaction((tx) => {
tx.executeSql("INSERT INTO category ("+ keys + ") VALUES " + String(arrValues)).then(([tx, results]) => {
resolve(results);
});
}).then((result) => {
this.closeDatabase(db);
}).catch((err) => {
console.log(err);
});
}).catch((err) => {
console.log(err);
});
});
}