最近大作业用到了python操作数据库的内容。涉及的库是pymmysql,我就不详细介绍这个库的操作了,直接奔入主题--->开整
背景:
涉及程序中一个实时查看数据表中state字段==1的功能,我把这个功能单独择出来写成了下面的代码:
1 #-*- coding=utf-8 -*-
2 importpymysql3 config={4 "host":"127.0.0.1",5 "user":"root",6 "password":"root",7 "database":"Person"
8 }9 db = pymysql.connect(**config)10 cursor=db.cursor()11 whileTrue:12 a=input("Input something interesting:")13 sql = "select username from client where state=1"
14 try:15 cursor.execute(sql)16 result=cursor.fetchall()17 except:18 db.rollback()19 print(result)20 cursor.close()21 db.close()
这是数据库Person的数据表client内容,关键在最后的state内容
执行这个代码:python3 testdb.py 出现下面效果:
现在看起来是正确的