使用 python cx_oracle 会话池将值插入到 oracle 表

发布于 2025-01-15 12:13:00 字数 635 浏览 2 评论 0原文

任何人都可以帮助如何使用 python cx_oracle 会话池将一行插入到 oracle 表中吗?我发现以下代码存在一些问题。

类型错误:需要字符串或对象
名字为空?类型        
---- ----- ------------
身份证号码(38)  
名称 VARCHAR2(30)
def connections():
    pool = cx_Oracle.SessionPool(user='system',password='oracle',dsn='localhost/orcl', min=5, max=5)
    connection = pool.acquire()
    cur = connection.cursor()

    return cur


def update_table(cursor):
    rows=[(2,"KRR")]
    sql=("insert into exm(id,name) values(:1, :2)", rows)
    cursor.execute(sql)

Can any one help how to insert a single row into the oracle table using the python cx_oracle Session Pool? I am seeing some issue with the below code.

Type error: expecting string or object
Name Null? Type        
---- ----- ------------
ID         NUMBER(38)  
NAME       VARCHAR2(30)
def connections():
    pool = cx_Oracle.SessionPool(user='system',password='oracle',dsn='localhost/orcl', min=5, max=5)
    connection = pool.acquire()
    cur = connection.cursor()

    return cur


def update_table(cursor):
    rows=[(2,"KRR")]
    sql=("insert into exm(id,name) values(:1, :2)", rows)
    cursor.execute(sql)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很酷又爱笑 2025-01-22 12:13:00

我可以看到您的代码有两个问题。

首先是您没有正确调用cursor.execute。您正在该行中创建一个元组

    sql=("insert into exm(id,name) values(:1, :2)", rows)

,然后将其传递给

    cursor.execute(sql)

这与运行以下行具有相同的效果(注意额外的括号):

   cursor.execute(("insert into exm(id,name) values(:1, :2)", rows))

当您确实想要运行这一行时,它没有额外的括号:

   cursor.execute("insert into exm(id,name) values(:1, :2)", rows)

我会建议您将 sql 设置为仅针对数据库运行的 SQL 字符串,并将 rows 单独传递给 cursor.execute

    sql="insert into exm(id,name) values(:1, :2)"
    cursor.execute(sql, rows)

第二个问题是您似乎想同时插入多行。您可以通过命名变量 rows 并向其分配一个 1 元素元组列表来暗示这一点。要同时插入多行,您需要使用 cursor.executemany,而不是 cursor.execute

    sql="insert into exm(id,name) values(:1, :2)"
    cursor.executemany(sql, rows)

I can see two problems with your code.

The first is that you are not calling cursor.execute correctly. You are creating a tuple in the line

    sql=("insert into exm(id,name) values(:1, :2)", rows)

which you then pass to

    cursor.execute(sql)

This has the same effect of running the following line (note the extra parentheses):

   cursor.execute(("insert into exm(id,name) values(:1, :2)", rows))

when you really want to run this line, which doesn't have the extra parentheses:

   cursor.execute("insert into exm(id,name) values(:1, :2)", rows)

I would recommend that you instead set sql to just the SQL string to run against the database, and pass rows to cursor.execute separately.

    sql="insert into exm(id,name) values(:1, :2)"
    cursor.execute(sql, rows)

The second problem is that you seem to want to insert multiple rows at the same time. You hint at this by naming your variable rows and assigning a 1-element list of tuples to it. To insert multiple rows at the same time, you need to use cursor.executemany, not cursor.execute:

    sql="insert into exm(id,name) values(:1, :2)"
    cursor.executemany(sql, rows)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文