I am using react-native-sqlite-storage to maintain books data in my React Native app. I am using updateData()
to update book's data:
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()
result:
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}
I want to get the updated row data in response. How can I do that? I tried console logging the values returned after the query is executed.
Update sql will not return the result, you need to update, then select same record. Here is an example:
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);
}
);
});