1

I'm trying to create a database connection in a python script to my DB2 database. When the connection is done I've to run some different SQL statements.

I googled the problem and has read the ibm_db API (http://code.google.com/p/ibm-db/wiki/APIs) but just can't seem to get it right.

Here is what I got so far:

import sys
import getopt
import timeit
import multiprocessing
import random
import os
import re
import ibm_db
import time
from string import maketrans

query_str = None

conn = ibm_db.pconnect("dsn=write","usrname","secret")
query_stmt   = ibm_db.prepare(conn, query_str)
ibm_db.execute(query_stmt, "SELECT COUNT(*) FROM accounts")
result = ibm_db.fetch_assoc()
print result
status = ibm_db.close(conn)

but I get an error. I really tried everything (or, not everything but pretty damn close) and I can't get it to work.

I just need to make a automatic test python script that can test different queries with different indexes and so on and for that I need to create and remove indexes a long the way.

Hope someone has a solutions or maybe knows about some example codes out there I can download and study.

Thanks

Mestika

1
  • 1
    What happens? It would be helpful if you further explain what "doesn't work" actually means. Commented May 18, 2010 at 16:20

2 Answers 2

4

it should be:

query_str = "SELECT COUNT(*) FROM accounts"

conn = ibm_db.pconnect("dsn=write","usrname","secret")
query_stmt   = ibm_db.prepare(conn, query_str)
ibm_db.execute(query_stmt)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so very much SilentGhost, it work like a charm! You just saved my week :-)
0

I'm sorry, of cause you need to error message. When trying to run my script it gives me this error:

Traceback (most recent call last):
  File "test.py", line 16, in <module>
    ibm_db.execute(query_stmt, "SELECT COUNT(*) FROM accounts")
Exception: Param is not a tuple

I'm pretty sure that it is my parameter "SELECT COUNT(*) FROM accounts" that is the problem, but I have no idea how to fix it or what to put in its place.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.