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.
StuSystem/Pymysql_Sample.py

18 lines
788 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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() # 关闭数据库连接