我正在尝试将 python 数据帧加载到 mysql 中。它返回错误“处理格式参数失败;Python‘时间戳’无法转换为 MySQL 类型”。我不确定它是关于什么的。
import pandas as pd from datetime import date import mysql.connector from mysql.connector import Error def priceStock(tickers): today = pd.to_datetime("today").strftime("%Y-%m-%d") for ticker in tickers: conn = mysql.connector.connect(host='103.200.22.212', database='analysis_stock', user='analysis_PhamThiLinhChi', password='Phamthilinhchi') new_record = stock_historical_data(ticker, '2018-01-01', today) new_record.insert(0, 'ticker', ticker) table = 'priceStock' cursor = conn.cursor() #loop through the data frame for i,row in new_record.iterrows(): #here %s means string values sql = "INSERT INTO " + table + " VALUES (%s,%s,%s,%s,%s,%s,%s)" #đoán chắc là do format time từ python sang sql ko khớp cursor.execute(sql, tuple(row)) print("Record inserted") # the connection is not auto committed by default, so we must commit to save our changes conn.commit() priceStock(['VIC'])
您可以使用 to_sql to_sql 使用 SQLAlchemy 并且 SQLAlchemy 支持 MySQL,因此下面的代码应该可以工作
要查看更新的行,请使用以下代码: