[英]MySQL if exists (select), update, else, insert with python using date comparison
我正在尝试执行db更新,条件是在满足其各自条件的情况下,对于多列具有DATE值的数据库记录存在,但我似乎遇到了麻烦。 任何帮助深表感谢。 谢谢!
以下代码
select_cmd = "SELECT * FROM " + self.designator + " WHERE player_fkid=" + str(fixed['player_fkid']) + \
" AND team_fkid=" + str(fixed['team_fkid']) + " AND season_fkid=" + str(fixed['season_fkid'])
update_start_cmd = "UPDATE " + self.designator + " SET date_start='" + str(update['date_start']) + \
"' WHERE date_start>'" + str(update['date_start']) + "'"
update_end_cmd = "UPDATE " + self.designator + " SET date_end='" + str(update['date_end']) + "' WHERE date_end<'" + \
str(update['date_end']) + "'"
insert_cmd = "INSERT INTO " + self.designator + " (player_fkid, team_fkid, season_fkid, date_start, date_end) VALUES (" + \
str(fixed['player_fkid']) + ", " + str(fixed['team_fkid']) + ", " + str(fixed['season_fkid']) + ", '" + \
str(update['date_start']) + "', '" + str(update['date_end']) + "')"
cmd = "IF EXISTS (" + select_cmd + ")" + "\n" + update_start_cmd + "\n" + update_end_cmd + "\nELSE " + insert_cmd
try:
cursor.execute(cmd)
cnx.commit()
except mysql.connector.errors.ProgrammingError:
print "Error: invalid command '" + cmd + "'"
返回以下错误:
错误:无效命令'IF EXISTS(SELECT * FROM成员WHERE player_fkid = 1 AND team_fkid = 1 AND season_fkid = 1)
UPDATE名册SET date_start ='2010-04-13'WHERE date_start>'2010-04-13'
UPDATE名册SET date_end ='2010-04-13'WHERE date_end <'2010-04-13'
ELSE INSERT INTO名册(player_fkid,team_fkid,season_fkid,date_start,date_end)值(1、1、1,'2010-04-13','2010-04-13')'
我认为您想在重复键更新语法上使用Insert...。
(特别是有条件的重复密钥更新 )
就像是:
INSERT INTO roster (player_fkid, team_fkid, season_fkid, date_start, date_end)
VALUES (1, 1, 1, '2010-04-13', '2010-04-13')
ON DUPLICATE KEY UPDATE
date_start= IF(date_start > '2010-04-13', '2010-04-13', values(date_start)),
date_end= IF(date_end > '2010-04-13', '2010-04-13', values(date_end));
我发现以下作品:
cmd = "SELECT COUNT(1) FROM roster WHERE player_fkid=" + str(fixed['player_fkid']) + " AND team_fkid=" + str(fixed['team_fkid']) + " AND season_fkid=" + str(fixed['season_fkid'])
cursor.execute(cmd)
if cursor.fetchone()[0]:
udpate_sub_cmd = "UPDATE roster"
date_start_sub_cmd = "SET date_start= IF(date_start > '" + str(update['date_start']) + "', '" + str(update['date_start']) + "', values(date_start)),"
date_end_sub_cmd = "date_end= IF(date_end > '" + str(update['date_end']) + "', '" + str(update['date_end']) + "', values(date_end));"
where_sub_cmd = "WHERE player_fkid=" + str(fixed['player_fkid']) + " AND team_fkid=" + str(fixed['team_fkid']) + " AND season_fkid=" + str(fixed['season_fkid'])
cmd = udpate_sub_cmd + " " + date_start_sub_cmd + " " + date_end_sub_cmd + " " + where_sub_cmd
cursor.execute(cmd, multi=True)
else:
insert_sub_cmd = "INSERT INTO roster (player_fkid, team_fkid, season_fkid, date_start, date_end)"
values_sub_cmd = "VALUES (" + str(fixed['player_fkid']) + ", " + str(fixed['team_fkid']) + ", " + str(fixed['season_fkid']) + ", '" + str(update['date_start']) + "', '" + str(update['date_end']) + "')"
cmd = insert_sub_cmd + " " + values_sub_cmd
cursor.execute(cmd)
使用前面提到的链接中的部分解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.