gpt4 book ai didi

python - 无法使用python将数据加载到mysql中

转载 作者:可可西里 更新时间:2023-11-01 08:38:52 26 4
gpt4 key购买 nike

我无法将符号列加载到 mysql 中。文件名为 BAJFINANCE.NS.csv

Date,Open,High,Low,Close,Adj Close,Volume   
2002-07-01,5.239740,5.540930,5.239740,5.412680,0.063393,21923
2002-07-02,5.523440,5.537040,5.421420,5.443770,0.063757,61045
2002-07-03,5.505950,5.545790,5.328150,5.466120,0.064019,34161

在 mysql 中正在获取

timestamp,open,high,low,close,adj_close,volume,symbol  
2002-07-01,5.23974,5.54093,5.23974,5.41268,0.063393,21923,NULL
2002-07-02,5.52344,5.53704,5.42142,5.44377,0.063757,61045,NULL
2002-07-03,5.50595,5.54579,5.32815,5.46612,0.064019,34161,NULL

如何在mysql中包含符号

import mysql.connector
config = {
'user':'root',
'password':'root',
'host':'127.0.0.1',
'database':'masterfiles'
}
conn= mysql.connector.connect(**config)
c=conn.cursor()

def insertRows(fileName,c):
delimiter=r','
file=fileName.split("/")[-1]
symbol = file[:-7]
if file.startswith("BAJ"):
c.execute("""Load data local infile %s into table adjprice fields terminated by %s ignore 1 lines
(timestamp,open,high,low,close,adj_close,volume,@symbol)""",(fileName,delimiter))

localExtractFilePath="/Users/dalal"

import os

for file in os.listdir(localExtractFilePath):
if file.endswith("csv"):
insertRows(localExtractFilePath+"/"+file,c)
print "Loaded file "+file+" into database"
conn.commit()
c.close()
conn.close()

最佳答案

我想这会做你想做的:

       c.execute("""
Load data local infile %s
into table adjprice
fields terminated by %s
ignore 1 lines
(timestamp, open, high, low, close, adj_close, volume)
set symbol = %s
""", (fileName, delimiter, symbol))

我建议使用查询参数而不是修改字符串。但是,我不能 100% 确定文件名和字段组件是否可以作为参数传入。

关于python - 无法使用python将数据加载到mysql中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50896935/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com