SQL插件在Oracle SQL Developer中有效,但在python

时间:2016-03-17 13:07:54

标签: python oracle

我有一个看起来像是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 Developer中运行,我会得到一个新行。在Python内部,我收到终止错误。从我可以告诉它正确形成和终止。

以下是我在python中构建查询的方法:

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

1 个答案:

答案 0 :(得分:0)

试试这个:

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