我正在使用react-native-sqlite-storage在我的React Native应用程序中维护书籍数据。我正在使用
updateData()
来更新书籍的数据:
async function updateData({ book_id, author, price }) {
return new Promise((resolve, reject) => {
return db.transaction(tx => {
return tx.executeSql(
'UPDATE books SET author = ?, price = ? where book_id= ? ',
[author, price, book_id],
(tx, res) => {
console.log(
' data updated',
tx,
'\nres===>',
res,
);
resolve(true);
},
error => {
console.log('insert data error in updateData: ', error);
reject(false);
},
);
});
});
}
console.log()
结果:
data updated {"db": {"dbname": "test.db", "openError": [Function errorcb], "openSuccess": [Function errorcb], "openargs": {"dblocation": "nosync", "name": "test.db"}}, "error": undefined, "executes": [], "fn": [Function anonymous], "readOnly": false, "success": undefined, "txlock": true}
res===> {"insertId": undefined, "rows": {"item": [Function item], "length": 0, "raw": [Function raw]}, "rowsAffected": 1}
我想获取更新的行数据作为响应。我怎样才能做到这一点?我尝试控制台记录执行查询后返回的值。
更新sql不会返回结果,需要更新,然后选择相同的记录。这是一个例子:
db.transaction(tx => {
tx.executeSql(
'UPDATE table_name SET column_name = ? WHERE id = ?',
[newValue, id],
(tx, results) => {
console.log('Update completed');
// Query the updated record
tx.executeSql(
'SELECT * FROM table_name WHERE id = ?',
[id],
(tx, results) => {
if (results.rows.length > 0) {
const updatedRecord = results.rows.item(0);
console.log('Updated record: ', updatedRecord);
} else {
console.log('Record not found');
}
},
error => {
console.log('Error during select: ', error);
}
);
},
error => {
console.log('Error during update: ', error);
}
);
});