【发布时间】:2020-10-31 20:40:25
【问题描述】:
'ohlc': {'open': 22719.25, 'high': 22880.0, 'low': 22665.4, 'close': 22610.75}
我需要将 JSON 输出插入到我的数据库中,但我的源代码抛出错误:
源代码:
import pymysql
conn = pymysql.connect(host='localhost', user='root', password='', database='connect')
insert_data_into_table = "insert into ticks(last_price,date,Volume,ins_token,ohlc) values(%(last_price)s,%(date)s,%(Volume)s," \
"%(ins_token)s, %(ohlc)s)"
def insert_ticks(ticks):
cursor = conn.cursor()
for tick in ticks:
cursor.execute(insert_data_into_table,{'last_price': tick['last_price'], 'date': tick['timestamp'], 'Volume': tick['volume'], 'ins_token': tick['instrument_token'], 'ohlc':tick['ohlc']})
try:
conn.commit()
except Exception:
conn.rollback()
谁能帮我解决这个问题,因为我需要获取开盘价、收盘价、高价和低价。
错误:
pymysql.err.ProgrammingError: (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 ''open': '22755.65', 'high': '22820', 'low': '22325.4', 'close': '22908.1'})' at line 1")
【问题讨论】:
-
请附上完整的错误信息。
-
试试,
json.dumps(tick['ohlc']) -
添加错误@DYZ
-
我在 for 循环中尝试了 json.dumps 但它抛出错误:@Sushanth
标签: python mysql sql mysql-python pymysql