Home  >  Article  >  Database  >  python mysqldb_MySQL

python mysqldb_MySQL

WBOY
WBOYOriginal
2016-06-01 13:12:021350browse

python

按装时也出了问题,记录如下,以备后参.

环境:

系统: os 10.9

Python: Python 2.7.5

Mysql: 5.5.29

Mysqldb:MySQL-python-1.2.4b4.tar.gz

一、下载

     首先下载,然后解压MySQL-python-1.2.4b4.tar.gz

ps: 附资源下载 [link:sourceforge]

        ps:另附[link:网盘]

        ps:因为官网地址可能有变更,下载时未找到资源,故从 sourceforge 下载的.

二、配置

到解压后目录,vim setup_posix.py,找到mysql_config.path = "mysql_config"(约 ln:27),修改成本地 mysql_config

ps: 可用 mysql 查看 version,得到 path

三、安装

在MySQL-python-1.2.4b4解压目录执行:

sudo python setup.py build


四、测试代码

#!/usr/bin/python#--encoding:utf8--import MySQLdb# 打开数据库连接db = MySQLdb.connect("localhost","root","root","gossip" )# 使用cursor()方法获取操作游标 cursor = db.cursor()# 使用execute方法执行SQL语句cursor.execute("SELECT VERSION()")# 使用 fetchone() 方法获取一条数据库。data = cursor.fetchone()print "Database version : %s " % data# 关闭数据库连接db.close()

ps:以上代码来自于 w3cschoo.ccl[link:http://www.w3cschool.cc/python/python-mysql.html]

ps:如果遇到Reason: image not found这样的错误提示,执行 sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

如果执行成功应看到:Database version : 5.5.29


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn