Python3直接pip安装ibm-db,连接DB2时,报错 SQLCODE=-1042,附ibm-db连接db2成功的代码。

看了网上很多下载到本地安装、导入的方法,感觉有点麻烦,我直接用pip安装也成功了。

pip安装ibm_db

pip install ibm_db -i https://pypi.douban.com/simple

安装完,程序里import ibm_db,如果没有报错基本就成功了

但是在真正执行SQL语句的时候,我报错了:SQLCODE=-1042
解决方法:
找到Python目录下的site-packages\clidriver\bin\amd64.VC12.CRT
中找到msvcp120.dll、msvcr120.dll
我用的Anaconda,所以目录在这里:
C:\ProgramData\Anaconda3\Lib\site-packages\clidriver\bin\amd64.VC12.CRT

msvcp120.dll、msvcr120.dll这两个文件,复制到C:\Windows\System32中即可,再执行成功了。

附我成功的代码~

import ibm_db_dbi

conn=ibm_db_dbi.connect('PORT=50000;PROTOCOL=TCPIP',user='back1',password='back1',
                   host='10.253.48.173',database='ksdbs',conn_options=None)
sql1="SELECT FILE_NAME FROM ks.CLR_FILE_STAT WHERE RECV_STATUS = '0' AND DATA_DATE = '20220908';"
sql2="SELECT FILE_NAME FROM ks.CLR_FILE_STAT WHERE RECV_STATUS = '1' AND HANDLE_FLAG  = '0' AND DATA_DATE = '20220908';"
cursor=conn.cursor()
cursor.execute(sql1)
row1=cursor.fetchall()
cursor.execute(sql2)
row2=cursor.fetchall()
 
print(row1)
print(row2)

结果:

[('sjsgf.dbf',), ('sjsgf.dbf',), ('sjshb.dbf',), ('sjstj.dbf',), ('sjsgf.dbf',), ('sjsfx.dbf',), ('sjsdz.dbf',), ('sjsqs.dbf',), ('sjsgf.dbf',), ('lofjs.dbf',), ('sjstp.dbf',), ('sjsgf.dbf',), ('SJSGB0630.DBF',)]
[('zqjsxx.325',)]

你可能感兴趣的:(Python,pip,pip,数据库,python)