SQL ExecuteMay(),带有python和数组,并使用重复键

SQL ExecuteMay(),带有python和数组,并使用重复键,python,mysql,sql,mysql-python,Python,Mysql,Sql,Mysql Python,我很难将重复键上的与当前代码一起使用: def upload_to_database(ticker_collection): trend_data = [] trend_data_table = "trend_data" trend_data_columns = "Ticker, Subreddit, Score, Rockets, Date" trend_data_sql = "INSERT INTO " + t

我很难将重复键上的
与当前代码一起使用:

def upload_to_database(ticker_collection):
  trend_data = []
  trend_data_table = "trend_data"
  trend_data_columns = "Ticker, Subreddit, Score, Rockets, Date"
  
  trend_data_sql = "INSERT INTO " + trend_data_table +\
                   " (" + trend_data_columns + ") VALUES (%s, %s, %s, %s, %s) " +\
                   "ON DUPLICATE KEY UPDATE " +\
                   "Score = Score + %s, " +\
                   "Rockets = Rockets + %s"

  for ticker in ticker_collection:
    ticker_subreddit = ticker_collection[ticker]['subreddit']
    ticker_score = int(ticker_collection[ticker]['score'])
    ticker_rockets = int(ticker_collection[ticker]['rockets'])
    insert_date = datetime.date(datetime.now(est))
    
    ticker_data = (ticker, ticker_subreddit, ticker_score, ticker_rockets, insert_date, 1, 1)

    trend_data.append(ticker_data)
  
  the_db_cursor.executemany(trend_data_sql, trend_data)
  the_database.commit()
  
  return the_db_cursor.rowcount, "was inserted."
trend_data_sql是我的查询,最后有分数和火箭,如果存在一个具有相同股票代码和日期时间(我的唯一键)的条目,我只想更新该条目的分数和火箭

但是,我正试图使用
executemany()
来节省数据库性能。我对如何将重复键上的
与for循环和
executemany()
结合在一起有点困惑

如有任何指导或建议,将不胜感激

表结构:

CREATE TABLE `trend_data` (
  `Ticker` varchar(255) NOT NULL,
  `Subreddit` varchar(255) NOT NULL,
  `Score` int(11) NOT NULL,
  `Rockets` int(11) NOT NULL,
  `Date` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
表索引:

ALTER TABLE `trend_data`
  ADD UNIQUE KEY `Unique_Keys` (`Ticker`,`Date`,`Subreddit`) USING BTREE;
请试试这个:

trend\u data\u sql=“插入”+趋势数据表+\
(“+趋势数据列+”)值(%s,%s,%s,%s)”+\
“在重复密钥更新时”+\
分数=分数+%s+\
“火箭=火箭+%s”

对于ticker\u集合中的ticker:
...
值=。。。
...
ticker\u数据=(ticker\u子Reddit、ticker\u分数、ticker\u火箭、值、值)
趋势数据。追加(股票行情数据)
它是在马里亚布上测试的。如果因为使用MySQL而无法正常工作,我认为您可以尝试以下方法。(我无法测试,因为我没有MySQL…)

trend\u data\u sql=“插入”+趋势数据表+\
(“+趋势数据列+”)值(%(股票代码)s、%(reddit)s、%(分数)s、%(火箭代码)s、%(日期)s)+\
“关于重复密钥更新”+\
分数=分数+%(值)s+\
“火箭=火箭+%(值)s”
对于ticker\u集合中的ticker:
...
股票行情数据={
“股票代码”:股票代码,
“reddit”:ticker_subreddit,
“分数”:股票代码分数,
"火箭":火箭,,
“日期”:插入日期,
“值”:1
}    
趋势数据。追加(股票行情数据)
\u db\u cursor.executemany(趋势数据\u sql、趋势数据)
_database.commit()
返回_db_cursor.rowcount,“已插入”
股票代码集合={
'a':{'subreddit':'aa','score':1','rockets':11},
‘b’:{‘subreddit’:‘bb’,‘score’:2,‘rockets’:22},
c:{'subreddit':'cc','score':3','rockets':33},
'd':{'subreddit':'dd','score':4','rockets':44}
}
打印(上传到数据库(股票行情采集))

@alphadmon您的示例中的变量
值是多少?如果还有其他内容,您也应该将它们附加到
trend\u数据中。@alphadmon我进行了编辑。请核对一下@我又检查了一遍。它很好用。请通过复制和粘贴替换您的
trend\u data\u sql
,并修改
ticker\u data
。您可以用简单的
1
替换
value
,以测试它是否有效。@alphadmon将您的表架构共享给我。还有,您是否将
ticker\u score
ticker\u rockets
重新用作
value
s?@alphadmon,我用编辑过的代码进行了测试。它很好用。我还添加了我的测试用例。请检查您的数据是否与我的数据具有不同的属性。