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.

31 lines
1.0 KiB

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
# 创建mysql连接连接并打开数据库
conn = pymysql.connect(user="root", password="Wyz010810", database="SCT", host="127.0.0.1", port=3306)
cursor = conn.cursor() # 游标的定义、打开
# 字符串型SQL语句的执行
SQLString = "INSERT INTO student VALUES ('20220001', '张三', '', 20, '01')"
cursor.execute(SQLString)
SQLString = "INSERT INTO course VALUES ('1001', '数据库', 3, 48, 'T01')"
cursor.execute(SQLString)
SQLString = "INSERT INTO sc VALUES ('20230001', '1001', 85)"
cursor.execute(SQLString)
conn.commit()
def query_data(table_name):
"""
游标数据的读写处理,读写游标数据
:param table_name: 表名称
:return: 游标数据
"""
SQLString = f"SELECT * FROM {table_name}"
cursor.execute(SQLString) # 执行查询sql语句
data = cursor.fetchall() # 获取游标中的数据
return data
StudentList = query_data("student")
CourseList = query_data("course")
SCList = query_data("sc")
# 关闭数据库连接
cursor.close()
conn.close()