下面讲讲关于python操作mysql数据库的方法,文字的奥妙在于贴近主题相关。所以,闲话就不谈了,我们直接看下文吧,相信看完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的,只需import MySQLdb修改为,import PyMySQL,就可以了。
二,PyMySQL连接数据库的方法以及参数介绍:
,,,,pymysql连接mysql使用pymysql.connect()方法,可以调整很多参数:
参数
描述
主机数据库地址用户数据库用户名,passwd数据库密码,默认为空分贝数据库库名,没有默认库端口数据库端口,默认3306 connect_timeout连接超时时间,秒为单位use_unicode结果以unicode字符串返回字符集插入数据库编码连接示例:
连接=pymysql.connect(主机=?92.168.186.157"、港口=3306,用户=皐inner", passwd=?23123“, db=癉B", charset=皍tf8", connect_timeout=3000) 示例连接主要包含主机、用户、passwrd以及港口等参数 连接示例2: 连接=pymysql.connect (“192.168.186.157",“winner",“123123”、“test") 不用加主机等参数,但是格式固定,分别是主机,,用户,,密码以及初始连接的数据库不能互换位置, 而上面的带参数的示例相对来说更随意一些。 注意:这里的端口以及连接超时时间都是int,所以不需要带引号
连接对象返回的connect()函数:
commit()提交事务。对支持事务的数据库和表,如果提交修改操作,不适用这个方法,则不会写到数据库中回滚()事务回滚。对支持事务的数据库和表,如果执行此方法,则回滚当前事务。在没有提交()前提下.cursor ([cursorclass])创建一个游标对象。所有的sql语句的执行都要在游标对象下进行.MySQL本身不支持游标,MySQLdb模块对其游标进行了仿真。,在python操作mysql数据库的过程中,我们主要是使用获取游标方法counect.cursor()和cursor.execute()方法对数据库进行操作,像创建数据库以及数据表等操作,我们一般直接在mysql客户端连接,执行sql语句就可以了,所以我们更多的操作就是增、删、改、查等操作
游标对象也提供了几种方法:
关闭()关闭游标执行(sql)执行sql语句excutemany (sql)执行多条sql语句fetchone()从执行结果中取第一条记录fetchmany (n)从执行结果中取n条记录fetchall()从执行结果中取所有记录滚动(自我、价值模式=& # 39;相对# 39;)游标滚动示例一,连接192.168.186.157的mysql服务端创建pymysql库字符集为utf8
#/usr/bin/env python # _ * _coding: utf-8_ * _ #导入pymysql模块 import pymysql #使用pymysql.connect()方法创建数据库链接 反对=pymysql.connect(主机=& # 39;192.168.186.157& # 39;,用户=& # 39;冠军# 39;,passwd=& # 39; 123123 & # 39;,端口=3306) #使用con.cursor()方法创建游标 光标=con.cursor () sql=?,, create , database , If Not Exists ,, pymysql default character set use utf8;“ & # 39;& # 39;& # 39;sql=啊啊癱reate table if not exists class (int id (10), primary key auto_increment, ,name varchar (20), not null address varchar (20), not null default “gansu")“““ & # 39;& # 39;& # 39; cursor.execute (sql) cursor.execute (“show databases") dataname=cursor.fetchall () 打印(dataname)
执行结果:
<>之前((& # 39;information_schema& # 39;,),, (& # 39; # mysql50 # 2017 - 03 - 16 _09 - 38 - 47 - & # 39;,),, (& # 39; db # 39;,),, (& # 39; mysql # 39;,),, (& # 39; performance_schema& # 39;,), ,(& # 39;pymysql& # 39;,),,(& # 39;测试# 39;,),,(& # 39;winner_mas& # 39;,)) ,Process finished  with exit  code 0示例二,连接刚创建的pymysql数据库创建类表
#/usr/bin/env python # _ * _coding: utf-8_ * _ #导入pymysql模块 import pymysql #使用pymysql.connect()方法创建数据库链接 反对=pymysql.connect(主机=& # 39;192.168.186.157& # 39;,用户=& # 39;冠军# 39;,passwd=& # 39; 123123 & # 39;,端口=3306,db=& # 39; pymysql& # 39;) #使用con.cursor()方法创建游标 光标=con.cursor () # sql=?, create , database , If Not Exists ,, pymysql default character set use utf8;“ sql=啊啊癱reate  table if not exists class (int id (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 () 打印(dataname)python操作mysql数据库的方法介绍