25.4.14
This commit is contained in:
parent
f58665e192
commit
72dd4770eb
9
testdb.sql
Normal file
9
testdb.sql
Normal file
@ -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
|
||||||
|
);
|
65
作业2.py
Normal file
65
作业2.py
Normal file
@ -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;")
|
13
案例1.py
Normal file
13
案例1.py
Normal file
@ -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()
|
13
案例2.py
Normal file
13
案例2.py
Normal file
@ -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()
|
15
案例3.py
Normal file
15
案例3.py
Normal file
@ -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()
|
22
案例4.py
Normal file
22
案例4.py
Normal file
@ -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()
|
15
案例5.py
Normal file
15
案例5.py
Normal file
@ -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()
|
Loading…
x
Reference in New Issue
Block a user