Get csvdb running (inital version)

This commit is contained in:
Klaus-Uwe Mitterer 2015-05-18 13:41:30 +02:00
parent f1ceb138bf
commit 4f1cb5cad1

View file

@ -1,17 +1,15 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
print("This script is not working yet... If you are trying to test it, please delete lines 3 to 5 of this file.") import tools
import sys
sys.exit(0)
import sqlite3, csv import sqlite3, csv
try: try:
infile = open('tweets.csv') infile = open('tweets.csv')
except IOError: except IOError:
print("Please make sure that the tweets.csv from the Twitter download is located in this directory.") raise IOError("Please make sure that the tweets.csv from the Twitter download is located in this directory.")
input = csv.reader(infile) input = list(csv.reader(infile))
conn = sqlite3.connect('Database.db') conn = sqlite3.connect('Database.db')
cur = conn.cursor() cur = conn.cursor()
@ -19,7 +17,9 @@ cur = conn.cursor()
try: 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));") 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: except sqlite3.OperationalError:
print("Database.db already exists. Please delete it before trying to create a new one.") raise IOError("Database.db already exists. Please delete it before trying to create a new one.")
for row in input[1:]: for row in input[1:]:
cur.execute("INSERT INTO tweets VALUES(?,?,?,?,?,?,?,?,?,?);",row) cur.execute("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("'","''") + "');")
conn.commit()