简体   繁体   中英

Python driver for IBM DB2 does not work

I try to execute a very simple sql statement that sets isolation level in DB2. I do it like so:

>>> import ibm_db
>>> cnx = ibm_db.connect("sample", "root", "root")
>>> sql = "CHANGE ISOLATION TO CS"
>>> query_stmt = ibm_db.prepare(cnx, sql)
>>> ibm_db.execute(query_stmt)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Exception

I also tried this:

>>> stmt = ibm_db.exec_immediate(cnx, sql)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Exception

The query for setting isolation level I took from this official tutorial. So, what's wrong with that and how can I fix it?

EDIT

I also tried other simple queries. And none of them works:

>>> ibm_db.exec_immediate(cnx, "LIST TABLES")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Exception

It seems like I lack some configuration, but I do not know how to check it and how to debug it. Need help.

EDIT

I tried this in DB2 CLP :

db2 => set schema sample

And got this error message:

SQL1024N A database connection does not exist. SQLSTATE=08003

However, when I do this:

db2 => LIST DATABASE DIRECTORY

I get this:

...
database alias   SAMPLE
database name    SAMPLE
...

So, what the heck is going on? PS. I'm working on Windows 10, if it matters.

EDIT

Well, I managed to connect to sample database in command line and even listed all tables. However, I cannot do the same thing in Python. I even tried other simple queries like select * from act - in the console I get a number of rows, but in Python I still get this Exception .

PROOF

Here is a proof:

在此处输入图片说明

As you can see on the first screen, everything is ok. However, the same thing does not work in Python according to the second screen.

在此处输入图片说明

1) First some dumb checks: is the DB server running and accepting connections. Have you checked if you are able to connect from command prompt sql tool and create tables and list tables.

2) Download the ibm_db driver source code from https://pypi.python.org/pypi/ibm_db/ and it has some tests folder in it. compare your connection settings with the config.py in the downloaded tests folder.

That's for resolving basic connectivity.

The official tutorial you quoted says "isolation changes are permitted using a type 2 connection, ". I am not sure if ibm_db is a type 2 driver or not. the README in the sources i just downloaded does not show anything related to the type of the driver.

regards -Manas

DB2 CLP commands are not SQL statements; they can only be interpreted by the CLP itself. Review the SQL reference for correct statement syntax. For example, to set the session isolation level you would use the statement set current isolation cs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM