I am trying to create a SQLite table and import a csv file into it using a python code. I've got the creating table part down correctly (easy xD):
cur.execute('''CREATE TABLE IF NOT EXISTS Survey
(`ResponseID` TEXT,
`StartDate` TEXT,
`EndDate` TEXT,
`Status` TEXT,
`SourceType` TEXT,
`InteractPedestrian` TEXT,
`InteractBicycle` TEXT,
`CircumstancesCoded` TEXT,
`FeelingsProvingGround` TEXT,
`SafetyHuman` TEXT,
`SafetyAV` TEXT,
`AVSafetyPotential` TEXT,
`RegulationTesting` TEXT,
`RegulationSpeed` TEXT,
`RegulationSchoolZone` TEXT,
`RegulationShareData` TEXT,
`AdvocacyIssues` TEXT,
`BikePghPosition` TEXT,
`PayingAttentionAV` TEXT,
`FamiliarityTechnoology` TEXT,
`ZipCode` TEXT,
PRIMARY KEY(`ResponseID`)
);
''')
That works. But I need to put in a couple lines of code after that to add the csv file's data into that table. I tried this :
''' .mode csv;
.import c:/Users/karan_4q59raf/Desktop/Pyhton codes/dataproject/bikepghpublic.csv Survey; '''
and this :
''' .separator ,
.import c:/Users/karan_4q59raf/Desktop/Python codes/dataproject/bikepghpublic.csv Survey;'''
Both of these codes end up with the same error :
sqlite3.OperationalError: near ".": syntax error
I don't see why. I've tried lots of things, which just confused me. I would love to know if the mistake is in the way I'm using '.import' and such, or if it is the syntax, or it is something else.
Right now I'm at (since someone asked for the full code I'm trying):
cur.execute('''CREATE TABLE IF NOT EXISTS Survey
(`ResponseID` TEXT,
`StartDate` TEXT,
`EndDate` TEXT,
`Status` TEXT,
`SourceType` TEXT,
`InteractPedestrian` TEXT,
`InteractBicycle` TEXT,
`CircumstancesCoded` TEXT,
`FeelingsProvingGround` TEXT,
`SafetyHuman` TEXT,
`SafetyAV` TEXT,
`AVSafetyPotential` TEXT,
`RegulationTesting` TEXT,
`RegulationSpeed` TEXT,
`RegulationSchoolZone` TEXT,
`RegulationShareData` TEXT,
`AdvocacyIssues` TEXT,
`BikePghPosition` TEXT,
`PayingAttentionAV` TEXT,
`FamiliarityTechnoology` TEXT,
`ZipCode` TEXT,
PRIMARY KEY(`ResponseID`)
);
''')
# Check to see if we are already in progress...
cur.execute('''SELECT * FROM Survey ORDER BY RANDOM() LIMIT 1''')
row = cur.fetchone()
if row is not None:
print("Restarting existing crawl. Remove spider.sqlite to start a fresh crawl.")
else :
cur.execute((''' .mode csv;
.import c:/Users/karan_4q59raf/Desktop/Pyhton codes/dataproject/bikepghpublic.csv Survey;'''))
conn.commit()
There is more code after that. But that's all based on this question. I really appreciate your help. Thank you so much! :)