【问题标题】:Unable to load data into mysql using python无法使用python将数据加载到mysql
【发布时间】:2018-06-17 13:02:54
【问题描述】:

我无法将符号列加载到 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 中得到 ​​p>

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()

【问题讨论】:

    标签: python mysql stock-data


    【解决方案1】:

    我认为这会满足您的需求:

           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% 确定文件名和字段组件是否可以作为参数传入。

    【讨论】:

      【解决方案2】:

      我不确定您对使用问题中的导入模块的承诺程度,但我会通过使用 Pandas 和 SQLalchemy 来解决这个问题,以简化这一切。

      我将如何解决您的问题:

      import os
      import pandas as pd
      from sqlalchemy import create_engine
      
      localExtractFilePath="/Users/dalal"
      
      config = {
      'user':'root',
      'password':'root',
      'host':'127.0.0.1',
      'database':'masterfiles'
       }
      
      
      def main():
          engine = create_engine('mysql://{}:{}r@{}/{}'.format(config['user'],config['password'],config['host'],config['database']))
      
          for file in os.listdir(localExtractFilePath):
              if file.endswith("csv"):
                  print('{} file has been found. Loading to Pandas DF'.format(file))
                  #Load to a Pandas DataFrame Object (Documentation: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html)
                  df = pd.read_csv(os.path.abspath(file))
                  df['symbol'] = os.path.splitext(file)[0][:-7]
                  print('Loading to DB....')
                  #Upload DF object to SQL (Documentation: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_sql.html)
                  df.to_sql(con=engine, index=False, if_exists='append')
                  print('Done, on to the next file!')
      
      if __name__ == '__main__':
          main()
      

      希望对您有所帮助!

      【讨论】:

        猜你喜欢
        • 2012-01-04
        • 1970-01-01
        • 2012-06-16
        • 1970-01-01
        • 2017-10-10
        • 2021-03-27
        • 2012-04-26
        • 1970-01-01
        相关资源
        最近更新 更多