簡體   English   中英

如何將python表數據導入sql表?

[英]How do I import python table data into an sql table?

import mysql.connector

mydb = mysql.connector.connect(
    host="localhost",
    user="root",
    passwd="password",
    database="stocksdatabase"
    )
mycursor = mydb.cursor()

import yfinance as yf
ticker = yf.Ticker("MSFT")
hist=ticker.history(period="max")

column_str = """id, Date, Open, High, 
          Low, Close, Volume, Dividends, Stock Splits"""
insert_str = ("%s, " * 9)[:-2]
final_str = "INSERT INTO historical_price (%s) VALUES (%s)" % (column_str, insert_str)

mycursor.execute(final_str, hist)
mydb.commit()

我收到錯誤消息“DataFrame 的真值不明確。請使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。”

我該如何處理?

您正在嘗試將整個 dataframe 插入到表中。 execute的參數關鍵字只能接受元組或字典 您可能需要遍歷整個 dataframe 並按行輸入數據。 類似(未測試)的內容:

for index, row in hist.iterrows():
    mycursor.execute(final_str, row)
mydb.commit()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM