{"id":89993,"date":"2024-02-27T02:10:12","date_gmt":"2024-02-26T18:10:12","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=89993"},"modified":"2024-02-27T02:10:12","modified_gmt":"2024-02-26T18:10:12","slug":"python-connect-mysql","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-connect-mysql.html","title":{"rendered":"\u4f7f\u7528 Python \u8fde\u63a5 MySQL \u7684\u65b9\u6cd5"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5c3d\u7ba1\u5f88\u591a NoSQL \u6570\u636e\u5e93\u8fd1\u51e0\u5e74\u5927\u653e\u5f02\u5f69\uff0c\u4f46\u662f\u50cf MySQL \u8fd9\u6837\u7684\u5173\u7cfb\u578b\u6570\u636e\u5e93\u4f9d\u7136\u662f\u4e92\u8054\u7f51\u7684\u4e3b\u6d41\u6570\u636e\u5e93\u4e4b\u4e00\uff0c\u6bcf\u4e2a\u5b66 Python \u7684\u90fd\u6709\u5fc5\u8981\u5b66\u597d\u4e00\u95e8\u6570\u636e\u5e93\uff0c\u4e0d\u7ba1\u4f60\u662f\u505a\u6570\u636e\u5206\u6790\uff0c\u8fd8\u662f\u7f51\u7edc\u722c\u866b\uff0cWeb \u5f00\u53d1\u3001\u4ea6\u6216\u662f\u673a\u5668\u5b66\u4e60\uff0c\u4f60\u90fd\u79bb\u4e0d\u5f00\u8981\u548c\u6570\u636e\u5e93\u6253\u4ea4\u9053\uff0c\u800c MySQL \u53c8\u662f\u6700\u6d41\u884c\u7684\u4e00\u79cd\u6570\u636e\u5e93\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4ecb\u7ecd Python \u64cd\u4f5c MySQL \u7684\u51e0\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u5728\u5b9e\u9645\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u5408\u7406\u9009\u62e9\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

1\u3001MySQL-python<\/strong><\/div>\n

MySQL-python \u53c8\u53eb MySQLdb\uff0c\u662f Python \u8fde\u63a5 MySQL \u6700\u6d41\u884c\u7684\u4e00\u4e2a\u9a71\u52a8\uff0c\u5f88\u591a\u6846\u67b6\u90fd\u4e5f\u662f\u57fa\u4e8e\u6b64\u5e93\u8fdb\u884c\u5f00\u53d1\uff0c\u9057\u61be\u7684\u662f\u5b83\u53ea\u652f\u6301 Python2.x\uff0c\u800c\u4e14\u5b89\u88c5\u7684\u65f6\u5019\u6709\u5f88\u591a\u524d\u7f6e\u6761\u4ef6\uff0c\u56e0\u4e3a\u5b83\u662f\u57fa\u4e8eC\u5f00\u53d1\u7684\u5e93\uff0c\u5728 Windows \u5e73\u53f0\u5b89\u88c5\u975e\u5e38\u4e0d\u53cb\u597d\uff0c\u7ecf\u5e38\u51fa\u73b0\u5931\u8d25\u7684\u60c5\u51b5\uff0c\u73b0\u5728\u57fa\u672c\u4e0d\u63a8\u8350\u4f7f\u7528\uff0c\u53d6\u4ee3\u7684\u662f\u5b83\u7684\u884d\u751f\u7248\u672c\u3002<\/p>\n

# \u524d\u7f6e\u6761\u4ef6sudo apt-get install python-dev libmysqlclient-dev \r\n# Ubuntusudo yum install python-devel mysql-devel \r\n# Red Hat \/ CentOS\r\n# \u5b89\u88c5pip install MySQL-python<\/pre>\n
#!\/usr\/bin\/pythonimport MySQLdbdb = MySQLdb.connect( host=\"localhost\", \r\n# \u4e3b\u673a\u540d user=\"john\", \r\n# \u7528\u6237\u540d passwd=\"megajonhy\", \r\n# \u5bc6\u7801 db=\"jonhydb\") \r\n# \u6570\u636e\u5e93\u540d\u79f0\r\n# \u67e5\u8be2\u524d\uff0c\u5fc5\u987b\u5148\u83b7\u53d6\u6e38\u6807cur = db.cursor()\r\n# \u6267\u884c\u7684\u90fd\u662f\u539f\u751fSQL\u8bed\u53e5cur.execute(\"SELECT * FROM YOUR_TABLE_NAME\")for row in cur.fetchall(): print(row[0])db.close()<\/pre>\n
2\u3001Mysqlclient<\/strong><\/div>\n

\u7531\u4e8e MySQL-python \u5e74\u4e45\u5931\u4fee\uff0c\u540e\u6765\u51fa\u73b0\u4e86\u5b83\u7684 Fork \u7248\u672c mysqlclient\uff0c\u5b8c\u5168\u517c\u5bb9 MySQLdb\uff0c\u540c\u65f6\u652f\u6301 Python3.x\uff0c\u662f Django ORM\u7684\u4f9d\u8d56\u5de5\u5177\uff0c\u5982\u679c\u4f60\u60f3\u4f7f\u7528\u539f\u751f SQL \u6765\u64cd\u4f5c\u6570\u636e\u5e93\uff0c\u90a3\u4e48\u63a8\u8350\u6b64\u9a71\u52a8\u3002\u5b89\u88c5\u65b9\u5f0f\u548c MySQLdb \u662f\u4e00\u6837\u7684\uff0cWindows \u53ef\u4ee5\u5728https:\/\/www.lfd.uci.edu\/~gohlke\/pythonlibs\/#mysqlclient \u7f51\u7ad9\u627e\u5230\u5bf9\u5e94\u7248\u672c\u7684 whl \u5305\u4e0b\u8f7d\u5b89\u88c5\u3002
\n\"\"<\/p>\n

# Windows\u5b89\u88c5pip install some-package.whl# linux \u524d\u7f6e\u6761\u4ef6sudo apt-get install python3-dev # debian \/ Ubuntusudo yum install python3-devel # Red Hat \/ CentOSbrew install mysql-connector-c # macOS (Homebrew)pip install mysqlclient<\/pre>\n
3\u3001PyMySQL<\/strong><\/div>\n

PyMySQL \u662f\u7eaf Python \u5b9e\u73b0\u7684\u9a71\u52a8\uff0c\u901f\u5ea6\u4e0a\u6bd4\u4e0d\u4e0a PyMySQL\uff0c\u6700\u5927\u7684\u7279\u70b9\u53ef\u80fd\u5c31\u662f\u5b83\u7684\u5b89\u88c5\u65b9\u5f0f\u6ca1\u90a3\u4e48\u7e41\u7410\uff0c\u540c\u65f6\u4e5f\u517c\u5bb9 MySQL-python\u3002<\/p>\n

pip install PyMySQL\r\n# \u4e3a\u4e86\u517c\u5bb9mysqldb\uff0c\u53ea\u9700\u8981\u52a0\u5165pymysql.install_as_MySQLdb()<\/pre>\n

\u4e00\u4e2a\u4f8b\u5b50<\/p>\n

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()<\/pre>\n
4\u3001Peewee<\/strong><\/div>\n

\u5199\u539f\u751f SQL \u7684\u8fc7\u7a0b\u975e\u5e38\u7e41\u7410\uff0c\u4ee3\u7801\u91cd\u590d\uff0c\u6ca1\u6709\u9762\u5411\u5bf9\u8c61\u601d\u7ef4\uff0c\u7ee7\u800c\u8bde\u751f\u4e86\u5f88\u591a\u5c01\u88c5 wrapper \u5305\u548c ORM \u6846\u67b6\uff0cORM \u662f Python \u5bf9\u8c61\u4e0e\u6570\u636e\u5e93\u5173\u7cfb\u8868\u7684\u4e00\u79cd\u6620\u5c04\u5173\u7cfb\uff0c\u6709\u4e86 ORM \u4f60\u4e0d\u518d\u9700\u8981\u5199 SQL \u8bed\u53e5\u3002\u63d0\u9ad8\u4e86\u5199\u4ee3\u7801\u7684\u901f\u5ea6\uff0c\u540c\u65f6\u517c\u5bb9\u591a\u79cd\u6570\u636e\u5e93\u7cfb\u7edf\uff0c\u5982sqlite, mysql\u3001postgresql\uff0c\u4ed8\u51fa\u7684\u4ee3\u4ef7\u53ef\u80fd\u5c31\u662f\u6027\u80fd\u4e0a\u7684\u4e00\u4e9b\u635f\u5931\u3002\u5982\u679c\u4f60\u5bf9 Django \u81ea\u5e26\u7684 ORM \u719f\u6089\u7684\u8bdd\uff0c\u90a3\u4e48 peewee\u7684\u5b66\u4e60\u6210\u672c\u51e0\u4e4e\u4e3a\u96f6\u3002\u5b83\u662f Python \u4e2d\u662f\u6700\u6d41\u884c\u7684 ORM \u6846\u67b6\u3002<\/p>\n

pip install peewee<\/pre>\n

\u4e00\u4e2a\u4f8b\u5b50<\/p>\n

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)<\/pre>\n

\u5b98\u65b9\u6587\u6863\uff1ahttp:\/\/docs.peewee-orm.com\/en\/latest\/peewee\/installation.html<\/p>\n

5\u3001SQLAlchemy<\/strong><\/div>\n

\u5982\u679c\u60f3\u627e\u4e00\u79cd\u65e2\u652f\u6301\u539f\u751f SQL\uff0c\u53c8\u652f\u6301 ORM \u7684\u5de5\u5177\uff0c\u90a3\u4e48 SQLAlchemy \u662f\u6700\u597d\u7684\u9009\u62e9\uff0c\u5b83\u975e\u5e38\u63a5\u8fd1 Java \u4e2d\u7684 Hibernate \u6846\u67b6\u3002<\/p>\n

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()<\/pre>\n

\u73b0\u5728\u5dee\u4e0d\u591a\u641e\u660e\u767d\u4e86\u8fd9\u51e0\u79cd\u6570\u636e\u5e93\u9a71\u52a8\u7684\u4f18\u52a3\uff0c\u63a5\u4e0b\u6765\u4f60\u5c31\u53ef\u4ee5\u9009\u62e9\u5176\u4e2d\u7684\u4e00\u4e2a\u8fdb\u884c\u7cfb\u7edf\u7684\u5b66\u4e60\u518d\u628a\u5b83\u5e94\u7528\u5230\u9879\u76ee\u4e2d\u53bb\u4e86\u3002<\/p>\n

\n

\u539f\u6587\u6765\u81ea\uff1ahttps:\/\/www.toutiao.com\/a6496260197185487374\/<\/a><\/p>\n

\u672c\u6587\u5730\u5740\uff1ahttp:\/\/lrxjmw.cn\/python-connect-mysql.html<\/a>\u7f16\u8f91\uff1a\u51af\u632f\u534e\uff0c\u5ba1\u6838\u5458\uff1a\u9004\u589e\u5b9d<\/span><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"

\u5bfc\u8bfb \u5c3d\u7ba1\u5f88\u591a NoSQL \u6570\u636e\u5e93\u8fd1\u51e0\u5e74\u5927\u653e\u5f02\u5f69\uff0c\u4f46\u662f\u50cf MySQL \u8fd9\u6837\u7684\u5173\u7cfb\u578b\u6570\u636e\u5e93\u4f9d\u7136\u662f\u4e92\u8054\u7f51\u7684\u4e3b\u6d41\u6570\u636e […]<\/p>\n","protected":false},"author":63,"featured_media":90005,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-89993","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/89993","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=89993"}],"version-history":[{"count":8,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/89993\/revisions"}],"predecessor-version":[{"id":290147,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/89993\/revisions\/290147"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/90005"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=89993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=89993"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=89993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}