2016-12-12 7 views
0

次の文は何が問題になりますか?重複キーの挿入/更新でMySQLエラーが発生しました

私は私のテーブルにデータを挿入するが、重複したエントリがある場合はそれを更新しようとしている - 私はそれを実行すると、私は次のエラーを取得:

(1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'updat e temp set impressions = impressions + 1, clicks = clicks + 0, ctr = (ctr +' at line 1")

def insert_or_update_new(self, profile_id, landing_page, keyword, position, impressions, clicks, ctr): 
    try: 
     self.cursor.execute('''insert into temp (profile_id, landing_page, keyword, position, impressions, clicks, ctr) values (%s, %s, %s, %s, %s, %s, %s) on duplicate key update temp set impressions = impressions + %s, clicks = clicks + %s, ctr = (ctr + %s/2)''', (profile_id, landing_page, keyword, position, impressions, clicks, ctr, impressions, clicks, ctr)) 
     self.db.commit() 
    except Exception as e: 
     self.db.rollback() 
     # Rollback in case there is any error 
     return e 

を更新:削除重複UPDATEを - まだそこにはエラーがあります。

+0

を好きではないはずです:-)トリックを行う可能性があります削除し、二回単語更新を含み更新版の更新版を複製しました。 – cwallenpoole

答えて

1

あなたの文はあなたがきた

on duplicate key update update temp set impressions... 

しかし

on duplicate key update impressions=... 
+0

あなたは美しさ:)私はそれを許可すると受け入れます。なぜこれについての説明? – Adders

+0

下記のlarwa1nによって提供されるリンクを見てください。使用できる構文があります:) – rafwlaz

0

あなたの文は1が

+0

しかし、それでもなお(1064、 "あなたはあなたのSQL構文に誤りがあります。正しい構文が使用できるようにMySQLサーバのバージョンに対応するマニュアルをチェックしてください" mpressions = impressions + 1、clicks = clicks + 0、ctr =(ctr + 0/2) 'at line 1」) – Adders

+0

* set *も必要ないと思います。 http://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.htmlを参照してください – Iarwa1n

関連する問題