使用 python cx_oracle 会话池将值插入到 oracle 表
任何人都可以帮助如何使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以看到您的代码有两个问题。
首先是您没有正确调用
cursor.execute
。您正在该行中创建一个元组,然后将其传递给
这与运行以下行具有相同的效果(注意额外的括号):
当您确实想要运行这一行时,它没有额外的括号:
我会建议您将
sql
设置为仅针对数据库运行的 SQL 字符串,并将rows
单独传递给cursor.execute
。第二个问题是您似乎想同时插入多行。您可以通过命名变量
rows
并向其分配一个 1 元素元组列表来暗示这一点。要同时插入多行,您需要使用cursor.executemany
,而不是cursor.execute
: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 linewhich you then pass to
This has the same effect of running the following line (note the extra parentheses):
when you really want to run this line, which doesn't have the extra parentheses:
I would recommend that you instead set
sql
to just the SQL string to run against the database, and passrows
tocursor.execute
separately.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 usecursor.executemany
, notcursor.execute
: