SQL insert适用于Oracle SQL Developer,但不适用于python



我有一个看起来很直接的Oracle SQL插入语句。 它在Oracle SQL Developer中工作正常,但相同的命令在Python中不起作用,抱怨

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

这发生在 cursor.execute() 调用的行上。查询本身为:

insert into TestNamesTable (TestName, TheUser, TheProject) values ('mytest.s', 'bjurasz', 'Beta');

如果在SQL开发人员中运行,我会得到一个新行。 在 Python 中,我收到终止错误。 据我所知,它的正确形成和终止。

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

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

试试这个:

# 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()

最新更新