【python】mysql的操作

1. mysql的连接

操作步骤:
    导包  import pymysql
    数据库连接设置    conn=pymysql.connect(host,user,passwd,port,db,charset)
    生成游标     cur=conn.cursor(cursor=pymysql.cursors.DictCursor)
    编写sql语句  sql='select * from  student'
    执行sql语句  cur.excute(sql)
    获取数据   data=cur.fetchall()
    关闭游标   cur.close()
    关闭连接   conn.close()
# 数据库连接设置
conn = pymysql.connect(host="127.0.0.1", port=3306,
                user="root", password="123456",
                database="test",charset='utf8')
# 生成游标
cur = conn.cursor()

# 数据库进行操作
sql = 'select * from student'
res = cur.execute(sql)
# 一是为了验证连接是否成功,二是查看返还值到底是什么
print(res)  # 5

# 如何获取返回的数据
# 1-获取查询结果中所有的返回数据
all_data = cur.fetchall()
print(all_data)  #((1, 'zs', 18), (2, 'ls', 19), (3, 'ww', 18), (4, 'zl', 16), (5, 'jack', 17))

# 2-获取查询结果中1条返回数据
data = cur.fetchmany(3) #((1, 'zs', 18), (2, 'ls', 19), (3, 'ww', 18))
print(data)

one_data = cur.fetchone()
print(one_data) #(4, 'zl', 16)

# 3-获取查询结果中前3条返回数据
data = cur.fetchmany(3)
print(data)

# 游标的移动

# 关闭游标
cur.close()
# 关闭连接
conn.close()

2. 数据库的增删改查

# 数据库连接设置
conn = pymysql.connect(host="127.0.0.1", port=3306,
                user="root", password="123456",
                database="test",charset='utf8',
                autocommit=True)
# 生成游标
cur = conn.cursor()

# 数据库进行操作

# 增加操作
sql_insert = 'insert into student(name,age) values("五月天1",20);'
res = cur.execute(sql_insert)

# 一是为了验证连接是否成功,二是查看返还值到底是什么
print(res)  # 5
# python操作Mysql数据库时,默认是手动提交事务的方式
# 1:自己来写提交的sql
conn.commit()  #
# 2:设置Mysql是自动提交事务的这种方式  --推荐

# # 修改操作
sql_update = 'update student set name = "珏" where name="五月天1";'
res = cur.execute(sql_update)

# # 删除操作
sql_delete = 'delete from student where id =5;'
res = cur.execute(sql_delete)

# 执行多条sql语句
sql = 'insert into student(name,age) values(%s,%s);'
data = [("muzi1",20),("muzi2",20)]
cur.executemany(sql,data)

def fun(name,age):
    sql = 'insert into student(name,age) values("{}",{});'.format(name,age)
    cur.execute(sql)

fun('zs',18)
fun('lisa',20)

# 关闭游标
cur.close()
# 关闭连接
conn.close()

3. Mysql数据库的游标移动操作

移动光标,scroll(value, mode)方法
参数:
    当mode='relative'时,代表相对移动,默认值,value就是移动的长度,
    value>0向后移动(从位置0移动到位置2),value<0向前移动(比如从位置2移动到位置0)
    
    当mode='absolute'时,代表绝对移动,value就代表移动的绝对位置,
    value=0就代表移动到位置0处,就是结果集开头,value=3就是移动到位置3处,也就是第4条记录处。
# 数据库连接设置
conn = pymysql.connect(host="127.0.0.1", port=3306,
                user="root", password="123456",
                database="test",charset='utf8')
# 生成游标
cur = conn.cursor()

# 数据库进行操作
sql = 'select * from student'
res = cur.execute(sql)
# 一是为了验证连接是否成功,二是查看返还值到底是什么
print(res)  # 5

# 如何获取返回的数据
# 1-获取查询结果中所有的返回数据
all_data = cur.fetchall()
print(all_data)  #((1, 'zs', 18), (2, 'ls', 19), (3, 'ww', 18), (4, 'zl', 16), (5, 'jack', 17))

# # 2-获取查询结果中1条返回数据
data = cur.fetchmany(3) #((1, 'zs', 18), (2, 'ls', 19), (3, 'ww', 18))
print(data)

# 游标的移动
# 需求1:要求移动游标,获取一条数据,数据内容:(1, 'zs', 18)
# 绝对位置的移动
# cur.scroll(value=0, mode='absolute') #结果集的开头就是0

# 相对位置的移动
cur.scroll(value=-3, mode='relative')

one_data = cur.fetchone()
print(one_data) #(2, 'ls', 19)

# 获取倒数的3条记录
# 控制游标的移动
cur.scroll(value=res-3, mode='relative')

data = cur.fetchmany(3)
print(data)


# 关闭游标
cur.close()
# 关闭连接
conn.close()

 

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值