本文鏈接:http://www.reibang.com/p/50ba66cd335a
作者:西瓜甜
一、基本介紹
Python3 操作 MySQL 數(shù)據(jù)庫 可以使用的模塊是 pymysql
和 MySQLdb
佃迄。
這個(gè)兩個(gè)模塊都是通過自己的 API 執(zhí)行原生的 SQL 語句實(shí)現(xiàn)的夫否。
MySQLdb 是最早出現(xiàn)的一個(gè)操作 MySQL 數(shù)據(jù)庫的模塊膊毁,核心由C語言編寫,接口精煉,性能最棒惰帽,缺點(diǎn)是環(huán)境依賴較多,安裝稍復(fù)雜父虑,特別是 windows 中不好安裝该酗。更新較慢。
pymysql
為替代 MySQLdb
而生,純 Python
實(shí)現(xiàn)呜魄,API 的接口與 MySQLdb
完全兼容悔叽,安裝方便。
二爵嗅、 安裝包 pymysql
1 方案一 pymsql
pymsql是Python中操作MySQL的模塊
shell> pip3 install pymysql
基本使用
import pymysql
# 獲取連接到 DB 的對(duì)象
db = pymysql.connect(host='172.16.153.10',
port=3306,
user='root',
passwd='123',
db='shark_db',
# 獲取游標(biāo)娇澎,之后使用游標(biāo)操作表
cursor = conn.cursor()
2 方案2 mysqlclient
安裝 mysqlclient
適用于 Centos7
安裝系統(tǒng)依賴庫和編譯器
yum install mariadb-devel gcc
- 如果是系統(tǒng)自帶的 python2環(huán)境,安裝 Python2 依賴庫
python-devel
- 如果是 python3環(huán)境睹晒,編譯安裝 python3 后就不用再操作了
安裝 mysqlclient
pip install mysqlclient
基本使用
import MySQLdb
# 獲取連接到 DB 的對(duì)象
db=MySQLdb.connect(host='172.16.153.10',passwd="moonpie",db="thangs")
# 獲取游標(biāo)趟庄,之后使用游標(biāo)操作表
c=db.cursor()
三、 基本操作
1 創(chuàng)建表
import pymysql
# 創(chuàng)建連接
conn = pymysql.connect(host='172.16.153.10',
port=3306,
user='root',
passwd='123',
db='shark_db',
charset='utf8mb4')
# 獲取游標(biāo)對(duì)象
cursor = conn.cursor()
# 定義 sql 語句
create_table_sql = """create table t1
(id int auto_increment primary key,
name varchar(10) not null,
age int not null)"""
# 執(zhí)行 sql 語句
cursor.execute(create_table_sql)
# 提交更改
conn.commit()
# 關(guān)閉游標(biāo)對(duì)象
cursor.close()
# 關(guān)閉連接對(duì)象
conn.close()
2 插入數(shù)據(jù)
一次插入一條數(shù)據(jù)
一次插入一條數(shù)據(jù), 并且使用變量占位符
insert_data_sql = "insert into t1(name, age) values(%s, %s);"
row = cursor.execute(insert_data_sql, ('shark', 18))
conn.commit()
cursor.close()
conn.close()
一次插入多條數(shù)據(jù)
定義插入數(shù)據(jù)的語句
many_sql = "insert into t1 (name, age) values(%s, %s)"
一次插入多條數(shù)據(jù)
row = cursor.executemany(many_sql, [('shark1', 18),('xiguatian', 20),('qf', 8)])
conn.commit()
cursor.close()
conn.close()
3 查詢數(shù)據(jù)
3.1 獲取到的數(shù)據(jù)是元組類型
定義一個(gè)查詢語句
query_sql = "select id,name,age from t1 where name=%s;"
執(zhí)行查詢語句伪很,并且返回得到結(jié)果的行數(shù)
row_nums = cursor.execute(query_sql, ('shark1'))
"""
獲取到數(shù)據(jù)結(jié)果集具有迭代器的特性:
1. 可以通過索引取值戚啥,可以切片
2. 結(jié)果集中的數(shù)據(jù)每次取出一條就少一條
"""
獲取結(jié)果集中的第一條數(shù)據(jù), 注意不是整個(gè)表的第一條數(shù)據(jù)
one_data = cursor.fetchone()
獲取結(jié)果集中接下來的第 2 條和 第 3 條 數(shù)據(jù)
many_data = cursor.fetchmany(2)
獲取結(jié)果集中剩余的全部數(shù)據(jù)
all_data = cursor.fetchall()
cursor.close()
conn.close()
print(row_nums)
print(one_data)
print(many_data)
print(all_data)
3.2 獲取到的數(shù)據(jù)是字典類型的
游標(biāo)設(shè)置為字典類型
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
query_sql = "select id,name,age from t1 where name=%s;"
row_nums = cursor.execute(query_sql, ('shark1'))
獲取結(jié)果的操作和之前的一樣
result = cursor.fetchone()
conn.commit()
cursor.close()
conn.close()
print(result)
4. 操作存儲(chǔ)過程(擴(kuò)展自修)
無參數(shù)存儲(chǔ)過程
cursor.callproc('p1') #等價(jià)于cursor.execute("call p1()")
有參存儲(chǔ)過程
cursor.callproc('p2', args=(1, 22, 3, 4))
#獲取執(zhí)行完存儲(chǔ)的參數(shù),參數(shù)@開頭
cursor.execute("select @p2,@_p2_1,@_p2_2,@_p2_3") #{'@_p2_1': 22, '@p2': None, '@_p2_2': 103, '@_p2_3': 24}
row_1 = cursor.fetchone()