mysql重复数据更新,不重复则插入

    insert into racing_ball(rounds, time, home_team, home_team_goal, home_team_goal_lost, 
    home_ranking, score,guest_team, guest_team_goal, guest_team_goal_lost, guest_ranking,
    court_all,court_half,court_all_size,court_half_size,score_half
    )
    VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
    ON DUPLICATE KEY UPDATE home_team_goal=VALUES(home_team_goal),
    home_team_goal_lost=VALUES(home_team_goal_lost), 
    home_ranking=VALUES(home_ranking),
    guest_team_goal=VALUES(guest_team_goal),
    guest_team_goal_lost=VALUES(guest_team_goal_lost), 
    guest_ranking=VALUES(guest_ranking),
    score_half=VALUES(score_half)

当主键time(我设定为时间戳)重复时就更新后面制定的数据,如果不重复则插入数据。此语句仅用于MySQL。
python源码

 def insert_date(self, rounds, date_time, home_team, home_team_goal, home_team_goal_lost, home_ranking, score,
                    guest_team, guest_team_goal, guest_team_goal_lost, guest_ranking, court_all, court_half,
                    court_all_size, court_half_size, score_half):
        db = pymysql.connect(host=self.host, user=self.user, password=self.passwd, port=self.port, db=self.db)
        cursor = db.cursor()
        insert_sql = """
                            insert into racing_ball(rounds, time, home_team, home_team_goal, home_team_goal_lost, 
                            home_ranking, score,guest_team, guest_team_goal, guest_team_goal_lost, guest_ranking,
                                court_all,court_half,court_all_size,court_half_size,score_half
                              )
                            VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
                            ON DUPLICATE KEY UPDATE home_team_goal=VALUES(home_team_goal),
                            home_team_goal_lost=VALUES(home_team_goal_lost), home_ranking=VALUES(home_ranking),
                            score=VALUES(score), guest_team_goal=VALUES(guest_team_goal),
                            guest_team_goal_lost=VALUES(guest_team_goal_lost), guest_ranking=VALUES(guest_ranking),
                            score_half=VALUES(score_half)
                        """
        try:
            cursor.execute(insert_sql,
                           (rounds, date_time, home_team, home_team_goal, home_team_goal_lost, home_ranking, score,
                            guest_team, guest_team_goal, guest_team_goal_lost, guest_ranking, court_all, court_half,
                            court_all_size, court_half_size, score_half))
            db.commit()
        except Exception as e:
            print('Error is ' + str(e))
            db.rollback()
        db.close()

调用方法

self.db.insert_date(
                rounds=i + 1,
                date_time=times,
                home_team=data[4],
                home_team_goal=goal1,
                home_team_goal_lost=goal2,
                home_ranking=data[8],
                score=data[6],
                guest_team=data[5],
                guest_team_goal=goal2,
                guest_team_goal_lost=goal1,
                guest_ranking=data[9],
                court_all=float(data[10]),
                court_half=float(data[11]),
                court_all_size=data[12],
                court_half_size=data[13],
                score_half=data[7]
            )
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值