简体   繁体   中英

MySQL if exists (select), update, else, insert with python using date comparison

i am trying to perform a db update on the condition that a db record exists in for multiple columns with values of type DATE in the event that their respective conditions are satisfied, but i appear to be having trouble. any help is much appreciated. Thanks!

the following code

    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 + "'"

returns the following error:

Error: invalid command 'IF EXISTS (SELECT * FROM roster WHERE player_fkid=1 AND team_fkid=1 AND season_fkid=1)
UPDATE roster SET date_start='2010-04-13' WHERE date_start>'2010-04-13'
UPDATE roster SET date_end='2010-04-13' WHERE date_end<'2010-04-13'
ELSE INSERT INTO roster (player_fkid, team_fkid, season_fkid, date_start, date_end) VALUES (1, 1, 1, '2010-04-13', '2010-04-13')'

I think you want to use the Insert... on duplicate key update syntax.
(and specifically, conditional duplicate key updates )

Something like:

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));

i've found that the following works:

    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)

using a portion of the solution from the previously mentioned link.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM