pymysql的使用

pymysql的使用

 

安装

使用pip安装

1
pip install pymysql

  

pycham模块安装

1
[File] >> [settings] >> [Project: python] >> [Project Interpreter] >> [Install按钮]

  

 

pymysql的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Python统一了数据库连接的接口,所以 pymysql 和 MySQLdb 在使用方式上是类似的:
 
pymysql.Connect()参数说明
host(str):      MySQL服务器地址
port(int):      MySQL服务器端口号
user(str):      用户名
passwd(str):    密码
db(str):        数据库名称
charset(str):   连接编码
 
connection对象支持的方法
cursor()        使用该连接创建并返回游标
commit()        提交当前事务
rollback()      回滚当前事务
close()         关闭连接
 
cursor对象支持的方法
execute(op)     执行一个数据库的查询命令
fetchone()      取得结果集的下一行
fetchmany(size) 获取结果集的下几行
fetchall()      获取结果集中的所有行
rowcount()      返回数据条数或影响行数
close()         关闭游标对象

  

 

pymysql的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 导入pymysql模块
import pymysql
 
# 连接database
conn = pymysql.connect(
    host="123.207.251.121",
    user="root",
    password="159357",
    db ="test",
    charset ="utf8")
 
# 得到一个可以执行SQL语句的光标对象
cursor = conn.cursor()  # 执行完毕返回的结果集默认以元组显示
# 得到一个可以执行SQL语句并且将结果作为字典返回的游标
# cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
 
# 定义要执行的SQL语句
sql = "select version()"
 
# 执行SQL语句
cursor.execute(sql)
 
# #使用fetchone()方法获取一条数据
data = cursor.fetchone()
print("Mysql version : %s"%data)
 
 
#插入数据
sql = "INSERT INTO account (id, name, balance) VALUES ( '%d', '%s', %.2f )"
data = (3, 'xinyi', 5000)
cursor.execute(sql % data)
conn.commit()
print('成功插入', cursor.rowcount, '条数据')
 
 
# 修改数据
sql = "UPDATE account SET balance = %.2f WHERE name = '%s' "
data = (8888, 'lili')
cursor.execute(sql % data)
conn.commit()
print('成功修改', cursor.rowcount, '条数据')
 
 
# 删除数据
sql = "DELETE FROM account WHERE name = '%s' "
data = ("alex")
cursor.execute(sql % data)
conn.commit()
print('成功删除', cursor.rowcount, '条数据')
 
 
# 查询数据
sql = "SELECT name,balance FROM account "
cursor.execute(sql)
for row in cursor.fetchall():
    print("Name:%s\tSaving:%.2f" % row)
print('共查找出', cursor.rowcount, '条数据')
 
# 游标的移动
# 回滚
# 相对位置
sql = "SELECT name,balance FROM account "
cursor.execute(sql)
print(cursor.fetchone())
print(cursor.fetchone())
cursor.scroll(-1,mode="relative")
print(cursor.fetchone())
 
 
# 绝对位置
sql = "SELECT name,balance FROM account "
cursor.execute(sql)
print(cursor.fetchone())
print(cursor.fetchone())
cursor.scroll(0,mode="absolute")
print(cursor.fetchone())
 
 
# 关闭光标对象
cursor.close()
 
# 关闭数据库连接
conn.close()

 

 

演示结果

 原始数据:

 

插入:

 

修改:

 

删除:

 

查询:

 

游标移动:

相对位置:

1
2
3
4
Mysql version : 5.5.60-MariaDB
('lili', 8888.0)
('xinyi', 5000.0)
('xinyi', 5000.0)

  

绝对位置:

1
2
3
4
Mysql version : 5.5.60-MariaDB
('lili', 8888.0)
('xinyi', 5000.0)
('lili', 8888.0)

  

  

 

posted @   -零  阅读(350)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示