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

标签 python mysql python-3.x

我正在运行 Ubuntu 服务器。我希望它有一个 Python (v3.2) CGI 脚本,可以连接并运行查询到我设置的本地 MySQL 数据库。目前,我发现的唯一东西不支持 Python 3.2。请不要建议切换到早期版本的 Python,因为这不是我的选择。

最佳答案

pymysql - 纯Python MySQL客户端非常好。
它适用于 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.

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()

关于连接本地MySQL数据库的Python 3.2脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6562691/

相关文章:

python - Python 重载父类字段

python - 值错误 : The channel sent is invalid on a Raspberry Pi - Controlling GPIO Pin 2 (BOARD) using Python causes Error

Python 多个 elif 替代品

Python 使用 IPv6 地址解析主机名

php - 从表中生成唯一的配对——一个 "Fixture"

mysql - 创建 MySQL 触发器以插入第二个表

mysql - 如何在mysql中使用if语句将文本添加到字符串值

python - 如果字典键等于某个值,则从列表中删除字典

python - python中逻辑线和物理线的区别

python - 如何从没有 None 字段的类创建字典?