2015-04-13 20:58:32 +00:00
#!/usr/bin/env python3
2015-04-12 23:58:32 +00:00
2016-04-07 22:23:04 +00:00
import dbtools
2015-04-13 00:14:34 +00:00
2016-02-08 00:55:12 +00:00
import sqlite3 , csv , sys
2015-04-12 23:58:32 +00:00
2016-04-07 22:23:04 +00:00
def makeDB ( dbo = dbtools . dbObject ( ) , infile = ' tweets.csv ' ) :
2015-10-10 22:10:57 +00:00
try :
2016-04-07 19:04:13 +00:00
infile = open ( infile )
2015-10-10 22:10:57 +00:00
except IOError :
2016-04-07 19:04:13 +00:00
raise IOError ( " Unable to read %s . " % infile )
2015-10-10 22:10:57 +00:00
input = list ( csv . reader ( infile ) )
try :
2016-04-07 22:23:04 +00:00
dbo . executeQuery ( " CREATE TABLE tweets(`tweet_id` INTEGER NOT NULL, `in_reply_to_status_id` TEXT, `in_reply_to_user_id` TEXT, `timestamp` TEXT, `source` TEXT, `text` TEXT, `retweeted_status_id` TEXT, `retweeted_status_user_id` TEXT, `retweeted_status_timestamp` TEXT, `expanded_urls` TEXT, PRIMARY KEY(tweet_id)); " )
except :
2015-10-10 22:10:57 +00:00
raise IOError ( " %s already exists. Please delete it before trying to create a new one. " % path )
for row in input [ 1 : ] :
2016-04-07 22:23:04 +00:00
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 ( " ' " , " ' ' " ) + " ' ); " )
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__ " :
if len ( sys . argv ) > 2 :
raise ValueError ( sys . argv [ 0 ] + " only takes one argument, the path of the new database file. " )
try :
makeDB ( sys . argv [ 1 ] )
except IndexError :
makeDB ( )
2015-04-12 23:58:32 +00:00