views:

93

answers:

2

Hi,

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

A: 

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.

Mestika
+2  A: 

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)
SilentGhost
Thank you so very much SilentGhost, it work like a charm! You just saved my week :-)
Mestika