python用sybase自带的sybpydb模块访问数据库

sybase自带的sybpydb模块用ucs2,而ubuntu14.04默认安装的python是ucs4,直接import会出错

$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sybpydb
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: /opt/sybase/OCS-16_0/python/python26_64r/lib/sybpydb.so: undefined symbol: PyUnicodeUCS2_Decode

故需要自己下载源码、编译安装

一、安装python

wget https://www.python.org/ftp/python/2.7.11/Python-2.7.11.tgz

 ./configure --prefix=/opt/local/python27 --enable-unicode=ucs2

mak&sudo make install

二、安装setuptools(为了顺利安装其它模块)

wget https://pypi.python.org/packages/source/s/setuptools/setuptools-20.2.2.tar.gz

/opt/local/python27/bin/python setup.py build

sudo /opt/local/python27/bin/python setup.py install

这样源码安装的模块都在/opt/local/python27/lib/python2.7/site-packages 目录下了

注意:easy_install也要用/opt/local/python27/bin/下的那个版本,如安装matlab模块

 sudo /opt/local/python27/bin/easy_install matplotlib

~$ /opt/local/python27/bin/python
Python 2.7.11 (default, Mar 12 2016, 23:13:42) 
[GCC 5.3.0 20151204] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/opt/local/python27/lib/python2.7/site-packages/setuptools-20.2.2-py2.7.egg', '/opt/local/python27/lib/python2.7/site-packages/MySQL_python-1.2.5-py2.7-linux-x86_64.egg', '/opt/local/python27/lib/python2.7/site-packages/ibm_db-2.0.6-py2.7-linux-x86_64.egg', '/opt/local/python27/lib/python2.7/site-packages/pymssql-2.1.1-py2.7-linux-x86_64.egg', '/opt/local/python27/lib/python27.zip', '/opt/local/python27/lib/python2.7', '/opt/local/python27/lib/python2.7/plat-linux2', '/opt/local/python27/lib/python2.7/lib-tk', '/opt/local/python27/lib/python2.7/lib-old', '/opt/local/python27/lib/python2.7/lib-dynload', '/opt/local/python27/lib/python2.7/site-packages', '/opt/sybase/OCS-16_0/python/python26_64r/lib']
>>>

可以看到/opt/local/python27/lib/python2.7/site-packages已经在自己编译的那个python的搜索路径了。

三、sybpydb.so模块加入到python路径

在该目录下创建sybpydb.pth(名字随便取、后缀必须pth)内容如下:

/opt/sybase/OCS-16_0/python/python26_64r/lib

四、测试

vi sybpytest1.py

#!/opt/local/python27/bin/python
import sybpydb

conn = sybpydb.connect(user='mymotif', password='wxwpxh', servername='MYMOTIFVOSTRO145480')
cur = conn.cursor()
cur.execute('select * from STUDENT')
rows = cur.fetchall()
for row in rows:
	print "-" * 55
	for col in range (len(row)):
		print "%s" % (row[col])
cur.close()
conn.close()

$ chmod +x sybpytest1.py 

$ ./sybpytest1.py 

-------------------------------------------------------

9302203

马志元   

1975-02-03

数理逻辑      

-------------------------------------------------------

9302303

马元      

1975-02-03

理论物理      

-------------------------------------------------------

9309203

王海滨   

1975-06-03

数理逻辑      

-------------------------------------------------------

9402203

金力标   

1972-02-03

通信工程         

-------------------------------------------------------

9402208

马娟      

1972-01-03

计算机    


你可能感兴趣的:(python用sybase自带的sybpydb模块访问数据库)