新聞中心
現(xiàn)在Python成為人工智和大數(shù)據(jù)編程的重要語音,既然是編程語言就多多少少會(huì)需求對(duì)數(shù)據(jù)進(jìn)行操作,本篇文章重點(diǎn)為大家講解一下使用Python連接MySQL具體方法。

創(chuàng)新互聯(lián)公司從2013年開始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元克山做網(wǎng)站,已為上家服務(wù),為克山各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18980820575
1、MySQL-python
MySQL-python 又叫 MySQLdb,是 Python 連接 MySQL 最流行的一個(gè)驅(qū)動(dòng),很多框架都也是基于此庫進(jìn)行開發(fā),遺憾的是它只支持 Python2.x,而且安裝的時(shí)候有很多前置條件,因?yàn)樗腔贑開發(fā)的庫,在 Windows 平臺(tái)安裝非常不友好,經(jīng)常出現(xiàn)失敗的情況,現(xiàn)在基本不推薦使用,取代的是它的衍生版本。
# 前置條件sudo apt-get install python-dev libmysqlclient-dev
# Ubuntusudo yum install python-devel mysql-devel
# Red Hat / CentOS
# 安裝pip install MySQL-python
#!/usr/bin/pythonimport MySQLdbdb = MySQLdb.connect( host="localhost",
# 主機(jī)名 user="john",
# 用戶名 passwd="megajonhy",
# 密碼 db="jonhydb")
# 數(shù)據(jù)庫名稱
# 查詢前,必須先獲取游標(biāo)cur = db.cursor()
# 執(zhí)行的都是原生SQL語句cur.execute("SELECT * FROM YOUR_TABLE_NAME")for row in cur.fetchall(): print(row[0])db.close()
2、Mysqlclient
由于 MySQL-python 年久失修,后來出現(xiàn)了它的 Fork 版本 mysqlclient,完全兼容 MySQLdb,同時(shí)支持 Python3.x,是 Django ORM的依賴工具,如果你想使用原生 SQL 來操作數(shù)據(jù)庫,那么推薦此驅(qū)動(dòng)。安裝方式和 MySQLdb 是一樣的,Windows 可以在https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient 網(wǎng)站找到對(duì)應(yīng)版本的 whl 包下載安裝。
# Windows安裝pip install some-package.whl# linux 前置條件sudo apt-get install python3-dev # debian / Ubuntusudo yum install python3-devel # Red Hat / CentOSbrew install mysql-connector-c # macOS (Homebrew)pip install mysqlclient
3、PyMySQL
PyMySQL 是純 Python 實(shí)現(xiàn)的驅(qū)動(dòng),速度上比不上 PyMySQL,最大的特點(diǎn)可能就是它的安裝方式?jīng)]那么繁瑣,同時(shí)也兼容 MySQL-python。
pip install PyMySQL
# 為了兼容mysqldb,只需要加入pymysql.install_as_MySQLdb()
一個(gè)例子
import pymysqlconn = pymysql.connect(host='127.0.0.1', user='root', passwd="xxx", db='mysql')cur = conn.cursor()cur.execute("SELECT Host,User FROM user")for r in cur: print(r)cur.close()conn.close()
4、Peewee
寫原生 SQL 的過程非常繁瑣,代碼重復(fù),沒有面向?qū)ο笏季S,繼而誕生了很多封裝 wrapper 包和 ORM 框架,ORM 是 Python 對(duì)象與數(shù)據(jù)庫關(guān)系表的一種映射關(guān)系,有了 ORM 你不再需要寫 SQL 語句。提高了寫代碼的速度,同時(shí)兼容多種數(shù)據(jù)庫系統(tǒng),如sqlite, mysql、postgresql,付出的代價(jià)可能就是性能上的一些損失。如果你對(duì) Django 自帶的 ORM 熟悉的話,那么 peewee的學(xué)習(xí)成本幾乎為零。它是 Python 中是最流行的 ORM 框架。
pip install peewee
一個(gè)例子
import peeweefrom peewee import *db = MySQLDatabase('jonhydb', user='john', passwd='megajonhy')class Book(peewee.Model): author = peewee.CharField() title = peewee.TextField() class Meta: database = dbBook.create_table()book = Book(author="me", title='Peewee is cool')book.save()for book in Book.filter(author="me"): print(book.title)
官方文檔:http://docs.peewee-orm.com/en/latest/peewee/installation.html
5、SQLAlchemy
如果想找一種既支持原生 SQL,又支持 ORM 的工具,那么 SQLAlchemy 是最好的選擇,它非常接近 Java 中的 Hibernate 框架。
from sqlalchemy import create_enginefrom sqlalchemy.orm import sessionmakerfrom sqlalchemy_declarative import Address, Base, Personclass Address(Base): __tablename__ = 'address' id = Column(Integer, primary_key=True) street_name = Column(String(250))engine = create_engine('sqlite:///sqlalchemy_example.db')Base.metadata.bind = engineDBSession = sessionmaker(bind=engine)session = DBSession()# Insert a Person in the person tablenew_person = Person(name='new person')session.add(new_person)session.commit()
現(xiàn)在差不多搞明白了這幾種數(shù)據(jù)庫驅(qū)動(dòng)的優(yōu)劣,接下來你就可以選擇其中的一個(gè)進(jìn)行系統(tǒng)的學(xué)習(xí)再把它應(yīng)用到項(xiàng)目中去了。
當(dāng)前標(biāo)題:如何使用Python連接MySQL
標(biāo)題URL:http://www.5511xx.com/article/dpipscj.html


咨詢
建站咨詢
