sybase自帶的sybpydb模塊用ucs2,而ubuntu14.04默認安裝的python是ucs4,直接import會出錯python
$ 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
故須要本身下載源碼、編譯安裝linux
1、安裝python程序員
wget https://www.python.org/ftp/python/2.7.11/Python-2.7.11.tgzsql
./configure --prefix=/opt/local/python27 --enable-unicode=ucs2bootstrap
mak&sudo make installubuntu
2、安裝setuptools(爲了順利安裝其它模塊)python2.7
wget https://pypi.python.org/packages/source/s/setuptools/setuptools-20.2.2.tar.gzcurl
/opt/local/python27/bin/python setup.py build測試
sudo /opt/local/python27/bin/python setup.py installfetch
這樣源碼安裝的模塊都在/opt/local/python27/lib/python2.7/site-packages 目錄下了
或先安裝pip(先下載get-pip.py,而後安裝pip):Installing with get-pip.py
To install pip, securely download get-pip.py. [1]:
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/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的搜索路徑了。
3、sybpydb.so模塊加入到python路徑
在該目錄下建立sybpydb.pth(名字隨便取、後綴必須pth)內容以下:
/opt/sybase/OCS-16_0/python/python26_64r/lib
4、測試
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
計算機
《程序員指南》中的例子(存儲過程調用),callproc.py
#!/opt/local/python27/bin/python #coding=utf-8 import sybpydb conn = 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 = 300 int_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 procedure cur.execute("drop procedure myproc") cur.close() conn.close()
運行:
$ ./callproc.py
Status = 0
int = 90000