diff --git a/testdb.sql b/testdb.sql new file mode 100644 index 0000000..4ecaeb9 --- /dev/null +++ b/testdb.sql @@ -0,0 +1,9 @@ +CREATE DATABASE TESTDB; +USE TESTDB; +CREATE TABLE EMPLOYEE ( + FIRST_NAME CHAR(20) NOT NULL, + LAST_NAME CHAR(20), + AGE INT, + SEX CHAR(1), + INCOME FLOAT +); diff --git a/作业2.py b/作业2.py new file mode 100644 index 0000000..08b2f85 --- /dev/null +++ b/作业2.py @@ -0,0 +1,65 @@ +import pymysql +# 定义 数据库工具类 +class DBTools(object): + # 创建连接 - 类方法。可以直接使用类名调用! + @classmethod + def __create_conn(cls): + conn = pymysql.connect(host="172.16.28.4", port=3306, user="root", password="root", database="books", charset="utf8") + # 不能遗漏 + return conn + + # 查一条记录 - 封装为类方法,方便调用 + @classmethod + def query_one(cls, sql): + my_conn = None + my_cursor = None + res = None + try: + # 创建连接, 借助类名,调用 类方法 create_conn + my_conn = DBTools.__create_conn() + # 创建游标 + my_cursor = my_conn.cursor() + # 执行 sql 语句,做查询 + my_cursor.execute(sql) + # 提取一条记录 + res = my_cursor.fetchone() + except Exception as err: + print("执行查询SQL失败:", str(err)) + finally: + # 关闭游标 + my_cursor.close() + # 关闭连接 + my_conn.close() + # 返回查询结果 + return res + + # 增删改记录 + @classmethod + def db_uid(cls, sql): + my_conn = None + my_cursor = None + try: + # 创建连接 + my_conn = DBTools.__create_conn() + # 创建游标 + my_cursor = my_conn.cursor() + # 执行 增删改 语句 + my_cursor.execute(sql) + print("Affected rows:", my_conn.affected_rows()) + # 提交事务 + my_conn.commit() + except Exception as err: + print("执行 增删改 SQL 失败:", str(err)) + # 回滚事务 + my_conn.rollback() + finally: + # 关闭游标 + my_cursor.close() + # 关闭连接 + my_conn.close() + +if __name__ == '__main__': + result = DBTools.query_one("select * from t_hero;") + print("查询语句的结果:", result) + + DBTools.db_uid("update t_book set `read` = 100 where id = 3;") diff --git a/案例1.py b/案例1.py new file mode 100644 index 0000000..129db97 --- /dev/null +++ b/案例1.py @@ -0,0 +1,13 @@ +import pymysql +db=pymysql.connect ( + host='localhost', + user='root', + passwd='114514', + database='TESTDB', +) +cursor=db.cursor() +cursor.execute('SELECT VERSION()') +data = cursor.fetchone() +print('Database version : %s ' % data) +cursor.close() +db.close() \ No newline at end of file diff --git a/案例2.py b/案例2.py new file mode 100644 index 0000000..209d954 --- /dev/null +++ b/案例2.py @@ -0,0 +1,13 @@ +import pymysql +db=pymysql.connect ( + host='localhost', + user='root', + passwd='114514', + database='TESTDB', +) +cursor=db.cursor() +cursor.execute('DROP TABLE IF EXISTS EMPLOYEE') +data=cursor.fetchone() +print(data) +cursor.close() +db.close() \ No newline at end of file diff --git a/案例3.py b/案例3.py new file mode 100644 index 0000000..7da5e94 --- /dev/null +++ b/案例3.py @@ -0,0 +1,15 @@ +import pymysql +db=pymysql.connect ( + host='localhost', + user='root', + passwd='114514', + database='TESTDB', +) +cursor=db.cursor() +sql="INSERT INTO EMPLOYEE(FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUE ('Mac','Mohan',20,'M',2000)" +try: + cursor.execute(sql) + db.commit() +except: + db.rollback() +db.close() \ No newline at end of file diff --git a/案例4.py b/案例4.py new file mode 100644 index 0000000..1b66dd8 --- /dev/null +++ b/案例4.py @@ -0,0 +1,22 @@ +import pymysql +db=pymysql.connect ( + host='localhost', + user='root', + passwd='114514', + database='TESTDB', +) +cursor=db.cursor() +sql="SELECT * FROM EMPLOYEE WHERE INCOME > %s" % 1000 +try: + cursor.execute(sql) + result=cursor.fetchall() + for row in result: + fname=row[0] + lname=row[1] + age=row[2] + sex=row[3] + income=row[4] + print(f"fname={fname},lname={lname},age={age},income={income}") +except: + print("Error: unable to fetch data") +db.close() \ No newline at end of file diff --git a/案例5.py b/案例5.py new file mode 100644 index 0000000..68f1884 --- /dev/null +++ b/案例5.py @@ -0,0 +1,15 @@ +import pymysql +db=pymysql.connect ( + host='localhost', + user='root', + passwd='114514', + database='TESTDB', +) +cursor=db.cursor() +sql="UPDATE EMPLOYEE SET AGE=AGE+1 WHERE SEX = '%c'" % 'M' +try: + cursor.execute(sql) + db.commit() +except: + db.rollback() +db.close() \ No newline at end of file