sqlinsert可以在oraclesqldeveloper中工作,但在python中不行

2024-09-23 22:19:57 发布

您现在位置:Python中文网/ 问答频道 /正文

对于oraclesql,我有一个看起来很直接的insert语句。它在oraclesqldeveloper中可以正常工作,但是同一个命令在Python中不起作用

cx_Oracle.DatabaseError: ORA-00933: SQL command not properly ended.

这种情况发生在光标.执行()打电话。查询本身是:

^{pr2}$

如果在sqldeveloper中运行,我会得到一个新行。在Python中,我得到了终止错误。据我所知,它的形成和终止都是正确的。在

下面是我如何在python中构造查询:

sql = "insert into TestNamesTable (TestName, TheUser, TheProject) values ('%s', '%s', '%s');" % (diagname, username, project)
print sql
cursor.execute(sql)
connection.commit()

Tags: 命令sqlnot情况语句commandoracleinsert
1条回答
网友
1楼 · 发布于 2024-09-23 22:19:57

试试这个:

# These are just random definitions, must be of type table requires
diagname = "DEFINE HERE"
username = "SOMEBODY"
project = "PROJECT NAME"

# Assuming you've defined your connection before
cursor.execute("""insert into TestNamesTable (TestName, TheUser, TheProject) values (:diagname, :username, :project)""",
{"diagname": diagname,    #cx_Oracle likes this bind-variable looking syntax
 "username": username.
 "project": project})
connection.commit()

相关问题 更多 >