python 操作 mysql 增删改查

Posted on 2019-11-25 18:29  Max404  阅读(166)  评论(0编辑  收藏  举报

# ### python 操作 mysql 增删改查
import pymysql
"""
    python 操作mysql 默认开启事务,必须在增删改之后,
    提交数据,
    才会对数据库产生变化,否则默认回滚
    提交数据 conn.commit()
    回滚数据 conn.rollback()
    
    execute 执行sql 
    executemany 执行多条sql (插入时,可以使用)
"""
# 创建连接mysql
conn = pymysql.connect(
    host="127.0.0.1",
    user="root",
    password="123456",
    database="db7"
)
# 查询数据,默认返回的是元组,可以设置参数,返回字典 pymysql.cursors.DictCursor
cursor = conn.cursor(cursor = pymysql.cursors.DictCursor)


#
"""
sql = "insert into t1(first_name,last_name,age,sex,money) values(%s,%s,%s,%s,%s)"
# 一次插一条数据
# res = cursor.execute(sql , ("周","永陵",81,1,9.9) )
# print(res)
# 一次插入多条数据
res = cursor.executemany(sql, [ ("马","训",20,0,15000), ("常","远",90,0,10000) , ("李","德亮",18,0,8.8) ] )
print(res)

# 获取最后一条数据的id号 (针对于单条语句的执行,获取最后id)
print(cursor.lastrowid)
# 如果是执行多条数据executemany , 通过查询的方式获取
# select id from t1 order by id desc limit 1
"""

#
"""
sql = "delete from t1 where id = %s"
res = cursor.execute(sql,(5,))
print(res)

if res :
    print("删除成功")
else:
    print("删除失败")
"""

#
"""
sql = "update t1 set first_name= %s where id=%s"
res = cursor.execute(sql,("王二麻子",8))
print(res)

if res:
    print("更新成功")
else:
    print("更新失败")
"""

#
sql = "select * from t1" # 6~65
res = cursor.execute(sql)
print(res)

# (1) 获取一条数据 fetchone
res = cursor.fetchone()
print(res)#{'id': 6, 'first_name': '常', 'last_name': '远', 'age': 90, 'sex': 0, 'money': 10000.0}

# (2) 获取多条数据 fetchmany 默认搜索一条,上一次查询的数据,往下获取
data = cursor.fetchmany(3)
print(data)
"""
[
    {'id': 7, 'first_name': '李', 'last_name': '德亮', 'age': 18, 'sex': 0, 'money': 8.8}, 
    {'id': 8, 'first_name': '王二麻子', 'last_name': '永陵', 'age': 81, 'sex': 1, 'money': 9.9}, 
    {'id': 9, 'first_name': '马', 'last_name': '训', 'age': 20, 'sex': 0, 'money': 15000.0}
]
"""
for row  in data:
    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)

# 可以自定义查询的位置
print("<=================>")
sql = "select * from t1 where id >= 50"
res = cursor.execute(sql)
"""
# 1.相对滚动
# 先搜索一条 50
res = cursor.fetchone()
print(res)

# 再向后滚动3条 54
cursor.scroll(3,mode="relative")
res = cursor.fetchone()
print(res)

# 再向后滚动2条 56
cursor.scroll(2,mode="relative")
res = cursor.fetchone()
print(res)

# 在往前滚2条 error 下标越界
cursor.scroll(-30,mode="relative")
res = cursor.fetchone()
print(res)
"""
# 2.绝对滚动 相对于最开始第一条数据进行运算
cursor.scroll(0,mode="absolute")
print(cursor.fetchone())
cursor.scroll(3,mode="absolute")
print(cursor.fetchone())
cursor.scroll(5,mode="absolute")
print(cursor.fetchone())

# 在进行增删改的时候,必须替换数据,才真正进行修改,默认开启事务处理
conn.commit()
cursor.close()
conn.close()

 

# ### python 操作 mysql 增删改查
import pymysql
"""
    python 操作mysql 默认开启事务,必须在增删改之后,提交数据,
    才会对数据库产生变化,否则默认回滚
    提交数据 conn.commit()
    回滚数据 conn.rollback()
    
    execute 执行sql 
    executemany 执行多条sql (插入时,可以使用)
"""
# 创建连接mysql
conn = pymysql.connect(host="127.0.0.1",user="root",password="123456",database="db7")
# 查询数据,默认返回的是元组,可以设置参数,返回字典 pymysql.cursors.DictCursor
cursor = conn.cursor(cursor = pymysql.cursors.DictCursor)


#
"""
sql = "insert into t1(first_name,last_name,age,sex,money) values(%s,%s,%s,%s,%s)"
# 一次插一条数据
# res = cursor.execute(sql , ("周","永陵",81,1,9.9) )
# print(res)
# 一次插入多条数据
res = cursor.executemany(sql, [ ("马","训",20,0,15000), ("常","远",90,0,10000) , ("李","德亮",18,0,8.8) ] )
print(res)

# 获取最后一条数据的id号 (针对于单条语句的执行,获取最后id)
print(cursor.lastrowid)
# 如果是执行多条数据executemany , 通过查询的方式获取
# select id from t1 order by id desc limit 1
"""

#
"""
sql = "delete from t1 where id = %s"
res = cursor.execute(sql,(5,))
print(res)

if res :
    print("删除成功")
else:
    print("删除失败")
"""

#
"""
sql = "update t1 set first_name= %s where id=%s"
res = cursor.execute(sql,("王二麻子",8))
print(res)

if res:
    print("更新成功")
else:
    print("更新失败")
"""

#
sql = "select * from t1" # 6~65
res = cursor.execute(sql)
print(res)

# (1) 获取一条数据 fetchone
res = cursor.fetchone()
print(res)#{'id': 6, 'first_name': '常', 'last_name': '远', 'age': 90, 'sex': 0, 'money': 10000.0}

# (2) 获取多条数据 fetchmany 默认搜索一条,上一次查询的数据,往下获取
data = cursor.fetchmany(3)
print(data)
"""
[
    {'id': 7, 'first_name': '李', 'last_name': '德亮', 'age': 18, 'sex': 0, 'money': 8.8}, 
    {'id': 8, 'first_name': '王二麻子', 'last_name': '永陵', 'age': 81, 'sex': 1, 'money': 9.9}, 
    {'id': 9, 'first_name': '马', 'last_name': '训', 'age': 20, 'sex': 0, 'money': 15000.0}
]
"""
for row  in data:
    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)

# 可以自定义查询的位置
print("<=================>")
sql = "select * from t1 where id >= 50"
res = cursor.execute(sql)
"""
# 1.相对滚动
# 先搜索一条 50
res = cursor.fetchone()
print(res)

# 再向后滚动3条 54
cursor.scroll(3,mode="relative")
res = cursor.fetchone()
print(res)

# 再向后滚动2条 56
cursor.scroll(2,mode="relative")
res = cursor.fetchone()
print(res)

# 在往前滚2条 error 下标越界
cursor.scroll(-30,mode="relative")
res = cursor.fetchone()
print(res)
"""
# 2.绝对滚动 相对于最开始第一条数据进行运算
cursor.scroll(0,mode="absolute")
print(cursor.fetchone())
cursor.scroll(3,mode="absolute")
print(cursor.fetchone())
cursor.scroll(5,mode="absolute")
print(cursor.fetchone())

# 在进行增删改的时候,必须替换数据,才真正进行修改,默认开启事务处理
conn.commit()
cursor.close()
conn.close()