python web.py操作mysql數據庫,實現對數據庫的增刪改查操作


使用web.py框架,實現對mysql數據庫的增刪改查操作:

該示例代碼中連接的是本地數據庫testdb,user表,表結構比較簡單,只有兩個字段:mobile和passwd,類型均為字符型

實際應用過程中,請根據自己需要更改配置信息和字段名稱

 1 import web
 2 db = web.database(
 3     dbn='mysql',
 4     host = 'localhost',
 5     user = 'root',
 6     pw = 'root',
 7     db = 'testdb',
 8     charset = 'utf8'
 9 )
10 
11 # 插入數據
12 result = db.query('insert into user(mobile,passwd) values("13100010004","666666")')
13 print(result)
14 
15 # 刪除數據
16 result = db.query('delete from user where mobile="13100010004"')
17 print(result)
18 
19 # 修改數據
20 result = db.query('update user set passwd="888888" where mobile="13100010003"')
21 print(result)
22 
23 result = list(db.query('select * from user where mobile="123"'))
24 if len(result) > 0:
25     for item in result:
26         print(item.get('mobile'))
27 else:
28     print('未查到符合條件的數據')

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM