diff --git a/example/mysql_conn.py b/example/mysql_conn.py index b129116..9ed34de 100644 --- a/example/mysql_conn.py +++ b/example/mysql_conn.py @@ -8,15 +8,40 @@ 2023/4/13 13:59 zart20 1.0 None ''' +# pymysql 链接示例 + import pymysql -db = pymysql.connect(host="127.0.0.1", port=3306, user='root', password="123123", charset="utf8", database="student") -cursor = db.cursor() +# db = pymysql.connect(host="127.0.0.1", port=3306, user='root', password="123123", charset="utf8") # 链接数据库管理系统 +db = pymysql.connect(host="127.0.0.1", port=3306, user='root', password="123123", charset="utf8", database="student") # 链接数据库 + +cursor = db.cursor() # 实例化游标对象 -cursor.execute("show tables;") +cursor.execute("show databases;") # 显示所有数据库 +cursor.execute("use student;") # 选择指定数据库 +cursor.execute("show tables;") # 执行数据库查询 -data = cursor.fetchall() +data = cursor.fetchall() # 取出游标中所有内容 print(data) -cursor.close() \ No newline at end of file +cursor.close() + +c2 = db.cursor() +# c2.execute("show databases;") +c2.execute("show tables;") +print(c2.fetchall()) + +# db.close() # 关闭数据库链接 + +# c3 = db.cursor() +# c3.execute("show databases;") +# print(c3.fetchall()) + + + + + + + + diff --git a/example/mysql_conn2.py b/example/mysql_conn2.py new file mode 100644 index 0000000..80e8673 --- /dev/null +++ b/example/mysql_conn2.py @@ -0,0 +1,25 @@ +# -*- encoding: utf-8 -*- +''' +@File : mysql_conn2.py +@License : (C)Copyright 2018-2022 + +@Modify Time @Author @Version @Desciption +------------ ------- -------- ----------- +2023/4/16 18:13 zart20 1.0 None +''' + +# mysql-connector-python 链接示例 + +import mysql.connector + +db = mysql.connector.connect(host="127.0.0.1", port=3306, user='root', password="123123", charset="utf8", database="student") + +cursor = db.cursor() + +cursor.execute("show databases;") + +data = cursor.fetchall() + +print(data) + +cursor.close() \ No newline at end of file diff --git a/example/pymysql_CRDU_student.py b/example/pymysql_CRDU_student.py new file mode 100644 index 0000000..834e87c --- /dev/null +++ b/example/pymysql_CRDU_student.py @@ -0,0 +1,95 @@ +# -*- encoding: utf-8 -*- +''' +@File : creat_student.py +@License : (C)Copyright 2018-2022 + +@Modify Time @Author @Version @Desciption +------------ ------- -------- ----------- +2023/4/16 18:51 zart20 1.0 None +''' +import pymysql + +def create_table(db:pymysql.connect): + """ + 创建表 + :param db: 数据库链接对象 + :return: + """ + cursor = db.cursor() + cursor.execute("DROP TABLE IF EXISTS student;") + sql = """CREATE TABLE Student ( + ID CHAR(10) NOT NULL, + Name VARCHAR(20), + Grade INT + );""" + 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() + 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) + try: + cursor.execute(sql) + db.commit() + cursor.close() + except Exception as e: + print(e) + db.rollback() + cursor.close() + + +def update_db(db:pymysql.connect): + cursor = db.cursor() + sql = "UPDATE Student SET Grade = Grade + 3 WHERE ID = '%s';" % '003' + print(sql) + try: + cursor.execute(sql) + db.commit() + cursor.close() + except Exception as e: + print(e) + db.rollback() + cursor.close() + + + +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