twitools/csvdb.py

33 lines
966 B
Python
Raw Normal View History

2015-04-13 20:58:32 +00:00
#!/usr/bin/env python3
2016-04-07 22:23:04 +00:00
import dbtools
import sqlite3, csv, sys
def makeDB(dbo=dbtools.dbHelper(), infile='tweets.csv'):
2017-01-27 21:02:53 +00:00
"""
Initializes the database.
:param dbo: Database object for the database to be initialized.
:param infile: Path of the CSV file to initalize the database with.
:return: Returns nothing.
"""
2015-10-10 22:10:57 +00:00
try:
infile = open(infile)
2015-10-10 22:10:57 +00:00
except IOError:
raise IOError("Unable to read %s." % infile)
2015-10-10 22:10:57 +00:00
infile = list(csv.reader(infile))
2015-10-10 22:10:57 +00:00
for row in infile[1:]:
try:
dbo.executeQuery("INSERT INTO tweets VALUES(" + row[0].replace("'","''") + ",'" + row[1].replace("'","''") + "','" + row[2].replace("'","''") + "','" + row[3].replace("'","''") + "','" + row[4].replace("'","''") + "','" + row[5].replace("'","''") + "','" + row[6].replace("'","''") + "','" + row[7].replace("'","''") + "','" + row[8].replace("'","''") + "','" + row[9].replace("'","''") + "');")
except:
pass
2015-10-10 22:10:57 +00:00
2016-04-07 22:23:04 +00:00
dbo.commit()
2015-10-10 22:10:57 +00:00
if __name__ == "__main__":
makeDB()