I am trying to get a .csv file to load in a database I created.
Here is my function to load the data, where path is for the path of where the file is
def load_data(path,table_name,conn):
"""
Populate table from data in csv file at
provided path
"""
columns = get_table_cols(table_name,conn)
coma_seperated_columns = ",".join(columns)
cursor = conn.cursor()
with open(path) as csv_file:
reader = csv.reader(csv_file, delimiter=',')
for row in reader:
values = ",".join(row)
cursor.execute(f"INSERT INTO {table_name}
({coma_seperated_columns}) VALUES ({values})")'
Here is how I call it:
#I have the same file at two different place because I read that the error could be coming from the
#fact the file is not in OneDrive
This is line 27: load_data(r"C:\Users\18195\Desktop\Spring 2021\CS
205\warmUp\State.csv","StateDatabase",conn)
#I also tried this:
This is line 27: load_data(r"C:\Users\18195\OneDrive\State.csv",
"StateDatabase",conn)
I get this as an error:
Traceback (most recent call last):
File "C:/Users/18195/Desktop/Spring 2021/CS
205/warmUp/main.py", line 27, in <module>
Database.load_data(r"C:\Users\18195\Desktop\Spring 2021\CS
205\warmUp\State.csv","StateDatabase",conn)
When I run the debug, I can see that what I initialize for path value is not the same once in the function:
this
"C:\Users\18195\Desktop\Spring 2021\CS 205\warmUp\State.csv"
Why does it add a \ and is that what is causing the error? if not any idea why I am getting this error?
Thank you!