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.
|
|
|
|
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()
|