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

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

相关文章
相关标签/搜索