我正在尝试使用react-native-sqlite-storage
将数据保留在设备上。
但是我需要在代码中使用大量事务,并且当我尝试通过SQLite.openConnection
返回实例并打开事务不起作用时。而且,如果我尝试从我的数据库实例执行executeSql,那么它会完美地工作。
db
,如果我的实例来自SQLite.openConnection()
// THIS WORKS
db.executeSql(queryCreateTable)
.catch(erro => console.log(`erro executesql ${erro}`));
db.executeSql('INSERT INTO PEOPLE VALUES ("Mateus Pereira", 20)')
.catch(erro => console.log(`erro executesql ${erro}`));
db.executeSql('SELECT * FROM PEOPLE').then(([results]) => {
const { rows } = results;
console.log(rows);
for (let i = 0; i < rows.length; i += 1) {
const row = rows.item(i);
console.log(`Name: ${row.nome}, Idade: ${row.idade}`);
}
});
// THIS DOESNT WORK
db.transaction().then((tx) => {
tx.executeSql(queryCreateTable)
.catch(erro => console.log(`erro executesql ${erro}`));
tx.executeSql('INSERT INTO PEOPLE VALUES ("Mateus Pereira", 20)')
.catch(erro => console.log(`erro executesql ${erro}`));
tx.executeSql('SELECT * FROM PEOPLE').then(([results]) => {
const { rows } = results;
console.log(rows);
for (let i = 0; i < rows.length; i += 1) {
const row = rows.item(i);
console.log(`Name: ${row.nome}, Idade: ${row.idade}`);
}
});
}).catch(error => console.log(error));
{
"db": {
"openargs": {
"name": "Test2.db",
"dblocation": "nosync"
},
"dbname": "Test2.db"
},
"txlock": true,
"readOnly": false,
"executes": []
}
答案 0 :(得分:2)
以下是使用交易的方法:
db.transaction(tx => {
tx.executeSql(`SELECT ...`)
.then([tx2, results]) => {
}
.catch((error) => {
console.log(error);
});
});