如何批量把A库的数据插入到B库中
把oracle数据库中的A库的CT_CUS_PAYITEMSCATE_INIT表同步到B库中的CT_CUS_PAYITEMSCATE_INIT,想通过insert into select方式实现按照下面的代码执行后,提示
Exception Traceback (most recent call last)
File OraclePreparedStatementWrapper.java:221, in oracle.jdbc.driver.OraclePreparedStatementWrapper.setObject()
File OraclePreparedStatement.java:8370, in oracle.jdbc.driver.OraclePreparedStatement.setObject()
File OraclePreparedStatement.java:8395, in oracle.jdbc.driver.OraclePreparedStatement.setObjectInternal()
File OraclePreparedStatement.java:7639, in oracle.jdbc.driver.OraclePreparedStatement.setObjectInternal()
File OraclePreparedStatement.java:7708, in oracle.jdbc.driver.OraclePreparedStatement.setObjectCritical()
File OraclePreparedStatement.java:4889, in oracle.jdbc.driver.OraclePreparedStatement.setStringInternal()
Exception: Java Exception
The above exception was the direct cause of the following exception:
java.sql.SQLException Traceback (most recent call last)
Input In [35], in <cell line: 29>()
33 sqlInsert="insert into"+' '+"CT_CUS_PAYITEMSCATE_INIT"+"(FREGIONCODE, FCATEGORYNUMBER, FCATEGORYNAME, FCATEGORYSUBCODE, FCATEGORYSUBNAME, FPAYITEMNUMBER, FPAYITEMNAME, FPAYITETYPE, FCATEGORYID, FCATEGORYSUBID)"
34 sqlInsert=sqlInsert+" Values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);"
---> 35 cursTo.execute(sqlInsert,value)
36 ()
37 #cursTo.execute(sqlStr)
38 #resultTo = cursFrom.fetchall()
File D:\Programs\Python\Python\lib\site-packages\jaydebeapi\__init__.py:532, in Cursor.execute(self, operation, parameters)
530 self._close_last()
531 self._prep = self._connection.jconn.prepareStatement(operation)
--> 532 self._set_stmt_parms(self._prep, parameters)
533 try:
534 is_rs = self._prep.execute()
File D:\Programs\Python\Python\lib\site-packages\jaydebeapi\__init__.py:523, in Cursor._set_stmt_parms(self, prep_stmt, parameters)
520 def _set_stmt_parms(self, prep_stmt, parameters):
521 for i in range(len(parameters)):
522 # print (i, parameters[i], type(parameters[i]))
--> 523 prep_stmt.setObject(i + 1, parameters[i])
java.sql.SQLException: java.sql.SQLException: 无效的列索引
我把原结果集中的数据和通过循环产生的数据都比对了,列的个数一致。下面是其中的一条数据
('3202', '3202-02', '种植治疗', '3202-02-003', '种植修复基台', '3202-02-N0020', '安卓健复合基台', 0, '3mh++7UdYAXgU2kAAAqkqvaZ/iw=', '3mh++7UjYAXgU2kAAAqkqkpSiHQ=')
代码如下:
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)
sqlStr = 'select FREGIONCODE, FCATEGORYNUMBER, FCATEGORYNAME, FCATEGORYSUBCODE, FCATEGORYSUBNAME, FPAYITEMNUMBER, FPAYITEMNAME, FPAYITETYPE, FCATEGORYID, FCATEGORYSUBID from CT_CUS_PAYITEMSCATE_INIT'
cursFrom.execute(sqlStr)
resultFrom = cursFrom.fetchall()
#cursTo.execute(sqlStr)
#result = cursFrom.fetchall()
#print(resultFrom)
value=()
for i in range(len(resultFrom)):
cursTo = connTo.cursor()
value=(resultFrom[i])
# print(value)
sqlInsert="insert into"+' '+"CT_CUS_PAYITEMSCATE_INIT"+"(FREGIONCODE, FCATEGORYNUMBER, FCATEGORYNAME, FCATEGORYSUBCODE, FCATEGORYSUBNAME, FPAYITEMNUMBER, FPAYITEMNAME, FPAYITETYPE, FCATEGORYID, FCATEGORYSUBID)"
sqlInsert=sqlInsert+" Values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);"
cursTo.execute(sqlInsert,value)
()
#cursTo.execute(sqlStr)
#resultTo = cursFrom.fetchall()
#print(sqlInsert)