- Cloud Native Python
- Manish Sethi
- 187字
- 2021-07-02 19:11:59
DELETE /api/v1/users
The delete method helps remove a specific record, which is defined by a username. We will pass username as the JSON object that needs to be deleted from the database.
The following code snippet will create a new route in app.py for the DELETE method for users:
@app.route('/api/v1/users', methods=['DELETE']) def delete_user(): if not request.json or not 'username' in request.json: abort(400) user=request.json['username'] return jsonify({'status': del_user(user)}), 200
In the next code snippet, we will call del_user, which deletes the user record specified by username after validating whether it exists or not:
def del_user(del_user): conn = sqlite3.connect('mydb.db') print ("Opened database successfully"); cursor=conn.cursor() cursor.execute("SELECT * from users where username=? ",
(del_user,)) data = cursor.fetchall() print ("Data" ,data) if len(data) == 0: abort(404) else: cursor.execute("delete from users where username==?",
(del_user,)) conn.commit() return "Success"
Great! We have added the route /handler for the DELETE method for the user resource; let's test it using the following test API call:
curl -i -H "Content-Type: application/json" -X delete -d '{
"username":"manish123" }' http://localhost:5000/api/v1/users
Then, hit the user list API (curl http://localhost:5000/api/v1/users) to see if the changes have been made:

Awesome! User deletion is successful.
推薦閱讀
- 數(shù)據(jù)庫程序員面試筆試真題與解析
- Apache Hive Essentials
- Java程序員面試算法寶典
- JavaScript前端開發(fā)與實(shí)例教程(微課視頻版)
- HTML5游戲開發(fā)案例教程
- Windows Server 2016 Automation with PowerShell Cookbook(Second Edition)
- 從0到1:Python數(shù)據(jù)分析
- Learning Python Design Patterns
- Getting Started with Laravel 4
- Python數(shù)據(jù)結(jié)構(gòu)與算法(視頻教學(xué)版)
- Unity UI Cookbook
- SciPy Recipes
- ActionScript 3.0從入門到精通(視頻實(shí)戰(zhàn)版)
- 從零開始學(xué)Selenium自動(dòng)化測試:基于Python:視頻教學(xué)版
- Python物理建模初學(xué)者指南(第2版)