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=‘1234‘,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]

    

  

时间: 2024-07-30 11:24:50

python连接mysql之pymysql模块的相关文章

使用python连接mysql数据库——pymysql模块的使用

安装pymysql pip install pymysql 使用pymysql 使用数据查询语句 查询一条数据fetchone() from pymysql import * conn = connect( host='127.0.0.1', port=3306, user='root', password='123456', database='itcast', charset='utf8') # 创建游标 c = conn.cursor() # 执行sql语句 c.execute("sele

Python操作MySQL:pymysql模块

连接MySQL有两个模块:mysqldb和pymysql,第一个在Python3.x上不能用,所以我们学pymysql import pymysql # 创建连接 conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='mysql8', db='mysqltest') # 创建游标(光标) cursor = conn.cursor() # 执行SQL,返回影响行数 effect_rows = curso

Python连接MySQL数据库(pymysql的使用)

本文Python版本3.5.3,mysq版本5.7.23 基本使用 # 导入pymysql模块 import pymysql #连接数据库 conn = pymysql.connect( database="数据库名", user="用户名", password="密码", host="数据的地址, port=3306, # 端口3306是固定的 charset="utf8") # 只能是utf8,千万不能是utf-

Python连接MySQL数据库之pymysql模块使用

Python连接MySQL数据库之pymysql模块使用 Python3连接MySQL PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb. Django中也可以使用PyMySQL连接MySQL数据库. PyMySQL安装 pip install pymysql 连接数据库 注意事项 在进行本文以下内容之前需要注意: 你有一个MySQL数据库,并且已经启动. 你有可以连接该数据库的用户名和密码 你有一个有权限操作的datab

【转】Python中操作mysql的pymysql模块详解

Python中操作mysql的pymysql模块详解 前言 pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持3.x版本. 本文测试python版本:2.7.11.mysql版本:5.6.24 一.安装 1 pip3 install pymysql 二.使用操作 1.执行SQL 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

Python中操作mysql的pymysql模块详解

Python中操作mysql的pymysql模块详解 前言 pymsql是Python中操作MySQL的模块,pymysql支持python3.x. 一.安装 pip install pymysql 二.使用操作 1.执行SQL #!/usr/bin/env pytho # -*- coding:utf-8 -*- importpymysql # 创建连接 conn =pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd

第二百七十九节,MySQL数据库-pymysql模块

MySQL数据库-pymysql模块 pymysql模块是python操作数据库的一个模块 connect()创建数据库链接,参数是连接数据库需要的连接参数使用方式: 模块名称.connect() 参数: host=数据库ip port=数据库端口 user=数据库用户名 passwd=数据库密码 db=数据库名称 cursor()创建数据库操作游标,无参使用方式: 游标变量.cursor() execute()操作数据库,参数1 sql语句,参数2 字符串占位符变量使用方式: 游标变量.exe

python连接mysql的操作

一,安装mysql 如果是windows 用户,mysql 的安装非常简单,直接下载安装文件,双击安装文件一步一步进行操作即可. Linux 下的安装可能会更加简单,除了下载安装包进行安装外,一般的linux 仓库中都会有mysql ,我们只需要通过一个命令就可以下载安装: Ubuntu\deepin >>sudo apt-get install mysql-server >>Sudo apt-get install  mysql-client centOS/redhat >

python 连接mysql多层结构实例

一.python 连接mysql多层结构: 目录文件介绍: sqlexec             sql执行操作,例:增删改查 table                 数据库表模块 一个表一个模块 index.py            主文件 conf.py              数据库连接信息 目录结构如下: -rw-r--r-- 1 root root     167 Jun 22 20:36 conf.py-rw-r--r-- 1 root root     244 Jun 2