Created
January 24, 2014 02:24
-
-
Save lovasoa/8591007 to your computer and use it in GitHub Desktop.
Import csv file in a SQLite database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import sqlite3,sys,os.path | |
if len(sys.argv) != 3: | |
print ("Usage:\n\t%s db_file csv_data_file" % (sys.argv[0],)) | |
exit(1) | |
datatypes = (int, float, lambda s:s.strip(" \n\t\"'")) | |
datafilename = sys.argv[2] | |
table_name = os.path.splitext(os.path.basename(datafilename))[0] | |
con = sqlite3.connect(sys.argv[1]) | |
with open(datafilename) as fdata: | |
fields = fdata.readline().strip().split(',') | |
con.execute("CREATE TABLE IF NOT EXISTS `%s` (%s)" % (table_name, ','.join(fields))) | |
questionmarks = ','.join(["?"] * len(fields)) | |
for l in fdata: | |
l=l.strip() | |
data = list(l.strip().split(',')) | |
for i,d in enumerate(data): | |
for typ in datatypes: | |
try: | |
data[i] = typ(d) | |
break | |
except:pass | |
con.execute("INSERT INTO `%s` VALUES (%s)" % (table_name, questionmarks), data) | |
con.commit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
At that time, I didn't know that the default sqlite3 binary allowed csv import.