Python cx_Oracle合并之谜-在SQL Dev中工作而在python33中不工作



我正在使用python33和cx_oracle(与oracle 11g一起使用)来分析数据库,但遇到了一个问题。此SQL存在问题:merge into ftab01 a using (select username , count (case when action ='friend' then 1 end) friendCount from pool_1 group by username) b on (a.USERNAME=b.username) WHEN MATCHED THEN update set a.friendCount=b.friendCount如果我在SQL Developer中运行此命令,一切都会完美运行,但如果我这样做:CCD_ 2,它将失败(没有任何错误!-它只是不改变表)。其他命令工作正常(例如:cursor.execute('alter table '+self.tabname + ' add ('+column_name+' number)')-这段代码只比有问题的代码高出两行。我不知道可能出了什么问题,我试着在谷歌上搜索了更长的时间,但我什么都没找到(也许是因为我不知道如何命名这个问题)

我使用的代码:

def action_counts(self,action_list):
sql = "merge into "+self.tabname + " a using (select username "
sql_when_matched ="";
for action in action_list:
  column_name = (action+'Count').replace('-','_')
  print(column_name)
  sql += ", count (case when action ='"+action+"' then 1 end) "+column_name
  sql_when_matched += " a."+column_name+"=b."+column_name+", "
  cursor.execute('alter table '+self.tabname + ' add ('+column_name+' number)')
sql += " from pool_1 group by username) b on (a.USERNAME=b.username) WHEN MATCHED THEN update set "+sql_when_matched
sq2 = sql.rstrip().rstrip(",")
print(sq2)
cursor.execute(sq2)
#this is the printed sq2 and copy-pasted into execute() (and if copy-pasted to SQL Developer it is working properly)
cursor.execute("merge into ftab01 a using (select username , count (case when action ='friend' then 1 end) friendCount from pool_1 group by username) b on (a.USERNAME=b.username) WHEN MATCHED THEN update set  a.friendCount=b.friendCount")

由于它不会产生任何错误信息,我不知道可能出了什么问题,我们非常感谢您的帮助。

您正在提交更新吗?根据您的Oracle版本,您的alter table可能会自动提交,但merge可能会回滚。

尝试添加:

Connection.commit()

在CCD_ 6之后,看看它是否工作。

据我所知,您正在PYTHON中执行具有MERGE语句的SQL。它需要提交。为此,请在连接后启用自动提交。

connconnection = connection.execution_options(autocommit=True)

最新更新