尝试使用Python在Oracle中运行executemmany for insert to返回值

n3h0vuf2  于 2023-06-22  发布在  Oracle
关注(0)|答案(1)|浏览(124)

早上好,
我试图找到一种方法来做一个插入到返回的SQL语句在Python中。我想使用executemmany,但我的代码在这一点上相当不可行。任何帮助将不胜感激。谢谢你。
下面是创建表的SQL:

CREATE TABLE TABLE1
   (    F1 NUMBER(*,0) GENERATED ALWAYS AS IDENTITY MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 1, 
    F2 NUMBER(*,0) NOT NULL ENABLE, 
     CONSTRAINT "TABLE1_PK" PRIMARY KEY ("F1")
   )

Python代码是:

import cx_Oracle

def makeCxDSNLessConnection(src):
    return cx_Oracle.connect(user=sUser, password=sPass ,dsn=src)
def tempDoit():
    oraCon = makeCxDSNLessConnection('EPAT')
    cur = oraCon.cursor()
    thislist=list()
    thisOut = cur.var(int)
    cur.execute("TRUNCATE TABLE TABLE1")
    for i in range(1000000):
        thislist.append((i,))
    cur.executemany("INSERT INTO TABLE1(F2) VALUES(:1) RETURNING F1 INTO :2", thislist, thisOut )
    oraCon.commit()
    while True:
        rows = cur.fetchmany(100)
        if not rows:
            break
        for row in rows:
            print(row)
q1qsirdb

q1qsirdb1#

我找到了正确的Python代码:

import cx_Oracle

def makeCxDSNLessConnection(src):
    return cx_Oracle.connect(user=sUser, password=sPass ,dsn=src)

def tempDoit():
    oraCon = makeCxDSNLessConnection('EPAT')
    cur = oraCon.cursor()
    thislist=list()
    thisOut =cur.var(cx_Oracle.STRING,arraysize=10000)
    cur.execute("TRUNCATE TABLE TABLE1")
    for i in range(10000):
        thislist.append({'F2':i,'F1':thisOut})
    cur.executemany("INSERT INTO TABLE1(F2) VALUES(:F2) RETURNING F1 INTO :F1", thislist)
    print(thisOut.values)
    oraCon.commit()

相关问题