gpt4 book ai didi

javascript - 尝试打开事务时使用react-native-sqlite-storage停止

转载 作者:行者123 更新时间:2023-12-01 01:55:15 25 4
gpt4 key购买 nike

我正在尝试使用 react-native-sqlite-storage 将数据保存在设备上。

但是我需要在代码中使用大量事务,并且当我尝试获取 SQLite.openConnection 返回的实例并打开事务时,该事务不起作用。如果我尝试从我的数据库实例执行Sql,它会完美地工作。

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": []
}

最佳答案

以下是如何使用事务:

db.transaction(tx => {
tx.executeSql(`SELECT ...`)
.then([tx2, results]) => {

}
.catch((error) => {
console.log(error);
});
});

关于javascript - 尝试打开事务时使用react-native-sqlite-storage停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51089109/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com