From 284e64daee4666b412fe0f93697abafc31267329 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Mon, 13 Apr 2015 01:58:32 +0200 Subject: [PATCH] Add error handling to filler.py, allowing empty databases to be filled Initial (non-functional) check-in of the CSV to sqlite3 converter --- csvdb.py | 21 +++++++++++++++++++++ filler.py | 11 ++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) create mode 100755 csvdb.py diff --git a/csvdb.py b/csvdb.py new file mode 100755 index 0000000..5385acd --- /dev/null +++ b/csvdb.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python + +import sqlite3, csv + +try: + infile = open('tweets.csv') +except IOError: + print "Please make sure that the tweets.csv from the Twitter download is located in this directory." + +input = csv.reader(infile) + +conn = sqlite3.connect('Database.db') +cur = conn.cursor() + +try: + cur.execute("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 sqlite3.OperationalError: + print "Database.db already exists. Please delete it before trying to create a new one." + +for row in input[1:]: + cur.execute("INSERT INTO tweets VALUES(?,?,?,?,?,?,?,?,?,?);",row) diff --git a/filler.py b/filler.py index 03f91b7..3f8e9a2 100755 --- a/filler.py +++ b/filler.py @@ -16,10 +16,15 @@ database_filename = "Database.db" sql_conn = sqlite3.connect(database_filename) cur = sql_conn.cursor() -cur.execute("SELECT max(tweet_id) FROM tweets") -savepoint = int(cur.fetchone()[0]) +try: + cur.execute("SELECT max(tweet_id) FROM tweets") +except sqlite3.OperationalError: + print "Please run ./makedb.py or ./csvdb.py before trying to populate the database." -print savepoint +try: + savepoint = int(cur.fetchone()[0]) +except: + savepoint = 0 auth = tweepy.OAuthHandler(cke, cse) auth.set_access_token(ato, ase)