What is the best way to connect to a sybase database from python?

匆匆过客 提交于 2019-12-06 02:13:33

问题


I am trying to retrieve data in a sybase data base from python and I was wondering which would be the best way to do it. I found this module but may be you have some other suggestions: http://python-sybase.sourceforge.net/ Thanks


回答1:


The sybase module you linked is by far the easiest way. You can get data like so:

import Sybase

db = Sybase.connect('server','name','pass','database')
c = db.cursor()
c.execute("sql statement")
list1 = c.fetchall()

print list1

You will have to use something like freetds to setup the interfaces for sybase, however.




回答2:


you can also connect through ODBC.




回答3:


There is also python-pymssql which is in Debian / Ubuntu. This can connect to MS-SQL-Server or Sybase using freetds. I'm not sure how it compares to the other options.

http://www.pymssql.org/

Example code, abridged from their website:

import pymssql

conn = pymssql.connect('server','user','pass','database')
cursor = conn.cursor()
cursor.execute('SELECT * FROM persons WHERE salesrep=%s', 'John Doe')
row = cursor.fetchone()
while row:
    print("ID=%d, Name=%s" % (row[0], row[1]))
    row = cursor.fetchone()
conn.close()


来源:https://stackoverflow.com/questions/3319788/what-is-the-best-way-to-connect-to-a-sybase-database-from-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!