Mysql查询没有更新同一条记录

Mysql query does not Update the same record

我想 Update/Insert 一些数据到 MySQL。这是我的代码片段:

import requests
from lxml import html
from bs4 import BeautifulSoup
import mysql.connector
import datetime

Host_Auction = 'MY_URL/live_data.json'

def Get_Json_From_Url():
        resp = requests.get(url=Host_Auction)
        data = resp.json()
        return data

def Update_Portal(server_data):
        """ Connect to MySQL database """
        conn = mysql.connector.connect(host=DBhost,database=DBname,user=DBusername,password=DBpassword)

        cursor = conn.cursor()
        ##Update Prices on portal
        for index in server_data:
          query = """UPDATE tblproducts 
                     SET updated_at  = %s                     
                     WHERE gid=21 AND configoption1 like '%auction%' AND configoption2 like %s """

          query_data =(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%d"),"%"+str(index['key'])+"%")          
          cursor.execute(query, query_data)
          conn.commit()


          print ("Update: ", cursor.rowcount, index['key'])
          if (cursor.rowcount == 0):
              cursor = conn.cursor()
              query= """INSERT INTO `tblproducts`
                        VALUES (null,'server',21,%s,%s,0,0,0,0,0,0,0,0,'recurring',0,'','','',0,
                        'auction',%s,'','','','','','','','','','','','','','','','','','','',''
                        ,'','','','','',0,0,0,0,'',0,'',0,0,0,0,0,0,'',0,0,0,0,%s,%s) """

              query_data =(index['name']+"("+str(index['key'])+")",index['description'][0]+"\n"+index['description'][1],str(index['key']),datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%d"),datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%d"))
              cursor.execute(query, query_data)
              conn.commit()

        conn.close()

if __name__ == '__main__':
        json_data_=Get_Json_From_Url()
        Update_Portal(json_data_['server'])

使用上面的代码,如果一行在 table 中,我希望它被更新,如果它在 table 中不存在,则插入一个新行。对于第一个 运行 更新查询不执行,只执行插入查询。在第一个 运行 之后,当变量 server_data 与之前相同时,所有行都应更新,并且不应插入新行。但是当我多次 运行 代码时,它会将新行插入 table 而不是更新!!我在每个 cursor.execute() 之后提交()来解决这个问题,但它没有帮助。我正在使用 mysql.connector,OS 是 linux,mysql 是 运行ning 作为 docker 容器。 table的结构如下:

CREATE TABLE `tblproducts` (
 `id` int(10) NOT NULL AUTO_INCREMENT,
 `type` text NOT NULL,
 `gid` int(10) NOT NULL,
 `name` text NOT NULL,
 `description` text NOT NULL,
 `hidden` tinyint(1) NOT NULL,
 `showdomainoptions` tinyint(1) NOT NULL,
 `welcomeemail` int(10) NOT NULL DEFAULT 0,
 `stockcontrol` tinyint(1) NOT NULL,
 `qty` int(10) NOT NULL DEFAULT 0,
 `proratabilling` tinyint(1) NOT NULL,
 `proratadate` int(2) NOT NULL,
 `proratachargenextmonth` int(2) NOT NULL,
 `paytype` text NOT NULL,
 `allowqty` int(1) NOT NULL,
 `subdomain` text NOT NULL,
 `autosetup` text NOT NULL,
 `servertype` text NOT NULL,
 `servergroup` int(10) NOT NULL,
 `configoption1` text NOT NULL,
 `configoption2` text NOT NULL,
 `configoption3` text NOT NULL,
 `configoption4` text NOT NULL,
 `configoption5` text NOT NULL,
 `configoption6` text NOT NULL,
 `configoption7` text NOT NULL,
 `configoption8` text NOT NULL,
 `configoption9` text NOT NULL,
 `configoption10` text NOT NULL,
 `configoption11` text NOT NULL,
 `configoption12` text NOT NULL,
 `configoption13` text NOT NULL,
 `configoption14` text NOT NULL,
 `configoption15` text NOT NULL,
 `configoption16` text NOT NULL,
 `configoption17` text NOT NULL,
 `configoption18` text NOT NULL,
 `configoption19` text NOT NULL,
 `configoption20` text NOT NULL,
 `configoption21` text NOT NULL,
 `configoption22` text NOT NULL,
 `configoption23` text NOT NULL,
 `configoption24` text NOT NULL,
 `freedomain` text NOT NULL,
 `freedomainpaymentterms` text NOT NULL,
 `freedomaintlds` text NOT NULL,
 `recurringcycles` int(2) NOT NULL,
 `autoterminatedays` int(4) NOT NULL,
 `autoterminateemail` int(10) NOT NULL DEFAULT 0,
 `configoptionsupgrade` tinyint(1) NOT NULL,
 `billingcycleupgrade` text NOT NULL,
 `upgradeemail` int(10) NOT NULL DEFAULT 0,
 `overagesenabled` varchar(10) NOT NULL,
 `overagesdisklimit` int(10) NOT NULL,
 `overagesbwlimit` int(10) NOT NULL,
 `overagesdiskprice` decimal(6,4) NOT NULL,
 `overagesbwprice` decimal(6,4) NOT NULL,
 `tax` tinyint(1) NOT NULL,
 `affiliateonetime` tinyint(1) NOT NULL,
 `affiliatepaytype` text NOT NULL,
 `affiliatepayamount` decimal(10,2) NOT NULL,
 `order` int(10) NOT NULL DEFAULT 0,
 `retired` tinyint(1) NOT NULL,
 `is_featured` tinyint(1) NOT NULL,
 `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
 `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
 PRIMARY KEY (`id`),
 KEY `gid` (`gid`),
 KEY `name` (`name`(64))
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT

我将列 configoption2 更改为唯一和 INT(10)。现在使用上面的代码我得到以下异常:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/mysql/connector/connection_cext.py", line 377, in cmd_query
    raw_as_string=raw_as_string)
_mysql_connector.MySQLInterfaceError: Duplicate entry '920333' for key 'configoption2'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./Update_Hetzner_Auction.py", line 139, in <module>
    Update_Portal(json_data_['server'])
  File "./Update_Hetzner_Auction.py", line 108, in Update_Portal
    cursor.execute(query, query_data)
  File "/usr/local/lib/python3.5/dist-packages/mysql/connector/cursor_cext.py", line 264, in execute
    raw_as_string=self._raw_as_string)
  File "/usr/local/lib/python3.5/dist-packages/mysql/connector/connection_cext.py", line 380, in cmd_query
    sqlstate=exc.sqlstate)
mysql.connector.errors.IntegrityError: 1062 (23000): Duplicate entry '920333' for key 'configoption2'

当我立即执行代码两次时出现此错误。如果我 运行 代码并稍等片刻,然后 运行 代码,它不会出错!

您必须将一个字段指定为唯一字段并将插入和重复更新用作单个查询。如果唯一字段不存在,该语句将插入,否则它将更新现有的。 参考:https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html