Skip to content

Instantly share code, notes, and snippets.

@reiktar
Last active September 1, 2021 04:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reiktar/a9111163fcd45611baa8c4c1b4922c46 to your computer and use it in GitHub Desktop.
Save reiktar/a9111163fcd45611baa8c4c1b4922c46 to your computer and use it in GitHub Desktop.
Csv To SQLite3
## To run
# python3 csv_to_sqlite.py -i inputcsvfile.csv -o mydatabase.db -t tablename
import sys
import getopt
import csv, sqlite3
def load_csv_headers(inputfile):
ifile = open(inputfile,'r')
for row in csv.reader(ifile):
return row
def load_csv(inputfile, outputfile, tablename="data"):
ifile = open(inputfile,'r')
dr = csv.DictReader(ifile)
columns = load_csv_headers(inputfile)
con = sqlite3.connect(outputfile)
cur = con.cursor()
collist = f"{columns[0]}" + ( ", {} "*(len(columns)-1)).format(*columns[1:])
inslist = f"?" + ( ", ? "*(len(columns)-1))
# print(f"CREATE TABLE {tablename} ({collist})")
cur.execute(f"CREATE TABLE {tablename} ({collist})")
to_db = [ tuple(row.values()) for row in dr ]
cur.executemany(f"INSERT INTO {tablename} ({collist}) VALUES ({inslist});",to_db)
con.commit()
con.close()
def main(cmd, argv):
csvfile = 'data.csv'
sqlite = ':memory:'
tablename = "data"
try:
opts, args = getopt.getopt(argv, "hi:o:t:", ["ifile=", "ofile=","table="])
except getopt.GetoptError:
print(f"{cmd} -i <inputfile> -o <outputfile> -t <tablename>")
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print(f"{cmd} -i <inputfile> -o <outputfile> -t <tablename>")
elif opt in ("-t", "-table"):
tablename = arg
elif opt in ("-i", "-ifile"):
csvfile = arg
elif opt in ("-o", "-ofile"):
sqlite = arg
load_csv(csvfile, sqlite, tablename)
if __name__ == "__main__":
main(sys.argv[0], sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment