我使用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")
由于它不会产生任何错误信息,我不知道可能出现什么问题,我们非常感谢您的帮助。
答案 0 :(得分:1)
您是否正在提交更新?根据您的Oracle版本,您的alter table
可能会自动提交,但merge
可能会被回滚。
尝试添加:
Connection.commit()
在merge
之后并查看它是否有效。
答案 1 :(得分:0)
据我了解,您正在PYTHON中执行具有MERGE语句的SQL。它需要提交。为此,请启用连接后自动提交。
broker.server.close()