parent
f2c4455441
commit
20c3879138
@ -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()
|
@ -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() # 关闭链接
|
Loading…
Reference in new issue