最近又开始从新学习python,研究了一些python操做mysql数据库的知识,记录在此,用做学习笔记,python
基础环境:Python 3.5.1mysql
mysql版本:5.6.35 (rpm安装方式)sql
操做系统:Centos7.3 和windows7数据库
1、python链接数据库模块介绍:windows
目前主要用的有如下几种、MySQLdb和pymsql以及mysql官方提供的mysql-connector-python驱动,MySQLdb模块是python2.X使用比较多的,而python3.X使用的pymsql会更多一点,之后再研究官方的mysql-connector-python,本次学习以及实践所有基于pymsql模块。bash
PyMySQL的使用方法和MySQLdb几乎同样,习惯用MySQLdb的,只需 import MySQLdb 修改成 import pymysql 就能够了。ide
2、pymysql链接数据库的方法以及参数介绍:函数
pymysql链接mysql 使用pymysql.connect()方法,能够调整不少参数:学习
参数fetch |
描述 |
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') | 游标滚动 |
示例1、链接192.168.186.157的mysql服务端建立pymysql库字符集为utf8
#/usr/bin/env python #_*_coding:utf-8_*_ #导入pymysql模块 import pymysql #使用pymysql.connect()方法建立数据库连接 con=pymysql.connect(host='192.168.186.157',user='winner',passwd='123123',port=3306) #使用con.cursor()方法建立游标 cursor=con.cursor() sql=" create database If Not Exists pymysql default character set utf8;" '''sql="""create table if not exists class (id int(10) primary key auto_increment, name varchar(20) not null ,address varchar(20) not null default "gansu")""" ''' cursor.execute(sql) cursor.execute("show databases") dataname=cursor.fetchall() print(dataname)
执行结果:
(('information_schema',), ('#mysql50#2017-03-16_09-38-47',), ('DB',), ('mysql',), ('performance_schema',), ('pymysql',), ('test',), ('winner_mas',)) Process finished with exit code 0
示例2、链接刚建立的pymysql数据库建立class表
#/usr/bin/env python #_*_coding:utf-8_*_ #导入pymysql模块 import pymysql #使用pymysql.connect()方法建立数据库连接 con=pymysql.connect(host='192.168.186.157',user='winner',passwd='123123',port=3306,db='pymysql') #使用con.cursor()方法建立游标 cursor=con.cursor() #sql=" create database If Not Exists pymysql default character set utf8;" sql="""create table if not exists class (id int(10) primary key auto_increment, name varchar(20) not null ,address varchar(20) not null default "gansu")""" cursor.execute(sql) cursor.execute("show tables") dataname=cursor.fetchall() print(dataname)
C:\Users\Administrator\AppData\Local\Programs\Python\Python35\python.exe C:/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' already exists") result = self._query(query) Process finished with exit code 0