python连接mysql之pymysql模块
以下demo均以python2中的mysqldb模块
一、插入数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
import MySQLdb conn = MySQLdb.connect(host = '127.0.0.1' ,user = 'root' ,passwd = '1234' ,db = 'mydb' ) cur = conn.cursor() reCount = cur.execute( 'insert into UserInfo(Name,Address) values(%s,%s)' ,( 'alex' , 'usa' )) # reCount = cur.execute('insert into UserInfo(Name,Address) values(%(id)s, %(name)s)',{'id':12345,'name':'wupeiqi'}) conn.commit() cur.close() conn.close() print reCount |
import MySQLdb conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='mydb') cur = conn.cursor() li =[
('alex','usa'),
('sb','usa'),
]
reCount = cur.executemany('insert into UserInfo(Name,Address) values(%s,%s)',li) conn.commit()
cur.close()
conn.close() print reCount 批量插入数据
批量插入数据
二、删除数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import MySQLdb conn = MySQLdb.connect(host = '127.0.0.1' ,user = 'root' ,passwd = '1234' ,db = 'mydb' ) cur = conn.cursor() reCount = cur.execute( 'delete from UserInfo' ) conn.commit() cur.close() conn.close() print reCount |
三、修改数据
1
2
3
4
5
6
7
8
9
10
11
12
13
|
import MySQLdb conn = MySQLdb.connect(host = '127.0.0.1' ,user = 'root' ,passwd = '1234' ,db = 'mydb' ) cur = conn.cursor() reCount = cur.execute( 'update UserInfo set Name = %s' ,( 'alin' ,)) conn.commit() cur.close() conn.close() print reCount |
四、查数据
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
|
# ############################## fetchone/fetchmany(num) ############################## import MySQLdb conn = MySQLdb.connect(host = '127.0.0.1' ,user = 'root' ,passwd = '1234' ,db = 'mydb' ) cur = conn.cursor() reCount = cur.execute( 'select * from UserInfo' ) print cur.fetchone() print cur.fetchone() cur.scroll( - 1 ,mode = 'relative' ) print cur.fetchone() print cur.fetchone() cur.scroll( 0 ,mode = 'absolute' ) print cur.fetchone() print cur.fetchone() cur.close() conn.close() print reCount # ############################## fetchall ############################## import MySQLdb conn = MySQLdb.connect(host = '127.0.0.1' ,user = 'root' ,passwd = '1234' ,db = 'mydb' ) #cur = conn.cursor(cursorclass = MySQLdb.cursors.DictCursor) cur = conn.cursor() reCount = cur.execute( 'select Name,Address from UserInfo' ) nRet = cur.fetchall() cur.close() conn.close() print reCount print nRet for i in nRet: print i[ 0 ],i[ 1 ] |
python连接mysql之pymysql模块的更多相关文章
- 使用python连接mysql数据库——pymysql模块的使用
安装pymysql pip install pymysql 使用pymysql 使用数据查询语句 查询一条数据fetchone() from pymysql import * conn = conne ...
- Python操作MySQL:pymysql模块
连接MySQL有两个模块:mysqldb和pymysql,第一个在Python3.x上不能用,所以我们学pymysql import pymysql # 创建连接 conn = pymysql.con ...
- Python连接MySQL数据库(pymysql的使用)
本文Python版本3.5.3,mysq版本5.7.23 基本使用 # 导入pymysql模块 import pymysql #连接数据库 conn = pymysql.connect( databa ...
- python操作MySQL之pymysql模块
import pymysql#pip install pymysql db=pymysql.connect(','day040') cursor=db.cursor() #创建游标 book_list ...
- pymysql模块使用---Python连接MySQL数据库
pymysql模块使用---Python连接MySQL数据库 浏览目录 pymysql介绍 连接数据库 execute( ) 之 sql 注入 增删改查操作 进阶用法 一.pymysql介绍 1.介绍 ...
- Python中操作mysql的pymysql模块详解
Python中操作mysql的pymysql模块详解 前言 pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持 ...
- Python 使用PyMySql 库 连接MySql数据库时 查询中文遇到的乱码问题(实测可行) python 连接 MySql 中文乱码 pymysql库
最近所写的代码中需要用到python去连接MySql数据库,因为是用PyQt5来构建的GUI,原本打算使用PyQt5中的数据库连接方法,后来虽然能够正确连接上发现还是不能提交修改内容,最后在qq交流群 ...
- 用python连接mysql失败总结
所用环境:python3,pycharm2018.2.4 先用mysql创建用户并授予相关权限 在我用python连接mysql时,一直提示连接不上,报错原因就是,用户没有被给予相关权限,比如查询,插 ...
- 第二百七十九节,MySQL数据库-pymysql模块操作数据库
MySQL数据库-pymysql模块操作数据库 pymysql模块是python操作数据库的一个模块 connect()创建数据库链接,参数是连接数据库需要的连接参数使用方式: 模块名称.connec ...
随机推荐
- zoj 2734 Exchange Cards【dfs+剪枝】
Exchange Cards Time Limit: 2 Seconds Memory Limit: 65536 KB As a basketball fan, Mike is also f ...
- appium api
AppiumDriver getAppStrings() 默认系统语言对应的Strings.xml文件内的数据.iOS driver.getAppStrings(Stringlanguage ...
- Struts2配置文件讲解
解决在断网环境下,配置文件无提示的问题我们可以看到Struts.xml在断网的情况下,前面有一个叹号,这时,我们按alt+/ 没有提示,这是因为” http://struts.apache.org/d ...
- CSS3弹性盒模型之Flexbox是布局模块box-sizing & box-orient & box-direction & box-ordinal-group
css3 box-sizing属性 box-sizing属性可以为三个值之一:content-box(default),border-box,padding-box. content-box,bord ...
- oracle恢复被覆盖的存储过程
假设你不小心覆盖了之前的存储过程,那得赶紧闪回,时长越长闪回的可能性越小.原理非常easy,存储过程的定义就是数据字典,改动数据字典跟改动普通表的数据没有差别,此时会把改动前的内容放到undo中,我们 ...
- myEclipse新建jsp,默认编码
修改地方在: myeclipse →fiter and editor →jsp
- ASP.NET Core和ASP.NET Framework共享Identity身份验证
.NET Core 已经热了好一阵子,1.1版本发布后其可用性也越来越高,开源.组件化.跨平台.性能优秀.社区活跃等等标签再加上"微软爸爸"主推和大力支持,尽管现阶段对比.net ...
- C# WinForm获取程序所在路径方法
多个获取WinForm程序所在文件夹路径的方法,收藏备忘. 1)获取当前进程的完整路径,包含文件名(进程名). 代码:string str =this.GetType().Assembly.Locat ...
- PHP错误类型及屏蔽方法
1. 注意(Notices)这些都是比较小而且不严重的错误,比如去访问一个未被定义的变量.通常,这类的错误是不提示给用户的,但有时这些错误会影响到运行的结果. 2. 警告(Warnings)这就是稍微 ...
- 权限系统与RBAC模型概述
为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/3793894.html ...