Python 데이터 프레임을 mysql에 로드할 수 없습니다.
P粉156983446
P粉156983446 2023-09-14 22:10:45
0
1
531

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'])

P粉156983446
P粉156983446

모든 응답(1)
P粉797004644

to_sql을 사용할 수 있습니다. to_sql은 SQLAlchemy를 사용하고 SQLAlchemy는 MySQL을 지원하므로 아래 코드가 작동해야 합니다

으아악

업데이트된 행을 보려면 다음 코드를 사용하세요.

으아악
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!