gpt4 book ai didi

javascript - 如何使 tx.executeSql() 函数与 async/await 同步?

转载 作者:行者123 更新时间:2023-11-30 08:19:28 25 4
gpt4 key购买 nike

如何使 tx.executeSql(SQL, [], (tx, results) 成为带有 sync/await 的同步函数?

我想做这样的事情:

tx.executesql 是异步的,所以我的 closeDB() (closeDatabase()) 不起作用,因为事务正在进行中。

await tx.executeSql(sql, [], (tx, results)

但它不起作用。

(查看输出)

这是我在输出末尾收到的警告:

C:\...name of proyect\node_modules\react-native\Libraries\Components\ScrollView\InternalScrollViewType.js:46 Possible Unhandled Promise Rejection (id: 0):
TypeError: error is not a function
TypeError: error is not a function
at SQLitePlugin.close (blob:http://localhost:8081/7a5bb020-64e9-41f5-a654-e50e6b9f5827:93749:9)
at _callee4$ (blob:http://localhost:8081/7a5bb020-64e9-41f5-a654-e50e6b9f5827:92942:54)
at tryCatch (blob:http://localhost:8081/7a5bb020-64e9-41f5-a654-e50e6b9f5827:23006:19)
.
.
.
.
async componentDidMount() {


console.log('*COMIENZA componentDidMount');
await this.consultarBD();
await this.openBD();

console.log('DataUsuarios es: ' + this.state.dataUsuarios);
await this.insertarDatos();
await this.closeDatabase();

console.log('*TERMINA componentDidMount');

}
 insertarDatos = async () =>{
console.log('COMIENZA insertarDatos');
await db.transaction((tx) => {
console.log('Comienza Transaction');
for(let i in this.state.dataUsuarios){
const sql = `INSERT INTO usuarios VALUES (
'${this.state.dataUsuarios[i].id}',
'${this.state.dataUsuarios[i].nombre}',
'${this.state.dataUsuarios[i].usuario}',
'${this.state.dataUsuarios[i].password}',
'${this.state.dataUsuarios[i].grupo}',
'${this.state.dataUsuarios[i].faenas}'
)`;
console.log('antes de excute i es: ' + i);
tx.executeSql(sql, [], (tx, results) => {
console.log('dentro de executesql i es: ' + i);
if(results.rowsAffected > 0){
console.log('¡Datos insertados en BD!');
}else{
console.log('¡No se puedieron insertar datos en la BD!');
}(error) => {
console.log(error.message);
}
});
}
console.log('termina transaccion');
});

console.log('TERMINA insertarDatos');

}

输出:



C:\Users\josen\React-Projects\Ges\src\Screen\Login.js:22 COMIENZA insertarDatos
C:\Users\josen\React-Projects\Ges\src\Screen\Login.js:88 Comienza Transaction
C:\Users\josen\React-Projects\Ges\src\Screen\Login.js:88 antes de excute i es: 0
C:\Users\josen\React-Projects\Ges\src\Screen\Login.js:88 antes de excute i es: 1
C:\Users\josen\React-Projects\Ges\src\Screen\Login.js:88 antes de excute i es: 2
C:\Users\josen\React-Projects\Ges\src\Screen\Login.js:88 antes de excute i es: 3
C:\Users\josen\React-Projects\Ges\src\Screen\Login.js:90 termina transaccion
C:\Users\josen\React-Projects\Ges\src\Screen\Login.js:112 TERMINA insertarDatos
C:\Users\josen\React-Projects\Ges\src\Screen\Login.js:128 Cerrando database ...
C:\Users\josen\React-Projects\Ges\src\Screen\DrawerScreens\index.js:7 cannot close: transaction is in progress
C:\Users\josen\React-Projects\Ges\src\Screen\Login.js:91 dentro de executesql i es: 0
C:\Users\josen\React-Projects\Ges\src\Screen\Login.js:93 ¡Datos insertados en BD!
C:\Users\josen\React-Projects\Ges\src\Screen\Login.js:91 dentro de executesql i es: 1
C:\Users\josen\React-Projects\Ges\src\Screen\Login.js:93 ¡Datos insertados en BD!
C:\Users\josen\React-Projects\Ges\src\Screen\Login.js:91 dentro de executesql i es: 2
C:\Users\josen\React-Projects\Ges\src\Screen\Login.js:93 ¡Datos insertados en BD!
C:\Users\josen\React-Projects\Ges\src\Screen\Login.js:91 dentro de executesql i es: 3
C:\Users\josen\React-Projects\Ges\src\Screen\Login.js:93 ¡Datos insertados en BD!

最佳答案

制作executeSql()async/await方法:

async function example() {
let db = await SQLite.openDatabase(
database_name,
database_version,
database_displayname,
database_size
);

await db.transaction(async tx => {
var [tx, results] = await tx.executeSql("SELECT * from TableName;");
var row = results.rows.item(0); //get the first row assumed existed.
});

await db.close();
}

关于javascript - 如何使 tx.executeSql() 函数与 async/await 同步?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56212945/

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