mysqlpythonubuntu
❶ 鐢≒ython榪炴帴Mysql鐨勫嚑縐嶆柟寮忚﹁В_python
榪欑瘒鏂囩珷涓昏佷粙緇嶄簡鐢 Python 榪炴帴 MySQL 鐨勫嚑縐嶆柟寮忥紝澶у跺彲浠ユ牴鎹瀹為檯鎯呭喌閫夋嫨鍚堢悊鐨勮繛鎺ユ柟寮,闇瑕佺殑鏈嬪弸鍙浠ュ弬鑰冧笅
灝界″緢澶 NoSQL 鏁版嵁搴撹繎鍑犲勾澶ф斁寮傚僵錛屼絾鏄鍍 MySQL 榪欐牱鐨勫叧緋誨瀷鏁版嵁搴撲緷鐒舵槸浜掕仈緗戠殑涓繪祦鏁版嵁搴撲箣涓錛屾瘡涓瀛 Python 鐨勯兘鏈夊繀瑕佸﹀ソ涓闂ㄦ暟鎹搴擄紝涓嶇′綘鏄鍋氭暟鎹鍒嗘瀽錛岃繕鏄緗戠粶鐖鉶錛學eb 寮鍙戙佷害鎴栨槸鏈哄櫒瀛︿範錛屼綘閮界諱笉寮瑕佸拰鏁版嵁搴撴墦浜ら亾錛岃 MySQL 鍙堟槸鏈嫻佽岀殑涓縐嶆暟鎹搴擄紝榪欑瘒鏂囩珷浠嬬粛 Python 鎿嶄綔 MySQL 鐨勫嚑縐嶆柟寮忥紝浣犲彲浠ュ湪瀹為檯寮鍙戣繃紼嬩腑鏍規嵁瀹為檯鎯呭喌鍚堢悊閫夋嫨銆
1銆丮ySQL-python
MySQL-python 鍙堝彨 MySQLdb錛屾槸 Python 榪炴帴 MySQL 鏈嫻佽岀殑涓涓椹卞姩錛屽緢澶氭嗘灦閮戒篃鏄鍩轟簬姝ゅ簱榪涜屽紑鍙戱紝閬楁喚鐨勬槸瀹冨彧鏀鎸 Python2.x錛岃屼笖瀹夎呯殑鏃跺欐湁寰堝氬墠緗鏉′歡錛屽洜涓哄畠鏄鍩轟簬C寮鍙戠殑搴擄紝鍦 Windows 騫沖彴瀹夎呴潪甯鎬笉鍙嬪ソ錛岀粡甯稿嚭鐜板け璐ョ殑鎯呭喌錛岀幇鍦ㄥ熀鏈涓嶆帹鑽愪嬌鐢錛屽彇浠g殑鏄瀹冪殑琛嶇敓鐗堟湰銆
# 鍓嶇疆鏉′歡
sudo apt-get install python-dev libmysqlclient-dev # Ubuntu
sudo yum install python-devel mysql-devel # Red Hat / CentOS
# 瀹夎
pip install MySQL-pythonWindows 鐩存帴閫氳繃涓嬭澆 exe 鏂囦歡瀹夎咃紝鍏浼楀彿鍥炲嶃寃in銆嶈幏鍙栦笅杞介摼鎺
#!/usr/bin/python
import MySQLdb
db = MySQLdb.connect(
host="localhost", # 涓繪満鍚
user="john", # 鐢ㄦ埛鍚
passwd="megajonhy", # 瀵嗙爜
db="jonhydb") # 鏁版嵁搴撳悕縐
# 鏌ヨ㈠墠錛屽繀欏誨厛鑾峰彇娓告爣
cur = db.cursor()
# 鎵ц岀殑閮芥槸鍘熺敓SQL璇鍙
cur.execute("SELECT * FROM YOUR_TABLE_NAME")
for row in cur.fetchall():
print(row[0])
db.close()2銆乵ysqlclient
鐢變簬 MySQL-python 騫翠箙澶變慨錛屽悗鏉ュ嚭鐜頒簡瀹冪殑 Fork 鐗堟湰 mysqlclient錛屽畬鍏ㄥ吋瀹 MySQLdb錛屽悓鏃舵敮鎸 Python3.x錛屾槸 Django ORM鐨勪緷璧栧伐鍏鳳紝濡傛灉浣犳兂浣跨敤鍘熺敓 SQL 鏉ユ搷浣滄暟鎹搴擄紝閭d箞鎺ㄨ崘姝ら┍鍔ㄣ傚畨瑁呮柟寮忓拰 MySQLdb 鏄涓鏍風殑錛學indows 鍙浠ュ湪 https://www.lfd.uci.e/~gohlke/pythonlibs/#mysqlclient 緗戠珯鎵懼埌 瀵瑰簲鐗堟湰鐨 whl 鍖呬笅杞藉畨瑁呫
# Windows瀹夎
pip install some-package.whl
# linux 鍓嶇疆鏉′歡
sudo apt-get install python3-dev # debian / Ubuntu
sudo yum install python3-devel # Red Hat / CentOS
brew install mysql-connector-c # macOS (Homebrew)
pip install mysqlclient3銆丳yMySQL
PyMySQL 鏄綰 Python 瀹炵幇鐨勯┍鍔錛岄熷害涓婃瘮涓嶄笂 MySQLdb錛屾渶澶х殑鐗圭偣鍙鑳藉氨鏄瀹冪殑瀹夎呮柟寮忔病閭d箞綣佺悙錛屽悓鏃朵篃鍏煎 MySQL-python
pip install PyMySQL
# 涓轟簡鍏煎筸ysqldb錛屽彧闇瑕佸姞鍏
pymysql.install_as_MySQLdb()涓涓渚嬪瓙
import pymysql
conn = 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銆乸eewee
鍐欏師鐢 SQL 鐨勮繃紼嬮潪甯哥箒鐞愶紝浠g爜閲嶅嶏紝娌℃湁闈㈠悜瀵硅薄鎬濈淮錛岀戶鑰岃癁鐢熶簡寰堝氬皝瑁 wrapper 鍖呭拰 ORM 妗嗘灦錛孫RM 鏄 Python 瀵硅薄涓庢暟鎹搴撳叧緋昏〃鐨勪竴縐嶆槧灝勫叧緋伙紝鏈変簡 ORM 浣犱笉鍐嶉渶瑕佸啓 SQL 璇鍙ャ傛彁楂樹簡鍐欎唬鐮佺殑閫熷害錛屽悓鏃跺吋瀹瑰氱嶆暟鎹搴撶郴緇燂紝濡俿qlite, mysql銆乸ostgresql錛屼粯鍑虹殑浠d環鍙鑳藉氨鏄鎬ц兘涓婄殑涓浜涙崯澶便傚傛灉浣犲 Django 鑷甯︾殑 ORM 鐔熸倝鐨勮瘽錛岄偅涔 peewee鐨勫︿範鎴愭湰鍑犱箮涓洪浂銆傚畠鏄 Python 涓鏄鏈嫻佽岀殑 ORM 妗嗘灦銆
pip install peewee
涓涓渚嬪瓙
import peewee
from peewee import *
db = MySQLDatabase('jonhydb', user='john', passwd='megajonhy')
class Book(peewee.Model):
author = peewee.CharField()
title = peewee.TextField()
class Meta:
database = db
Book.create_table()
book = Book(author="me", title='Peewee is cool')
book.save()
for book in Book.filter(author="me"):
print(book.title)瀹樻柟鏂囨。錛歨ttp://docs.peewee-orm.com/en/latest/peewee/installation.html
5銆丼QLAlchemy
濡傛灉鎯蟲壘涓縐嶆棦鏀鎸佸師鐢 SQL錛屽張鏀鎸 ORM 鐨勫伐鍏鳳紝閭d箞 SQLAlchemy 鏄鏈濂界殑閫夋嫨錛屽畠闈炲父鎺ヨ繎 Java 涓鐨 Hibernate 妗嗘灦銆
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy_declarative import Address, Base, Person
class 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 = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
# Insert a Person in the person table
new_person = Person(name='new person')
session.add(new_person)
session.commit()鐜板湪宸涓嶅氭悶鏄庣櫧浜嗚繖鍑犵嶆暟鎹搴撻┍鍔ㄧ殑浼樺姡錛屾帴涓嬫潵浣犲氨鍙浠ラ夋嫨鍏朵腑鐨勪竴涓榪涜岀郴緇熺殑瀛︿範鍐嶆妸瀹冨簲鐢ㄥ埌欏圭洰涓鍘諱簡錛岀濅綘瀛︿範寮蹇冿紝涓嶆噦鐨勫彲浠ュ挩璇㈡垜鍝堛