python web.py操作mysql数据库,实现对数据库的增删改查操作
发布时间:2020-12-17 17:06:00 所属栏目:Python 来源:网络整理
导读:使用web.py框架,实现对mysql数据库的增删改查操作: 该示例代码中连接的是本地数据库testdb,user表,表结构比较简单,只有两个字段:mobile和passwd,类型均为字符型 实际应用过程中,请根据自己需要更改配置信息和字段名称 1 import web 2 db = web.datab
使用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('未查到符合条件的数据') ? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |