温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python操作mysql数据库的方法介绍

发布时间:2020-05-28 16:52:42 来源:网络 阅读:273 作者:三月 栏目:数据库

下面讲讲关于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()方法,可以调整很多参数:

参数

描述

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/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

示例二、连接刚创建的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

对于以上python操作mysql数据库的方法相关内容,大家还有什么不明白的地方吗?或者想要了解更多相关,可以继续关注我们的行业资讯板块。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI