python操作数据之查询
import pymysql
def query(sql):
"""
查询数据
"""
con = pymysql.connect(
host="**********",
port=3306,
user="root",
password="*******",
database="*****",
charset='utf8'
) # 链接数据库
cur = con.cursor() # 获取游标
cur.execute(sql) #执行sql
for id, instance, plan in cur.fetchmany(size=3): #查询前3个
print(id,instance,plan)
cur.close() # 关闭游标
con.close() # 关闭链接
if __name__=='__main__':
sql = "select * from plan"
query(sql)