最近又開始從新學習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