diff --git a/example/pymysql_CRDU_student.py b/example/pymysql_CRDU_student.py index 834e87c..f3d74bd 100644 --- a/example/pymysql_CRDU_student.py +++ b/example/pymysql_CRDU_student.py @@ -9,37 +9,60 @@ ''' import pymysql + +def connect_db(): + """链接数据库 """ + db = pymysql.connect(host="127.0.0.1", port=3306, user='root', password="123123", charset="utf8",database="example") # 链接数据库 + return db + +def close_db(db:pymysql.connect): + """关闭数据库链接""" + db.close() + def create_table(db:pymysql.connect): - """ - 创建表 - :param db: 数据库链接对象 - :return: - """ - cursor = db.cursor() - cursor.execute("DROP TABLE IF EXISTS student;") + """创建表""" + cursor = db.cursor() # 创建游标 + cursor.execute("DROP TABLE IF EXISTS student;") # 如果存在student表先删除 sql = """CREATE TABLE Student ( ID CHAR(10) NOT NULL, Name VARCHAR(20), Grade INT - );""" - cursor.execute(sql) - cursor.close() + );""" # 构建创建表结构的SQL语句 + cursor.execute(sql) # 通过游标执行语句 + cursor.close() # 关闭游标 def insert_table(db:pymysql.connect): + """插入数据""" cursor = db.cursor() sql = """INSERT INTO Student VALUES ('001', 'CZQ', 70),('002', 'LHQ', 80),('003', 'MQ', 90), ('004', 'WH', 80), ('005', 'HP', 70),('006', 'YF', 66),('007', 'TEST', 100);""" try: cursor.execute(sql) - db.commit() + db.commit() # 插入操作必须提交事务才能成功 cursor.close() except: - db.rollback() + db.rollback() # 发生错误时回滚 + cursor.close() + + +def query_table(db:pymysql.connect): + """查询表""" + cursor = db.cursor() + sql = """SELECT * FROM student""" + try: + cursor.execute(sql) + results = cursor.fetchall() # 获取所有记录列表 + for row in results: # 遍历所有表记录 + print(row) # 打印输出 + cursor.close() + except: + db.rollback() # 发生错误时回滚 cursor.close() def delete_db(db:pymysql.connect): + """删除数据 """ cursor = db.cursor() sql = "DELETE FROM Student WHERE Grade = %d;"% 100 print(sql) @@ -54,6 +77,7 @@ def delete_db(db:pymysql.connect): def update_db(db:pymysql.connect): + """更新数据""" cursor = db.cursor() sql = "UPDATE Student SET Grade = Grade + 3 WHERE ID = '%s';" % '003' print(sql) @@ -69,27 +93,31 @@ def update_db(db:pymysql.connect): if __name__ == '__main__': - db = pymysql.connect(host="127.0.0.1", port=3306, user='root', password="123123", charset="utf8", - database="student") # 链接数据库 - - create_table(db) - - insert_table(db) - - delete_db(db) - - update_db(db) - - # 检查数据库的变化 - cursor2 = db.cursor() - cursor2.execute("show tables;") # 显示所有表 - print(cursor2.fetchall()) - cursor2.execute("DESCRIBE student;") # 查看表结构 - print(cursor2.fetchall()) - - cursor2.execute("SELECT * FROM student;") # 查看student表所有记录 - print(cursor2.fetchall()) - - - cursor2.close() - db.close() # 关闭链接 \ No newline at end of file + db = connect_db() # 链接数据库 + query_table(db) # 查询表 + update_db(db) # 修改记录 + query_table(db) # 查询表 + # close_db(db) # 关闭数据库链接释放资源 + + + + + # create_table(db) # 创建表 + # insert_table(db) # 插入数据 + # # delete_db(db) + # # + # # update_db(db) + # + # # 检查数据库的变化 + # cursor2 = db.cursor() + # cursor2.execute("show tables;") # 显示所有表 + # print(cursor2.fetchall()) + # cursor2.execute("DESCRIBE student;") # 查看表结构 + # print(cursor2.fetchall()) + # + # cursor2.execute("SELECT * FROM student;") # 查看student表所有记录 + # print(cursor2.fetchall()) + + + # cursor2.close() + # db.close() # 关闭链接 \ No newline at end of file