当前位置:首页 » 编程语言 » mysqlpythonubuntu

mysqlpythonubuntu

发布时间: 2024-09-19 22:46:27

❶ 鐢≒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()鐜板湪宸涓嶅氭闷鏄庣槠浜呜繖鍑犵嶆暟鎹搴挞┍锷ㄧ殑浼桦姡锛屾帴涓嬫潵浣犲氨鍙浠ラ夋嫨鍏朵腑镄勪竴涓杩涜岀郴缁熺殑瀛︿範鍐嶆妸瀹冨簲鐢ㄥ埌椤圭洰涓铡讳简锛岀濅綘瀛︿範寮蹇冿纴涓嶆哕镄勫彲浠ュ捝璇㈡垜鍝堛

热点内容
编译不能显示 发布:2024-11-24 23:40:52 浏览:698
人体编程 发布:2024-11-24 23:40:51 浏览:304
谜妹缓存的文件在哪 发布:2024-11-24 23:38:12 浏览:145
服务器自动清理缓存 发布:2024-11-24 23:37:14 浏览:662
中国移动网络如何查密码 发布:2024-11-24 23:37:06 浏览:580
计算机数据库试题 发布:2024-11-24 23:30:17 浏览:173
联想云教室连接不上服务器 发布:2024-11-24 23:24:31 浏览:894
七七源码 发布:2024-11-24 22:47:20 浏览:676
请访问其他页面 发布:2024-11-24 22:46:09 浏览:555
爱丢了编程 发布:2024-11-24 22:32:36 浏览:109