0

我正在使用 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 中运行这个命令,一切正常,但如果我这样做: 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"),它将失败(没有任何错误!-它只是不会更改表)。其他命令正常工作(例如: 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")

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

4

2 回答 2

1

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

尝试添加:

Connection.commit()

之后merge看看它是否有效。

于 2013-08-09T19:15:06.667 回答
0

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

connconnection = connection.execution_options(autocommit=True)
于 2019-06-02T13:02:10.610 回答