Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
to_table: This function takes a csv filename, an SQLite filename, and a table name with default value "new1". It adds a new table with the specified name to the db that has the information from the csv file in it. The first row of the csv file contains the column names. All columns are TEXT type. The first column is the primary key. I recommend using Python's csv module so you don't have to parse the rows of the csv file yourself.

What I have tried:

def to_table(csv_file,sq_file,table_name='new1'):
'''
'''
conn=sqlite3.connect(sq_file)
conn.row_factory=sqlite3.Row
c=conn.cursor()
with open(csv_file,'r') as csvfile:
reader =csv.reader(csvfile)
row1=next(reader)
c.execute("CREATE TABLE table_name(row1[0] TEXT PRIMARY KEY,"+"row1[1:] TEXT);")
return sq_file
Posted
Comments
Richard MacCutchan 7-Sep-18 4:55am    
Well done, your assignment is complete.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900