pymysql模块

来源:互联网 发布:kuka仿真软件 编辑:程序博客网 时间:2024/05/20 15:10

1)插入数据

  通过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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:lcj
import pymysql
#连接数据库
conn = pymysql.connect(host='192.168.1.152',port= 3306,user = 'root',passwd='123123',db='test'#db:库名
#创建游标
cur = conn.cursor()
#插入一条数据
# reCount = cur.excute('insert into lcj(name,age) vaules(%s,%s)',('ff',18))
#向test库中的lcj表插入
# ret = cur.executemany("insert into lcj(name,tel)values(%s,%s)", [("kk",13212344321),("kw",13245678906)])
#同时向数据库lcj表中插入多条数据
ret = cur.executemany("insert into lcj values(%s,%s,%s,%s,%s)", [(41,"xiaoluo41",'man',24,13212344332),
                                                              (42,"xiaoluo42",'gril',21,13245678948),
                                                              (43,"xiaoluo43",'gril',22,13245678949),
                                                                 (44,"xiaoluo44",'main',24,13543245648)])
#提交
conn.commit()
#关闭指针对象
cur.close()
#关闭连接对象
conn.close()
#打印结果
print(ret)

   2)查询数据

  在Pycharm控制台输出lcj表中数据

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
# !/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:lcj
import pymysql
#连接数据库
conn = pymysql.connect(host='192.168.1.152',port= 3306,user = 'root',passwd='123123',db='test'#db:库名
#创建游标
cur = conn.cursor()
#查询lcj表中存在的数据
cur.execute("select * from lcj")
#fetchall:获取lcj表中所有的数据
ret1 = cur.fetchall()
print(ret1)
print("----------------------")
#获取lcj表中前三行数据
ret2 = cur.fetchmany(3)
print(ret2)
print("------------------------------")
#获取lcj表中第一行数据
ret3= cur.fetchone()
print(ret3)
#同时向数据库lcj表中插入多条数据
# ret = cur.executemany("insert into lcj values(%s,%s,%s,%s,%s)", [(41,"xiaoluo41",'man',24,13212344332),
#                                                             (42,"xiaoluo42",'gril',21,13245678948),
#                                                             (43,"xiaoluo43",'gril',22,13245678949),
#                                                                (44,"xiaoluo44",'main',24,13543245648)])
#提交
conn.commit()
#关闭指针对象
cur.close()
#关闭连接对象
conn.close()

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

  • cursor.scroll(1,mode='relative')  # 相对当前位置移动【1:表示向下移动一行,-1:表示向上移动一行
  • cursor.scroll(2,mode='absolute') # 相对绝对位置移动 【1:表示向上移动一行,-1:表示向下移动一行

  3)删除数据  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# !/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:lcj
import pymysql
#连接数据库
conn = pymysql.connect(host='192.168.1.152',port= 3306,user = 'root',passwd='123123',db='test'#db:库名
#创建游标
cur = conn.cursor()
#删除cj表中数据
cur.execute("delete * from lcj")
#提交
conn.commit()
#关闭指针对象
cur.close()
#关闭连接对象
conn.close()

   4)修改表中的数据  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# !/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:lcj
import pymysql
#连接数据库
conn = pymysql.connect(host='192.168.1.152',port= 3306,user = 'root',passwd='123123',db='test'#db:库名
#创建游标
cur = conn.cursor()
#将lcj表中id=3的name 修改为lcjj
cur.execute("UPDATE lcj set name = 'lcjj' WHERE id = 3")  #逼表中所有的操作都可以再此进行操作
#提交
conn.commit()
#关闭指针对象
cur.close()
#关闭连接对象
conn.close()

   5)fetch数据类型

  关于默认获取的数据是元祖类型,如果想要或者字典类型的数据,即:  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# !/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:lcj
import pymysql
#连接数据库
conn = pymysql.connect(host='192.168.1.152',port= 3306,user = 'root',passwd='123123',db='test'#db:库名
#设置游标类型,默认游标类型为元祖形式
#将游标类型设置为字典形式
cur = conn.cursor(cursor=pymysql.cursors.DictCursor)
cur.execute("select * from lcj")  #逼表中所有的操作都可以再此进行操作
#将lcj表中所有数据以字典形式输出
ret = cur.fetchall()
print(ret)   #[{'age': 18, 'tel': '13520617734', 'name': 'xiaoluo', 'id': 1, 'sex': '?'},
#提交
conn.commit()
#关闭指针对象
cur.close()
#关闭连接对象
conn.close()
原创粉丝点击