[英]Sqlite delete a row with a defined id using curl and express
我已經使用sqlite創建了一個數據庫。 該表如下所示:
[{"item:[{"id":2,"name":"mara","html":"Hallo","minimum_price":23,"buyout_price":45,"created_on":1564785,"duration":60,"seller":"Host","state":"offered"}, "bids":[{"id":1,"price":50,"buyer":"Host","item_id":2}]}]
現在,我想實現一個delete函數,該函數刪除具有定義的id並寫在url中的行(我正在使用curl來調用函數)。 到目前為止,我已經做到了:
app.delete('/auction/:ID', function (req, res) {
var deletedId = req.params.ID;
async function deleteItem() {
try {
const dbPromise = sqlite.open('./database.sqlite', {
Promise
});
const db = await dbPromise;
await Promise.all([
db.run('DELETE FROM items WHERE id = ?', deletedId),
db.run('DELETE FROM bids WHERE item_id = ?', deletedId)
]);
res.send("Completed deletion of id: " + deletedId);
} catch (err) {
console.error(err);
}
};
deleteItem();
});
事情是我刪除一行,全部,數據庫正在更新。 但是,當我嘗試刪除不存在的ID的行或再次使用相同的ID時,控制台會說:
C:\Users\User>curl --request DELETE http://localhost:9000/auction/2
Completed deletion of id: 2
C:\Users\User>curl --request DELETE http://localhost:9000/auction/2
Completed deletion of id: 2
我必須以某種方式在URL中提供的ID與所有現有ID之間進行比較,以查看其是否存在,但到目前為止我還沒有想到任何有效的方法。
先感謝您! 期待您的回答! :)
alert(1)
alert(1)
<a href=[�]"� onmouseover=prompt(1)//">XYZ</a
真的有效嗎?
await Promise.all([non-promise-func, non-promise-func])
this.changes
包含受查詢影響的行數,例如
db.run('delete from items where id = ?', [10], function (err) {
// Don't use arrow function (=>) because you need `this` context
if (err)
return console.error(err);
console.log('Deleted: ' + this.changes);
});
我不知道您說all existing ids, to see if it's there
時是什么意思all existing ids, to see if it's there
:項目計數,投標計數,總和? 所以我認為這是一個總結。 由於node-sqlite3
是采用強回調樣式編寫的,因此您需要使它們成對 ,或向回調地獄打個招呼:)
let deleted = 0;
db.run('delete from items where id = ?', [10], function (err) {
if (err)
console.error(err);
deleted += err ? 0 : this.changes;
db.run('delete from bids where item_id = ?', [10], function (err) {
if (err)
console.error(err);
deleted += err ? 0 : this.changes;
res.send("Completed deletion of id: " + deleted);
})
});
另一種情況:您可以after delete
-trigger之后用於items
-table ,以便delete from items where id = ?
執行delete from items where id = ?
返回兩個表中所有已刪除行的計數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.