python操作mysql数据库(一),pythonmysql,最近又开始重新学习p


最近又开始重新学习python,研究了一些python操作mysql数据库的知识,记录在此,用作学习笔记,

基础环境:Python 3.5.1

mysql版本:5.6.35 (rpm安装方式)

操作系统:Centos7.3 和windows7

一、python连接数据库模块介绍:

目前主要用的有以下几种、MySQLdb和pymsql以及mysql官方提供的mysql-connector-python驱动,MySQLdb模块是python2.X使用比较多的,而python3.X使用的pymsql会更多一点,以后再研究官方的mysql-connector-python,本次学习以及实践全部基于pymsql模块。

PyMySQL的使用方法和MySQLdb几乎一样,习惯用MySQLdb的,只需 importMySQLdb 修改为importpymysql 就可以了。

二、pymysql连接数据库的方法以及参数介绍:

pymysql连接mysql 使用pymysql.connect()方法,可以调整很多参数:

参数

描述

host数据库地址
user数据库用户名,
passwd数据库密码,默认为空
db数据库库名,没有默认库
port数据库端口,默认3306
connect_timeout连接超时时间,秒为单位
use_unicode结果以unicode字符串返回
charset插入数据库编码

连接示例:

connect=pymysql.connect(host="192.168.186.157",port=3306,user="winner",passwd="123123",db="DB",charset="utf8",connect_timeout=3000)示例连接主要包含host,user,passwrd以及port等参数连接示例2:connect=pymysql.connect("192.168.186.157","winner","123123","test")不用加host等参数,但是格式固定,分别是主机、用户、密码以及初始连接的数据库不能互换位置,而上面的带参数的示例相对来说更随意一些。注意:这里的端口以及连接超时时间都是int,所以不需要带引号

连接对象返回的connect()函数:

commit()提交事务。对支持事务的数据库和表,如果提交修改操作,不适用这个方法,则不会写到数据库中
rollback()事务回滚。对支持事务的数据库和表,如果执行此方法,则回滚当前事务。在没有commit()前提下。
cursor([cursorclass])创建一个游标对象。所有的sql语句的执行都要在游标对象下进行。MySQL本身不支持游标,MySQLdb模块对其游标进行了仿真。

在python操作mysql数据库的过程中,我们主要是使用获取游标方法counect.cursor()和cursor.execute()方法对数据库进行操作,像创建数据库以及数据表等操作,我们一般直接在mysql客户端连接,执行SQL语句就可以了,所以我们更多的操作就是增、删、改、查等操作

游标对象也提供了几种方法:

close()关闭游标
execute(sql)执行sql语句
excutemany(sql)执行多条sql语句
fetchone()从执行结果中取第一条记录
fetchmany(n)从执行结果中取n条记录
fetchall()从执行结果中取所有记录
scroll(self, value, mode=‘relative‘)游标滚动

示例一、连接192.168.186.157的mysql服务端创建pymysql库字符集为utf8

#/usr/bin/envpython#_*_coding:utf-8_*_#导入pymysql模块importpymysql#使用pymysql.connect()方法创建数据库链接con=pymysql.connect(host=‘192.168.186.157‘,user=‘winner‘,passwd=‘123123‘,port=3306)#使用con.cursor()方法创建游标cursor=con.cursor()sql="createdatabaseIfNotExistspymysqldefaultcharactersetutf8;"‘‘‘sql="""createtableifnotexistsclass(idint(10)primarykeyauto_increment,namevarchar(20)notnull,addressvarchar(20)notnulldefault"gansu")"""‘‘‘cursor.execute(sql)cursor.execute("showdatabases")dataname=cursor.fetchall()print(dataname)

执行结果:

((‘information_schema‘,),(‘#mysql50#2017-03-16_09-38-47‘,),(‘DB‘,),(‘mysql‘,),(‘performance_schema‘,),(‘pymysql‘,),(‘test‘,),(‘winner_mas‘,))Processfinishedwithexitcode0

示例二、连接刚创建的pymysql数据库创建class表

#/usr/bin/envpython#_*_coding:utf-8_*_#导入pymysql模块importpymysql#使用pymysql.connect()方法创建数据库链接con=pymysql.connect(host=‘192.168.186.157‘,user=‘winner‘,passwd=‘123123‘,port=3306,db=‘pymysql‘)#使用con.cursor()方法创建游标cursor=con.cursor()#sql="createdatabaseIfNotExistspymysqldefaultcharactersetutf8;"sql="""createtableifnotexistsclass(idint(10)primarykeyauto_increment,namevarchar(20)notnull,addressvarchar(20)notnulldefault"gansu")"""cursor.execute(sql)cursor.execute("showtables")dataname=cursor.fetchall()print(dataname)

C:\Users\Administrator\AppData\Local\Programs\Python\Python35\python.exeC:/Users/Administrator/PycharmProjects/python/createdatabase.py((‘class‘,),)C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\pymysql\cursors.py:166:Warning:(1050,"Table‘class‘alreadyexists")result=self._query(query)Processfinishedwithexitcode0


python操作mysql数据库(一)

评论关闭