pymysql连接数据库

import pymysql
from pymysql.cursors import DictCursor
res = pymysql.connect(‘localhost‘, ‘root‘, ‘123456‘, ‘2007a‘)
try:
res1 = res.cursor(DictCursor)
except: print(‘连接失败‘)
else: print(‘连接成功‘)
mystr = ‘1‘ mystr1 = ‘18‘
str = "select * from student where id=%s and age > %s"
try:
a = res1.execute(str,[mystr,mystr1])
except:
res.rollback()
print(‘查询失败‘)
else:
print(res1.fetchall())
print(str)
print(‘查询成功‘)
finally:
print(‘唱歌‘)
res.close()
res1.close()

pymysql连接数据库

上一篇:mongodb安装


下一篇:MySQL索引教程