You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

18 lines
788 B

5 months ago
import pymysql
if __name__ == '__main__':
# 建立数据库连接的语句connection
connection = pymysql.connect(user="root", password="Wyz010810", database="SCT", host="127.0.0.1", port=3306)
cursor = connection.cursor() # 创建游标
sql = "select * from student" # SQL语句字符串
# 为了防止SQL注入等安全问题可以使用参数化查询。
try:
cursor.execute(sql) # 执行SQL语句
ResultList = cursor.fetchall() # 获取游标中的数据
print(ResultList) # Student[(S#,Sname,Sage,...),...]
except pymysql.Error as e: # 捕获报错信息
print("MySQL Error:", e) # 打印报错信息
finally:
cursor.close() # 关闭游标
connection.close() # 关闭数据库连接