0

それぞれの条件が満たされている場合に、タイプ DATE の値を持つ複数の列に対して db レコードが存在するという条件で db 更新を実行しようとしていますが、問題があるようです。どんな助けでも大歓迎です。ありがとう!

次のコード

    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 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 名簿 (player_fkid, team_fkid, season_fkid, date_start, date_end) VALUES (1, 1, 1, '2010-04-13', '2010-04-13')'

4

2 に答える 2

1

私は次の作品を発見した:

    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)

前述のリンクのソリューションの一部を使用します。

于 2013-11-14T19:19:08.753 に答える
0

Insert... on duplicate key update構文を使用したいと思います。
(具体的には、条件付きの重複キーの更新)

何かのようなもの:

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));
于 2013-11-13T23:53:39.620 に答える