python 2.7.11 + windows 10 連線 mysql學習記錄
阿新 • • 發佈:2019-02-02
1. 前言
最近在學習mysql 的相關內容, 於是考慮使用 python 去連線他, 學習一下之後發現, 使用python 去連線mysql, 真的好方便啊。
2. 基本流程
我們這裡使用 3 種途徑來連線 mysql, 分別是 connector, mysqldb, torndb
ps: 我們這裡選用的資料庫表的格式為:
ps: 圖中的這個軟體是 mysql workbench, 當然我們也可以使用 cmd控制檯, 不過這個介面看上去更加直觀一些, 嘻嘻嘻
2.1 mysql connectors
2.1.1 安裝配置
- 首先進入 https://www.mysql.com/products/connector/ 頁面, 選擇相應版本的 python driver 下載安裝, 我們這裡選用的是 x64, python 2.7 版本的 msi installer, 連結: http://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python-2.1.3-py2.7-winx64.msi
2.1.2 查詢
from __future__ import print_function
sql = ('select * from ipdata limit 10')
# mysql-connector
print('mysql-connector'.center(50, '='))
from mysql import connector
cnx = connector.Connect(host="127.0.0.1", user="root", password="zhyh2010", database="pythontest", charset = "utf8")
cnx.autocommit = True
db0 = cnx.cursor()
db0.execute(sql)
for row in db0:
print(*row)
2.1.3 插入資料
sql = 'insert into `ipdata` (`startip`, `endip`, `country`, `local`) values (20, 21, "china", "happy")'
sql_tmp = 'insert into `ipdata` (`startip`, `endip`, `country`, `local`) values (%s, %s, %s, %s)'
values = [(21, 22 , "china", "roman")]
print('mysql-connector'.center(50, '='))
from mysql import connector
cnx = connector.Connect(host="127.0.0.1", user="root", password="zhyh2010", database="pythontest", charset = "utf8")
cnx.autocommit = True
db0 = cnx.cursor()
print db0.execute(sql)
print db0.executemany(sql_tmp, values)
2.2 mysqldb
2.2.1 安裝配置
- 首先進入 https://sourceforge.net/projects/mysql-python/ 下載相應的 mysqldb版本, 連結: https://sourceforge.net/projects/mysql-python/files/latest/download
2.2.2 查詢資料庫
#Mysqldb
print('Mysqldb'.center(50, "="))
import MySQLdb
def connect_mysql(db_host='127.0.0.1', user = "root",
passwd = "zhyh2010", db = "pythontest", charset="utf8"):
conn = MySQLdb.connect(host=db_host, user = user, passwd=passwd, db=db, charset=charset)
conn.autocommit(True)
return conn.cursor()
db1 = connect_mysql()
db1.execute(sql)
for row in db1:
print(*row)
2.2.3 查詢資料
sql = 'insert into `ipdata` (`startip`, `endip`, `country`, `local`) values (20, 21, "china", "happy")'
sql_tmp = 'insert into `ipdata` (`startip`, `endip`, `country`, `local`) values (%s, %s, %s, %s)'
values = [(21, 22, "china", "roman")]
#Mysqldb
print('Mysqldb'.center(50, "="))
import MySQLdb
def connect_mysql(db_host='127.0.0.1', user = "root",
passwd = "zhyh2010", db = "pythontest", charset="utf8"):
conn = MySQLdb.connect(host=db_host, user = user, passwd=passwd, db=db, charset=charset)
conn.autocommit(True)
return conn.cursor()
db1 = connect_mysql()
print db1.execute(sql), db1.lastrowid
print db1.executemany(sql_tmp, values), db1.lastrowid
2.3 torndb
2.3.1 安裝配置
- torndb 的主頁是: https://pypi.python.org/pypi/torndb, 不過我們這裡直接採用 pip 安裝:
- `pip install torndb`
2.3.2 查詢資料庫
#torndb
print('torndb1'.center(50, '='))
import torndb
import simplejson as json
db2 = torndb.Connection(
host="127.0.0.1",
database="pythontest",
user="root",
password="zhyh2010",
charset="utf8"
)
rows = db2.query(sql)
for row in rows:
print(json.dumps(row, ensure_ascii=False))
# print('torndb2'.center(50, '='))
# row = db2.get(sql)
# print(json.dumps(row, ensure_ascii=False))
print('torndb3'.center(50, '='))
row = db2.get('select * from ipdata limit 1')
print(json.dumps(row, ensure_ascii=False))
2.3.3 插入資料
sql = 'insert into `ipdata` (`startip`, `endip`, `country`, `local`) values (20, 21, "china", "happy")'
sql_tmp = 'insert into `ipdata` (`startip`, `endip`, `country`, `local`) values (%s, %s, %s, %s)'
values = [(21, 22, "china", "roman")]
print('torndb1'.center(50, '='))
import torndb
import simplejson as json
db2 = torndb.Connection(
host="127.0.0.1",
database="pythontest",
user="root",
password="zhyh2010",
charset="utf8"
)
print db2.insert(sql)
print db2.insertmany(sql_tmp, values)
3. 小結
- 以上3種方式, 都可以方便的實現資料庫操作
- 他們有著相同的流程: