"""
python 操作mysql时,默认开启事务,必须在增删改之后
提交数据,才会真正对数据库发生变化,默认默认是回滚
提交数据: conn.commit()
回滚数据: conn.rollback()
execute 一次插入一条
executemany 一次插入多条
"""
import pymysql
# 1.创建mysql 链接
conn = pymysql.connect(host="127.0.0.1",user="root",password="123456",database="db0826")
# 查询数据,默认是元组,可以设置返回的类型为字典 pymysql.cursors.DictCursor
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # cursor=pymysql.cursors.DictCursor
################ (1) 增
"""
sql = "insert into t1(first_name,last_name,age,sex,money) values(%s,%s,%s,%s,%s)"
# 一次插入一条
res = cursor.execute(sql, ("宋","云杰",30,0,15000) )
print(res) # 1
# 获取最后插入这条数据的id号(针对单条数据插入)
print(cursor.lastrowid) # 3
res = cursor.executemany( sql, [ ("高","云峰",50,1,16000) , ("戈","隆",80,1,17000) , ("袁","伟倬",120,0,130000) , ("刘","欣慰",150,0,18000) ] )
print(res) # 插入的条数
# 针对于多条数据,搜最后的id 可以通过倒序查询id
sql = "select id from t1 order by id desc limit 1"
res = cursor.execute(sql)
print(res)
# 获取最后一个id号
res = cursor.fetchone()
print(res)
"""
################### 删
"""
sql = "delete from t1 where id = %s"
res = cursor.execute(sql , (3,))
print(res)
if res:
print("删除成功")
else:
print("删除失败")
"""
################## 改
"""
sql = "update t1 set first_name = %s where id = %s"
res = cursor.execute(sql,("王",4))
print(res)
if res:
print("修改成功")
else:
print("修改失败")
"""
#################### 查
"""
fetchone fetchmany fetchall 都是基于上一条数据往下查询.
"""
sql = "select * from t1"
res = cursor.execute(sql)
print(res) # 总条数
# (1) 获取一条数据 fetchone
# {'id': 4, 'first_name': '王', 'last_name': '云杰', 'age': 30, 'sex': 0, 'money': 15000.0}
res = cursor.fetchone()
print(res)
# (2) 获取多条数据 fetchmany
data = cursor.fetchmany() # 默认搜索的的是一条数据
print(data)
data = cursor.fetchmany(3)
print(data)
"""
[
{'id': 6, 'first_name': '高', 'last_name': '云峰', 'age': 50, 'sex': 1, 'money': 16000.0},
{'id': 7, 'first_name': '戈', 'last_name': '隆', 'age': 80, 'sex': 1, 'money': 17000.0},
{'id': 8, 'first_name': '袁', 'last_name': '伟倬', 'age': 120, 'sex': 0, 'money': 130000.0}
]
"""
for row in data :
# print(row)
first_name = row["first_name"]
last_name = row["last_name"]
age = row["age"]
if row["sex"] == 0:
sex = "女性"
else:
sex = "男性"
money = row["money"]
print("姓:{},名:{},年龄:{},姓名:{},收入:{}".format(first_name,last_name,age,sex,money) )
# (3) 获取所有数据 fetchall
"""
data = cursor.fetchall()
print(data)
"""
# (4) 自定义搜索查询的位置
sql = "select * from t1 where id >= 20"
res = cursor.execute(sql)
print(res)
"""
# 1.相对滚动 (正数相对于当前位置往后滚,负数反之.)
cursor.scroll(3,mode="relative")
res = cursor.fetchone()
print(res)
cursor.scroll(3,mode="relative")
res = cursor.fetchone()
print(res)
# 27 往前滚
cursor.scroll(-2,mode="relative")
res = cursor.fetchone()
print(res)
"""
# 2.绝对滚动 , 永远基于第一条数据的位置进行移动
cursor.scroll(0,mode="absolute")
print(cursor.fetchone())
cursor.scroll(1,mode="absolute")
print(cursor.fetchone())
cursor.scroll(3,mode="absolute")
print(cursor.fetchone())
# 往前滚没有数据,超出范围error
"""
cursor.scroll(-1,mode="absolute")
print(cursor.fetchone())
"""
# 在进行增删改查时,必须提交数据,才会产生影响.
conn.commit()
cursor.close()
conn.close()
# innodb 在只有frm和ibd文件的情况下,如何恢复数据;
安装 MySQL Utilities
https://downloads.mysql.com/archives/utilities/
cmd中找到frm那个文件,执行如下命令:
切换到对应目录,执行下面语句,不要加分号
mysqlfrm --diagnostic ./文件目录/t1.frm
查出建表语句,复制查询出来的建表语句在mysql中创建的新数据中使用
#对已创建的表进行表空间卸载 删除ibd文件
mysql> alter table t1 discard tablespace;
把要恢复的idb文件替换进去
#对已创建的表进行空间装载
mysql> alter table t1 import tablespace;
CREATE TABLE `t1` (
`id` int(11) DEFAULT NULL,
`name` char(9) DEFAULT NULL
) ENGINE=InnoDB;