Python SQLAlchemy 的安装

首先,当然是到官方网站去下载最新的安装包了。

由于我使用的是mysqldb连接数据库,所以还要下载这个的最新版

参考了以下这篇文章 猛击这里

有一点要指出的是

#mysql_config = /usr/local/bin/mysql_config

在ubuntu里需要 sudo apt-get install libmysqlclient-dev 才能找到这个文件,注意这个是个

可执行文件,不是my.cnf

也不是其路径哦~

然后就是 sudo python setup.py install 了,然后我就杯具的反省_mysql.c编译错误。。。

果断搜索,发现需要python2.6-dev,显然这个是个开发包,需要先给装上。

OK,这次终于成功了~

然后就是安装SQLAlchemy了,一路成功,没问题。


import MySQLdb
conn = MySQLdb.connect(
		host='localhost',user='user',
		passwd='passwd',db='test')

cursor = conn.cursor()
sql = 'select * from t'
n = cursor.execute(sql)
r = cursor.fetchall()

print n
print r

这样就可以测试MySQLdb是否正常了

然后

from sqlalchemy import *
mysql_engine = create_engine("mysql://root:root@127.0.0.1/test")
connection = mysql_engine.connect()
result = connection.execute("select name from tname")
for row in result:
    print "name:", row['name']
connection.close()

这样测试SQLAlchemy

 

原文地址:https://www.cnblogs.com/TLightSky/p/2941439.html