为什么这个sql脚本不执行?

时间:2011-09-09 06:39:55

标签: python sql

您好我有以下python:

c = conn.cursor()

#get the account id for the specific user

actidSQL = "select id from accounts where user_id = (select id from auth_user where username = '%s');" % user
c.execute(actidSQL)
actid = c.fetchone()[0]
print actid

#fill the latencies table - currently not firing, not sure why?
latencies_sql = "insert into latencies(account, replier, sender, replyemail, origemail, replydate, origdate) select m1.account, c1.id as replier, c2.id as sender, m1.id as replyemail, m2.id as origemail, m1.date as replydate, m2.date as origdate from contacts c1, contacts c2, emails m1, emails m2 where m1.id > m2.id and m1.reply = m2.mid and m1.reply is not null and c1.id = m1.fr and c2.id = m2.fr and m1.account = %s and m1.account = m2.account;" % (actid)
print latencies_sql
c.execute(latencies_sql)

第一个sql执行但第二个没有执行。有原因吗?

2 个答案:

答案 0 :(得分:1)

你是什么意思“第一个sql执行但第二个没有。”?你收到一个错误?或者数据库中没有数据?我假设数据库中没有数据,而您使用的是MySQL。这是因为您不提交更改。脚本末尾的conn.commit()应该有帮助。

答案 1 :(得分:0)

它看起来像一个无效的查询问题。您尝试在同一个c.execute中运行2个查询或1.5个查询

您要么插入

insert into latencies(account, replier, sender, replyemail, origemail, replydate, origdate)

或选择

select m1.account, c1.id as replier, c2.id as sender, m1.id as replyemail, m2.id as origemail, m1.date as replydate, m2.date as origdate from contacts c1, contacts c2, emails m1, emails m2 where m1.id > m2.id and m1.reply = m2.mid and m1.reply is not null and c1.id = m1.fr and c2.id = m2.fr and m1.account = %s and m1.account = m2.account;"