TypeError: can only concatenate str (not "numpy.float64") to str Python/MYSQL

时间:2021-05-16 16:29:09

标签: python mysql

我正在为自己编写一个带有 python 后端的股票跟踪器应用程序,我需要添加一个更新语句,当我执行代码时,代码不再起作用 - 我收到错误消息,“TypeError: can only concatenate str (not "numpy.float64") 到 str。"

incert 语句工作正常,如果我删除它,它会再次工作。有什么想法我哪里出错了吗?

mycursor = mydb.cursor()
mycursor.execute("UPDATE stock_mains.user_stock SET user_stock1_price = '" + stock_live_price + "' WHERE user_id = 'da_mike' AND user_stock1 is NOT NULL")
mydb.commit()

     if market_status == "REGULAR":
         mycursor = mydb.cursor()
         sql = "INSERT INTO stock_live (Stock_symbol, Stock_live_price, stock_datetime, stock_volume, stock_marketcap, stock_dayrange, stock_open, stock_previousclose) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)"
         val = (arg, stock_live_price, timestamp, volume, marketcap, dayrange, openprice, previousclose)
         mycursor.execute(sql, val)
         mydb.commit()
         stock_live_int = float(stock_live_price)
         print(round(stock_live_int, 2))
     elif market_status == "PRE":
         stock_premarket_price = si.get_premarket_price(arg)                   
         mycursor = mydb.cursor()
         sql = "INSERT INTO stock_live (Stock_symbol, stock_premarket_price, stock_datetime, stock_volume, stock_marketcap, stock_dayrange, stock_open, stock_previousclose) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)"
         val = (arg, stock_premarket_price, timestamp, volume, marketcap, dayrange, openprice, previousclose)
         mycursor.execute(sql, val)
         mydb.commit()
         print(stock_premarket_price)
         print(mycursor.rowcount, "record inserted.")  
         
     elif market_status == "POST":
         stock_postmarket_price = si.get_postmarket_price(arg)                   
         mycursor = mydb.cursor()
         sql = "INSERT INTO stock_live (Stock_symbol, stock_aftermarket_price, stock_datetime, stock_volume, stock_marketcap, stock_dayrange, stock_open, stock_previousclose) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)"
         val = (arg, stock_postmarket_price, timestamp, volume, marketcap, dayrange, openprice, previousclose)
         mycursor.execute(sql, val)
         mydb.commit()
         print(stock_postmarket_price) 
         print(mycursor.rowcount, "record inserted.")                 
     elif market_status == "CLOSED":
         mycursor = mydb.cursor()
         mycursor.execute("UPDATE stock_mains.user_stock SET user_stock1_price = '" + stock_live_price + "' WHERE user_id = 'da_mike' AND user_stock1 is NOT NULL")
         mydb.commit()
         print("Market is Closed")                       
         stock_live_int = float(stock_live_price)
         print(round(stock_live_int, 2))
         


   
     time.sleep(0.1)


1 个答案:

答案 0 :(得分:0)

首先,您应该使用准备好的语句来避免 SQL 注入。您可以更改查询以将浮点值转换为 str:

mycursor = mydb.cursor()
query = "UPDATE stock_mains.user_stock SET user_stock1_price = %s WHERE user_id = 'da_mike' AND user_stock1 is NOT NULL" 
mycursor.execute(query, (str(stock_live_price),))
mydb.commit()