使用Python2.4在Linux下访问SQL Server(安装篇)

来源:互联网 发布:淘宝直通车图构图 编辑:程序博客网 时间:2024/05/20 18:17

以前用Python访问MSSQL用一个叫pymssql的模块,只在windows下用过,挺好用的。具体说明见这里
http://pymssql.sourceforge.net/。

现在需要在Linux下用Python访问MSSQL,安装了FreeTDS(http://www.freetds.org/)。由于这几天pymssql的源代码无法下载,Debian上也没有这个包下载,只好用FreeTDS文档中提到的Python MSSQL module(http://object-craft.com.au/projects/mssql/examples.html),下载下来按照文档执行 python setup.py install,
编译错误,出现下边的提示:
running install
running build
running build_py
running build_ext
building 'mssqldb' extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -fPIC -DHAVE_FREETDS -DHAVE_DBALTBIND -DHAVE_DBCLROPT -DHAVE_DBCURCMD -DHAVE_DBCURROW -DHAVE_DBISOPT -DHAVE_DBNUMCOMPUTE -DHAVE_DBRETTYPE -I/usr/include -I/home/hjue/Python-2.4.2/Include -I/home/hjue/Python-2.4.2 -c databuf.c -o build/temp.linux-i686-2.4/databuf.o
In file included from databuf.c:9:
mssqldb.h:59: error: syntax error at '##' token
mssqldb.h:59: error: syntax error at '##' token
mssqldb.h:59: error: syntax error at '##' token
mssqldb.h:68: error: `DBMAXCHAR' undeclared here (not in a function)
mssqldb.h:71: error: syntax error at '##' token
mssqldb.h:71: error: syntax error at '##' token
mssqldb.h:71: error: syntax error at '##' token
error: command 'gcc' failed with exit status 1

在Google上查了一下,主要是因为关于MSSQL的一些变量没有定义。
在mssqldb.h 中加入下边代码,重新运行python setup.py install 就OK了

# define DBMAXCHAR 256
#ifdef HAVE_FREETDS  
typedef unsigned char DBBIT;
#define PRNUMERIC MAXPRECISION
#define DB_MAX_PREC MAXPRECISION
#define DB_MAX_SCALE MAXPRECISION
typedef struct dbvarychar
{                 
        DBSMALLINT  len;                 
        DBCHAR      str[DBMAXCHAR];
} DBVARYCHAR;        
#endif