自动将csv文件插入mysql表

时间:2018-06-19 04:54:53

标签: mysql python-3.x csv pymysql

我试图将大约2000个csv文件中的每一行插入到mysql表中。使用以下代码,我只从一个文件中插入了一行。如何自动化代码以便为每个文件插入所有行?插入只需要完成一次。

import pymysql.cursors
connection = pymysql.connect(host='localhost',
                         user='s',
                         password='n9',
                         db='si',
                         charset='utf8mb4',
                         cursorclass=pymysql.cursors.DictCursor)

try:
with connection.cursor() as cursor:
    sql = "INSERT INTO `TrainsS` (`No.`, `Name`,`Zone`,`From`,`Delay`,`ETA`,`Location`,`To`) VALUES (%s,%s,%s,%s,%s,%s,%s, %s)"
    cursor.execute(sql, ('03', 'P Exp','SF','HWH', 'none','no arr today','n/a','ND'))


connection.commit()
finally:
connection.close()

2 个答案:

答案 0 :(得分:0)

检查此代码怎么样?

要运行它,您可以将所有.csv文件放在一个文件夹和os.walk(folder_location)该文件夹中,以获取所有.csv文件的位置,然后我逐个打开它们并插入这里需要的DB(MySQL)。

import pandas as pd
import os
import subprocess
import warnings
warnings.simplefilter("ignore")

cwd = os.getcwd()

connection = pymysql.connect(host='localhost',
                     user='s',
                     password='n9',
                     db='si',
                     charset='utf8mb4',
                     cursorclass=pymysql.cursors.DictCursor)
files_csv = []
for subdir, dir, file in os.walk(cwd):
    files_csv += [ fi for fi in file if fi.endswith(".csv") ]

print(files_csv)

for i in range(len(files_csv)):
    with open(os.path.join(cwd, files_csv[i])) as f:
        lis=[line.split() for line in f]
        for i,x in enumerate(lis):
            #print("line{0} = {1}".format(i,x))
            #HERE x contains the row data and you can access it individualy using x[0], x[1], etc
            #USE YOUR MySQL INSERTION commands here and insert the x row here.
            with connection.cursor() as cursor:
                sql = "INSERT INTO `TrainsS` (`No.`, `Name`,`Zone`,`From`,`Delay`,`ETA`,`Location`,`To`) VALUES (%s,%s,%s,%s,%s,%s,%s, %s)"
                cursor.execute(sql, (#CONVERTED VALUES FROM x))


connection.commit()

更新 - 获取(#CONVERTED VALUES FROM X)的值

values = ""
for i in range(len(columns)):
    values = values + x[i] + "," # Here x[i] gives a record data in ith row. Here i'm just appending the all values to be inserted in the sql table.
values = values[:-1] # Removing the last extra comma.
command = "INSERT INTO `TrainsS` (`No.`, `Name`,`Zone`,`From`,`Delay`,`ETA`,`Location`,`To`) VALUES (" + str(values) + ")"
cursor.execute(command)
#Then commit using connection.commit()

答案 1 :(得分:0)

     import psycopg2
     import time
     import csv
     conn = psycopg2.connect(
             host = "localhost",
              database = "postgres",
              user = "postgres",
              password = "postgres"
             )
     cur = conn.cursor()
     start = time.time()
     with open('combined_category_data_100 copy.csv', 'r') as file:
          reader=csv.reader(file)
          ncol = len(next(reader))
          next(reader)
          for row in reader:
                  cur.execute(" insert into data values (%s = (no. of columns 
                   ))", row)

          conn.commit()
          print("data entered successfully")
          end = time.time()
          print(f" time taken is {end - start}")
          cur.close()