简体   繁体   English

在 python 脚本中使用 cursor.execute 获取时间戳错误消息

[英]getting timestamp error message using cursor.execute in python script

My code is to collect stock data and save it into a database, but after it gets the data I am getting an error trying to save it to the database.我的代码是收集股票数据并将其保存到数据库中,但是在它获取数据后,我在尝试将其保存到数据库时遇到错误。

code snippet is: (failing at cursor.execute)代码片段是:(在 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

Error message:错误信息:

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

I've tried adjusting the start/end time because the 'timestamp' object makes me think it is something to do with the time I am passing, but I can't figure out what this should be.我尝试调整开始/结束时间,因为“时间戳”object 让我认为这与我经过的时间有关,但我不知道这应该是什么。

I think MySQL isn't liking the pandas Timestamp object, but I don't have MySQL to test it out on... and this is too verbose to leave in a comment. I think MySQL isn't liking the pandas Timestamp object, but I don't have MySQL to test it out on... and this is too verbose to leave in a comment.

Can you try replacing your values line with this?:您可以尝试用这个替换您的values行吗?:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM