import pymysql; db = pymysql.connect(host = "localhost",user = "root", password = "root",db = "liupeng");#进入数据库 #缓存 cur = db.cursor(); #查询 sql = "select * from user"; try : cur.execute(sql); results = cur.fetchall();#查询所有 print("id","name","passwd","iamg"); for row in results: id = row[0]; name = row[1]; passwd = row[2]; iamg = row[3]; print(id ,name ,passwd,iamg); except Exception as e: raise e; #触发异常 finally: #无论代码是否错误,都执行最后一段 db.close();
#提示使用代码前要pip install pymysql ,数据库要设置好
原文:https://www.cnblogs.com/shenweizhi/p/10492056.html