千家信息网

如何使用pyMySql连接mysql

发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,本篇内容介绍了"如何使用pyMySql连接mysql"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!安
千家信息网最后更新 2025年11月07日如何使用pyMySql连接mysql

本篇内容介绍了"如何使用pyMySql连接mysql"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

安装

pip3 install pymysql

常用函数:

execute() 执行语句并返回收影响的行数

import pymysql


# 创建连接

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='test',charset="utf8" )

# 创建游标

cursor = conn.cursor()


# 执行SQL,并返回收影响行数

effect_row = cursor.execute("insert into t2 VALUES(1,'ray')")


# 执行SQL,并返回受影响行数

# effect_row = cursor.execute("update hosts set host = '1.1.1.2' where nid > %s", (1,))


# 执行SQL,并返回受影响行数

# effect_row = cursor.executemany("insert into hosts(host,color_id)values(%s,%s)", [("1.1.1.11",1),("1.1.1.11",2)])



# 提交,不然无法保存新建或者修改的数据

conn.commit()


# 关闭游标

cursor.close()

# 关闭连接

conn.close()

import pymysql


# 创建连接

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='test',charset="utf8" )

# 创建游标

cursor = conn.cursor()


# 执行SQL,并返回收影响行数

truple_str='ray'

effect_row = cursor.execute("insert into t2 VALUES(3,%s)",truple_str)

print(effect_row)


truple_str='suen'

effect_row = cursor.execute("insert into t2 VALUES(4,%s)",truple_str)

print(effect_row)


# 提交,不然无法保存新建或者修改的数据

conn.commit()


# 关闭游标

cursor.close()

# 关闭连接

conn.close()

import pymysql


# 创建连接

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='test',charset="utf8" )

# 创建游标

cursor = conn.cursor()


# 执行SQL,并返回收影响行数

truple_str=('lalala',1)


cursor.execute('update t2 set t_name=%s where t_id=%s',truple_str)

# 提交,不然无法保存新建或者修改的数据

conn.commit()


# 关闭游标

cursor.close()

# 关闭连接

conn.close()

import pymysql


# 创建连接

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='test',charset="utf8" )

# 创建游标

cursor = conn.cursor()


# 执行SQL,并返回收影响行数

# truple_str=('lalala',1)


tid=1

cursor.execute('delete from t2 where t_id=%s',tid)

# 提交,不然无法保存新建或者修改的数据

conn.commit()


# 关闭游标

cursor.close()

# 关闭连接

conn.close()

executemany()执行条语句,以元组的形式传入

import pymysql


# 创建连接

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='test',charset="utf8" )

# 创建游标

cursor = conn.cursor()


# 执行SQL,并返回收影响行数

truple_str=[

(5,'aaa'),

(6,'bbb'),

(7,'ccc'),

(8,'ddd')

]


cursor.executemany("insert into t2 VALUES(%s,%s)",truple_str)

# 提交,不然无法保存新建或者修改的数据

conn.commit()


# 关闭游标

cursor.close()

# 关闭连接

conn.close()

fecheone()

fechemany()

fecheall()

注:在fetch数据时按照顺序进行,可以使用cursor.scroll(num,mode)来移动游标位置,如:

  • cursor.scroll(1,mode='relative') # 相对当前位置移动,正数为向下移动,负数为向上移动

  • cursor.scroll(2,mode='absolute') # 相对绝对位置移动

import pymysql


# 创建连接

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='test',charset="utf8" )

# 创建游标

cursor = conn.cursor()


# 执行SQL,并返回收影响行数

# truple_str=('lalala',1)


cursor.execute('select * from t2')



#fech 是去内存中获取

res = cursor.fetchone()

print(res)


res = cursor.fetchmany(7) #指定获取的条目数

print(res)


res = cursor.fetchall()

print(res)



# 关闭游标

cursor.close()

# 关闭连接

conn.close()

import pymysql


# 创建连接

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='test',charset="utf8" )

# 创建游标

cursor = conn.cursor()


# 执行SQL,并返回收影响行数

# truple_str=('lalala',1)


cursor.execute('select * from t2')



#fech 是去内存中获取

res = cursor.fetchone()

print(res)


res = cursor.fetchone()

print(res)


cursor.scroll(0,mode='absolute')

res = cursor.fetchall()

print(res)


cursor.scroll(-1,mode='relative')

res = cursor.fetchall()

print(res)



# 关闭游标

cursor.close()

# 关闭连接

conn.close()

import pymysql


# 创建连接

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='test',charset="utf8" )

# 创建游标

cursor = conn.cursor()


# 执行SQL,并返回收影响行数

# truple_str=('lalala',1)


cursor.execute('select * from t2')



#fech 是去内存中获取

while 1:

res = cursor.fetchone()

if res == None:

break

print(res)




# 关闭游标

cursor.close()

# 关闭连接

conn.close()

cursor修改,改变获取后的结果为字典集合

import pymysql


# 创建连接

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='test',charset="utf8" )

# 创建游标

cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)


# 执行SQL,并返回收影响行数

# truple_str=('lalala',1)


cursor.execute('select * from t2')



#fech 是去内存中获取

while 1:

res = cursor.fetchone()

if res == None:

break

print(res)


# 关闭游标

cursor.close()

# 关闭连接

conn.close()

lastrowid 获取最后的自增序列号

import pymysql


# 创建连接

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='test',charset="utf8" )

# 创建游标

cursor = conn.cursor()


cursor.executemany('insert into t3(t_name) VALUES (%s)',[('aaa07'),('aaa08'),('aaa09')])


conn.commit()


tid=cursor.lastrowid

print(tid)


# 关闭游标

cursor.close()

# 关闭连接

conn.close()

"如何使用pyMySql连接mysql"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

游标 影响 数据 移动 内存 位置 内容 更多 知识 语句 实用 学有所成 接下来 函数 困境 字典 实际 常用 序列 序列号 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 网络安全检查组织开展情况 汽车车载网络技术付百学 我的世界网易服务器海岛生存 我的世界蓝天服务器 网络安全知识图集 长沙民政学院网络技术 上海正规软件开发服务费 泰拉瑞亚手游服务器招聘 穿越火线哪个服务器可以加好友 研究网络安全防范的意义 打开数据库的指令是什么原因 数据库技术发展阶段的是 服务器设备维保方案pdf 操作系统的数据库怎么样 松江区品牌软件开发销售方法 我的世界ice服务器主人是谁 天长工业软件开发技术怎么样 希沃软件开发者 黄服务器网站 上海软件开发代理做账 计算机应用网络技术自考 二战中的数据库 网络安全交流讨论军人 rust服务器里面不能打字 进销存管理系统数据库设计目的 服务器设置多个管理口 lol各个服务器在什么城市 网络安全管理追责 协创网络技术怎么样 易语言取出数据库最新的数据
0