【问题标题】:getting timestamp error message using cursor.execute in python script在 python 脚本中使用 cursor.execute 获取时间戳错误消息
【发布时间】:2020-12-06 18:05:31
【问题描述】:

我的代码是收集股票数据并将其保存到数据库中,但在获取数据后,我在尝试将其保存到数据库时遇到错误。

代码 sn-p 是: (在 cursor.execute 失败)

all_tickers = pd.read_sql("SELECT ticker, id FROM security", conn)
ticker_index = dict(all_tickers.to_dict('split')['data'])
tickers = list(ticker_index.keys())

start = dt.datetime(2018, 8, 8)
end = dt.datetime.now()

for ticker in tickers:
    # Download data
    df = pdr.get_data_yahoo(ticker, start, end)
    # Write to daily_price
    for row in df.itertuples():
        values = [YAHOO_VENDOR_ID, ticker_index[ticker]] + list(row)
        sql_two = "INSERT INTO daily_price (data_vendor_id,ticker_id, price_date, open_price, high_price, low_price, close_price, adj_close_price, volume) " + \
                  "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)"
        cursor.execute(sql_two, tuple(values))      #<--errorhere

错误信息:

AttributeError: 'Timestamp' object has no attribute 'translate'

我尝试调整开始/结束时间,因为“时间戳”对象让我认为这与我经过的时间有关,但我不知道这应该是什么。

【问题讨论】:

    标签: python mysql pymysql


    【解决方案1】:

    我认为 MySQL 不喜欢 pandas Timestamp 对象,但我没有 MySQL 来对其进行测试......这太冗长了,无法在评论中留下。

    你可以尝试用这个替换你的values 行吗?:

            values = [YAHOO_VENDOR_ID, ticker_index[ticker]] + 
                     [row[0].to_pydatetime()] + list(row[1:])
    

    【讨论】:

    • 似乎有效,但我现在遇到另一个错误。我会玩一段时间,看看我是否无法让它工作并会更新。
    • @JasonMaynard:好的。祝你好运。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 2012-10-26
    • 2020-06-13
    • 2020-07-05
    • 1970-01-01
    • 2021-03-14
    相关资源
    最近更新 更多