baztian / jaydebeapi

JayDeBeApi module allows you to connect from Python code to databases using Java JDBC. It provides a Python DB-API v2.0 to that database.
GNU Lesser General Public License v3.0
365 stars 148 forks source link

how to executemany #216

Open ccqz opened 2 years ago

ccqz commented 2 years ago

I wish query oracle test.A table to insert oracle MAEAS.A table , it use executemany and batch run.

` import sys import os import jaydebeapi

url = 'jdbc:oracle:thin:@8.142.180.78:1521:MAEASDB' driver = 'oracle.jdbc.driver.OracleDriver' userFrom = 'test' passwordFrom = 'test' jarFile = 'D:/Tools/JDBC/ojdbc8.jar' connFrom = jaydebeapi.connect(jclassname=driver, url=url, driver_args=[userFrom, passwordFrom], jars=jarFile) cursFrom = connFrom.cursor() userTo = 'MAEAS' passwordTo = 'MAEAS' jarFile = 'D:/Tools/JDBC/ojdbc8.jar' connTo = jaydebeapi.connect(jclassname=driver, url=url, driver_args=[userTo, passwordTo], jars=jarFile) cursTo = connTo.cursor() sqlStr = 'select FREGIONCODE, FCATEGORYNUMBER, FCATEGORYNAME, FCATEGORYSUBCODE, FCATEGORYSUBNAME, FPAYITEMNUMBER, FPAYITEMNAME, FPAYITETYPE, FCATEGORYID, FCATEGORYSUBID from CT_CUS_PAYITEMSCATE_INIT order by FCATEGORYNUMBER,FCATEGORYSUBCODE,FPAYITEMNUMBER' cursFrom.execute(sqlStr) resultFrom = cursFrom.fetchall() cursTo = connTo.cursor()

print (i)

sqlInsert = "insert into CT_CUS_PAYITEMSCATE_INIT (FREGIONCODE, FCATEGORYNUMBER, FCATEGORYNAME, FCATEGORYSUBCODE, FCATEGORYSUBNAME, FPAYITEMNUMBER, FPAYITEMNAME, FPAYITETYPE, FCATEGORYID, FCATEGORYSUBID) values('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')" cursTo.executemany(sqlInsert, resultFrom)

` Traceback (most recent call last): File "d:\C\import sys.py", line 29, in cursTo.executemany(sqlInsert, resultFrom) File "D:\Programs\Python\Python\lib\site-packages\jaydebeapi__init.py", line 549, in executemany self._set_stmt_parms(self._prep, parameters) File "D:\Programs\Python\Python\lib\site-packages\jaydebeapi\init__.py", line 523, in _set_stmt_parms prep_stmt.setObject(i + 1, parameters[i]) java.sql.java.sql.SQLException: java.sql.SQLException: Invalid column index

mikhail5555 commented 2 years ago

you have to use ? as injector in your query instead of %s