Connection to DB2 in Python
Posted
by Mestika
on Stack Overflow
See other posts from Stack Overflow
or by Mestika
Published on 2010-05-18T16:18:39Z
Indexed on
2010/05/18
16:20 UTC
Read the original article
Hit count: 296
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
© Stack Overflow or respective owner