使用Python 3.2脚本连接到本地MySQL数据库 [英] Python 3.2 script to connect to local MySQL database

查看:135
本文介绍了使用Python 3.2脚本连接到本地MySQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行Ubuntu服务器.我希望它具有一个Python(v3.2)CGI脚本,该脚本将连接到我已建立的本地MySQL数据库并运行查询.目前,我发现的唯一内容不支持Python 3.2.请不要建议切换到早期版本的Python,因为这对我来说不是一个选择.

I am running an Ubuntu Server. 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. Currently, the only things I found don't support Python 3.2. Please do not suggest switching to an earlier version of Python, because that is not an option for me.

推荐答案

pymysql -纯Python MySQL客户端非常好.
它适用于Python 3.x,并且没有任何依赖关系.

pymysql - Pure Python MySQL client is very good.
It works with Python 3.x, and doesn't have any dependencies.

此纯Python MySQL客户端通过二进制客户端/服务器协议直接与服务器通信,从而为MySQL数据库提供DB-API.

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.

示例:

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

这篇关于使用Python 3.2脚本连接到本地MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆