一、引包:
要想在Python中进行MySQL数据库的操作需要引入MySQL的相关资源包:
方法一:pip命令引入(通用)
pip install pymysql
方法二:直接引入(PyCharm)
在程序中直接输入import pymysql
根据修改意见选择 install所在选项自动引入
方法三:PyCharm 引入
文件 --> 设置 -->项目 -->project Interpreter --> pip --> 搜索框输入 pymysql
–> 选中pymysql点击 Install packge引入
二、连接MySQL数据库:
import pymysql
# 连接mysql
# 参数1:表示主机或ip地址
# 参数2:表示mysql的用户名
# 参数3:表示mysql的密码
# 参数4:表示mysql的数据库名
# conn = pymysql.connect('localhost', 'root', 'root', 'mydb2')
conn = pymysql.connect('10.36.133.6', 'root', 'root', 'mydb2')
# 创建游标对象: 可以执行sql语句
cursor = conn.cursor()
# sql语句
sql = 'select version()'
# 执行sql语句
cursor.execute(sql)
res = cursor.fetchone()
print(res)
# 关闭游标对象
cursor.close()
# 关闭mysql的连接
conn.close()
三、增加数据操作:
import pymysql
conn = pymysql.connect('localhost', 'root', 'root', 'mydb2')
cursor = conn.cursor()
# 插入数据
sql = 'insert into person(name, age) values("aa", 20)'
try:
cursor.execute(sql)
# 提交事务
conn.commit()
except:
# 回滚
conn.rollback()
cursor.close()
conn.close()
四、删除数据操作:
import pymysql
conn = pymysql.connect('localhost', 'root', 'root', 'mydb2')
cursor = conn.cursor()
# 删除数据
sql = 'delete from person where id=18'
try:
cursor.execute(sql)
# 提交事务
conn.commit()
except:
# 回滚
conn.rollback()
cursor.close()
conn.close()
五、更新数据操作:
import pymysql
conn = pymysql.connect('localhost', 'root', 'root', 'mydb2')
cursor = conn.cursor()
# 更新数据
sql = 'update person set age=30 where id=20'
try:
cursor.execute(sql)
# 提交事务
conn.commit()
except:
# 回滚
conn.rollback()
cursor.close()
conn.close()
六、查询数据操作:
import pymysql
conn = pymysql.connect('localhost', 'root', 'root', 'mydb2', charset='utf8')
cursor = conn.cursor()
# 查询数据
sql = 'select * from person'
# 执行sql
cursor.execute(sql)
# fetchone() : 每次查询下一条数据
# res = cursor.fetchone()
# print(res)
# res = cursor.fetchone()
# print(res)
# res = cursor.fetchone()
# print(res)
# fetchall() : 所有数据
res = cursor.fetchall()
# res = cursor.fetchmany(3) # 前3条数据
for row in res:
print(row)
print(cursor.rowcount) # 总的数据条数
cursor.close()
conn.close()
七、上期习题答案:
- 递归删除文件夹(可能包含子文件或子文件夹)
【温馨提示:创建一个文件夹,不要直接操作已有的文件夹】
提示:要先将文件夹中的所有子文件删除再删除本文件夹
remove(): 删除文件
rmdir(): 删除空目录
import os
def delAll(path):
if os.path.isdir(path):
files = os.listdir(path)
# 遍历并删除文件
for file in files:
p = os.path.join(path, file)
if os.path.isdir(p):
# 递归
delAll(p)
else:
os.remove(p)
# 删除文件夹
os.rmdir(path)
else:
os.remove(path)
if __name__ == '__main__':
delAll(r'D:\python\newdir')
- 拷贝文件【考虑大文件拷贝,每次读取1024字节拷贝】
import os
def copy_file(path):
if not os.path.exists(path):
return
i = path.rfind('.')
path2 = path[:i]+'_副本'+path[i:]
fp1 = open(path, 'rb') # 读取
fp2 = open(path2, 'wb') # 写入
while True:
content = fp1.read(1024)
if not content:
break
fp2.write(content)
fp1.close()
fp2.close()
if __name__ == '__main__':
copy_file(r'D:\python\aa.png')
- 使用函数递归,分别统计文件夹newdir中文件和文件夹的个数
提示:统计当前目录下的文件数量和文件夹数量
如果碰到文件,则文件数量+1
如果碰到文件夹,则文件夹数量+1,递归调用fn()并传入当前子文件夹目录
import os
dict_swd = {'files': 0, 'dirs': 0}
def fn(path):
if not os.path.exists(path):
return
file_list = os.listdir(path)
for file in file_list:
file_path = os.path.join(path, file)
if os.path.isfile(file_path):
dict_swd['files'] += 1
else:
dict_swd['dirs'] += 1
fn(file_path)
return dict_swd
if __name__ == '__main__':
print(fn(r'D:\python\newdir'))