0

I want to make the database as my all data is in the form of CSV. having 160 column and 15 rows. as the column are more so to write the name manually is very hard I want to do it like get the column names from csv file and store it in a variable I did this. My column name are now in a variable. but I'm not able to assign these value to database column.

def parseCSV(filePath):
      # CVS Column Names
      data = pd.read_csv(filePath)
      column = data.columns
      # Use Pandas to parse the CSV file
      csvData = pd.read_csv(filePath,names=column, header=None)
      # Loop through the Rows
    #   for i,row in csvData.iterrows():
    #          sql = "INSERT INTO addresses (first_name, last_name, address, street, state, zip) VALUES (%s, %s, %s, %s, %s, %s)"
    #          value = (row['first_name'],row['last_name'],row['address'],row['street'],row['state'],str(row['zip']))
    #          mycursor.execute(sql, value, if_exists='append')
    #          mydb.commit()
    #          print(i,row['first_name'],row['last_name'],row['address'],row['street'],row['state'],row['zip'])

I comment those line which is hard coded. I want this column name come from CSV not to be added manually.

2 Answers 2

2

You can directly save your dataframe as an sql table, with df.to_sql, like below:

def parseCSV(filePath):
      data = pd.read_csv(filePath)
      data.to_sql('addresses', 'yoursqlconnection')
Sign up to request clarification or add additional context in comments.

Comments

1

Get the first row as fieldnames and store it in an array. Example: columns = csvData.head() After that you can do this:

sql = "INSERT INTO table_name (".join(columns) + ") VALUES (the_values_from_csv)"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.