简体   繁体   English

Python 3.2 脚本连接到本地 MySQL 数据库

[英]Python 3.2 script to connect to local MySQL database

I am running an Ubuntu Server.我正在运行 Ubuntu 服务器。 I would like for it to have a Python (v3.2) CGI script that would connect, and run a query, to the local MySQL database I have set up.我希望它有一个 Python (v3.2) CGI 脚本,它可以连接并运行查询到我设置的本地 MySQL 数据库。 Currently, the only things I found don't support Python 3.2.目前,我发现的唯一东西不支持 Python 3.2。 Please do not suggest switching to an earlier version of Python, because that is not an option for me.请不要建议切换到 Python 的早期版本,因为这不是我的选择。

pymysql - Pure Python MySQL client is very good. pymysql - 纯Python MySQL客户端非常好。
It works with Python 3.x, and doesn't have any dependencies.它适用于 Python 3.x,并且没有任何依赖关系。

This pure Python MySQL client provides a DB-API to a MySQL database by talking directly to the server via the binary client/server protocol.这个纯 Python MySQL 客户端通过二进制客户端/服务器协议直接与服务器对话,为 MySQL 数据库提供 DB-API。

Example:例子:

 import pymysql conn = pymysql.connect(host='127.0.0.1', unix_socket='/tmp/mysql.sock', user='root', passwd=None, db='mysql') cur = conn.cursor() cur.execute("SELECT Host,User FROM user") for r in cur: print(r) cur.close() conn.close()

sqlalchemy supports MySQL and all versions of Python 3 . sqlalchemy支持 MySQL 和所有版本的 Python 3 It's nice because you don't have to write SQL;很好,因为你不必写 SQL; it makes tables look like classes and records look like objects.它使表看起来像类,而记录看起来像对象。

BlaXpirit's answer above to use pymysql works great. BlaXpirit 上面使用 pymysql 的回答效果很好。 But one small caveat.但有一个小警告。 The link to pymysql in his comment will take you to https://github.com/petehunt/PyMySQL/ which is for python 2.x.在他的评论中指向 pymysql 的链接将带你到https://github.com/petehunt/PyMySQL/这是 python 2.x。 If you are using python 3, you want to download and install pymysql3 (version 3.0.5 as of 5/12/12) from http://pypi.python.org/pypi/PyMySQL3/如果您使用的是 python 3,您想从http://pypi.Z23EEEB4347BDD26BFC6B7EE9A3B735/DDhttp下载并安装 pymysql3(版本 3.0.5,截至 2012 年 5 月 12 日)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM