6

I am currently connecting to a Sybase 15.7 server using sybpydb. It seems to connect fine:

import sys
sys.path.append('/dba/sybase/ase/15.7/OCS-15_0/python/python26_64r/lib')
sys.path.append('/dba/sybase/ase/15.7/OCS-15_0/lib')
import sybpydb

conn = sybpydb.connect(user='usr', password='pass', servername='serv')

is working fine. Changing any of my connection details results in a connection error.

I then select a database:

curr = conn.cursor()
curr.execute('use db_1')

however, now when I try to run queries, it always returns None

print curr.execute('select * from table_1')

I have tried running the use and select queries in the same execute, I have tried including go commands after each, I have tried using curr.connection.commit() after each, all with no success. I have confirmed, using dbartisan and isql, that the same queries I am using return entries.

Why am I not getting results from my queries in python?

EDIT:

Just some additional info. In order to get the sybpydb import to work, I had to change two environment variables. I added the lib paths (the same ones that I added to sys.path) to $LD_LIBRARY_PATH, i.e.:

setenv LD_LIBRARY_PATH "$LD_LIBRARY_PATH":dba/sybase/ase/15.7/OCS-15_0/python/python26_64r/lib:/dba/sybase/ase/15.7/OCS-15_0/lib

and I had to change the SYBASE path from 12.5 to 15.7. All this was done in csh.

If I print conn.error(), after every curr.execute(), I get:

("Server message: number(5701) severity(10) state(2) line(0)\n\tChanged database context to 'master'.\n\n", 5701)
Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
alh
  • 2,569
  • 6
  • 29
  • 42
  • You can specify the database name in your select. 'select * from db_1..table_1' I don't know if that will help, but it makes for more concise code. – Mike Gardner Dec 21 '12 at 15:08
  • The docs don't seem to require use db_1 ( http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc01692.1570/doc/html/car1313098595988.html ). Try printing conn.messages() after every communication with database to see if there are any warnings/errors. – Himanshu Dec 24 '12 at 02:00
  • conn.messages() doesn't seem to be a function in the api, but if I print conn.error(), after every curr.execute(), I get: ("Server message: number(5701) severity(10) state(2) line(0)\n\tChanged database context to 'master'.\n\n", 5701) – alh Dec 26 '12 at 22:34
  • As an aside, you might consider contacting Sybase technical support. Their documentation seems to reference sample sample code, but never actually provides it over the web. Sybase is not free, so I imagine that any sort of license will probably come with at least basic email support. – bitcycle Jan 02 '13 at 19:11

2 Answers2

1

I completely understand where you might be confused by the documentation. Its doesn't seem to be on par with other db extensions (e.g. psycopg2).

When connecting with most standard db extensions you can specify a database. Then, when you want to get the data back from a SELECT query, you either use fetch (an ok way to do it) or the iterator (the more pythonic way to do it).

import sybpydb as sybase

conn = sybase.connect(user='usr', password='pass', servername='serv')
cur = conn.cursor()

cur.execute("use db_1")
cur.execute("SELECT * FROM table_1")
print "Query Returned %d row(s)" % cur.rowcount

for row in cur:
    print row

# Alternate less-pythonic way to read query results
# for row in cur.fetchall():
#    print row

Give that a try and let us know if it works.

bitcycle
  • 7,632
  • 16
  • 70
  • 121
0

Python 3.x working solution:

import sybpydb

try:
    conn = sybpydb.connect(dsn="Servername=serv;Username=usr;Password=pass")
    cur = conn.cursor()

    cur.execute('select * from db_1..table_1')

    # table header
    header = tuple(col[0] for col in cur.description)
    print('\t'.join(header))
    print('-' * 60)

    res = cur.fetchall()

    for row in res:
        line = '\t'.join(str(col) for col in row)
        print(line)

    cur.close()
    conn.close()

except sybpydb.Error:
    for err in cur.connection.messages:
        print(f'Error {err[0]}, Value {err[1]}')
MST
  • 651
  • 1
  • 4
  • 6