程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> SyBase數據庫 >> SyBase綜合文章 >> python用sybase自帶的sybpydb模塊訪問數據庫

python用sybase自帶的sybpydb模塊訪問數據庫

編輯:SyBase綜合文章

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])

$ 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

計算機    

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved