我正在使用 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")
由于它不会产生任何错误消息,因此我不知道可能出了什么问题,因此感谢您的帮助。