sybase自带的sybpydb模块用ucs2,而ubuntu14.04默认安装的python是ucs4,直接import会出错
$ pythonPython 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import sybpydbTraceback (most recent call last): File "", line 1, in 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
/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 目录下了
或先安装pip(先下载get-pip.py,然后安装pip):Installing with get-pip.py
To install pip, securely download . :
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
Inspect get-pip.py
for any malevolence. Then run the following:
sudo /opt/local/python27/bin/python get-pip.py
这样/opt/local/python27/bin下面就有了pip和easy_install
注意:easy_install也要用/opt/local/python27/bin/下的那个版本,如安装matlab模块
sudo /opt/local/python27/bin/easy_install matplotlib
~$ /opt/local/python27/bin/pythonPython 2.7.11 (default, Mar 12 2016, 23:13:42) [GCC 5.3.0 20151204] on linux2Type "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/pythonimport sybpydbconn = 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
计算机
《程序员指南》中的例子(存储过程调用),callproc.py
#!/opt/local/python27/bin/python#coding=utf-8import sybpydbconn = sybpydb.connect(user='mymotif', password='wxwpxh', servername='MYMOTIFVOSTRO145480')# Create a cursor object.cur = conn.cursor()cur.execute(""" create procedure myproc @int1 int, @int2 int output as begin select @int2 = @int1 * @int1 end """)int_in = 300int_out = sybpydb.OutParam(int())vals = cur.callproc('myproc', (int_in, int_out))print ("Status = %d" % cur.proc_status)print ("int = %d" % vals[1])cur.connection.commit()# Remove the stored procedurecur.execute("drop procedure myproc")cur.close()conn.close()
运行:
$ ./callproc.py
Status = 0
int = 90000