[英]Connection to DB2 in Python
I'm trying to create a database connection in a python script to my DB2 database. 我正在尝试使用python脚本创建与我的DB2数据库的数据库连接。 When the connection is done I've to run some different SQL statements.
连接完成后,我必须运行一些不同的SQL语句。
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. 我在问题上进行了搜索,并阅读了ibm_db API( http://code.google.com/p/ibm-db/wiki/APIs ),但似乎无法正确解决。
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. 我只需要制作一个自动测试python脚本即可测试具有不同索引的不同查询,依此类推,为此,我需要很长一段时间来创建和删除索引。
Hope someone has a solutions or maybe knows about some example codes out there I can download and study. 希望有人能提供解决方案,或者希望我能下载和研究一些示例代码。
Thanks 谢谢
Mestika 梅斯蒂卡
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)
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. 我很确定问题出在我的参数“ SELECT COUNT(*)FROM accounts”,但是我不知道如何解决它或将其放置在什么地方。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.