-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimport_csv_sql.py
48 lines (33 loc) · 1.15 KB
/
import_csv_sql.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# convert csv to sql table
import sqlite3, csv
connection = sqlite3.connect("reddit_politics.db")
cursor = connection.cursor()
cursor.execute(
"""
CREATE TABLE reddit_posts(POST_NUMBER PRIMARY KEY,
Title STR,
Body STR,
Karma INT,
DateandTime DATE,
Subreddit STR,
Post_Author STR,
Submission Link STR,
Submission_URL STR,
Submission_ID STR,
Top_Comment STR,
Top_Comment_Author STR,
Top_Comment_Upvotes INT,
Datetime_Collected DATE,
Sort);
"""
)
with open ('pol_reddit_posts.csv', 'r') as politics_table:
dr = csv.DictReader(politics_table)
to_db = [(i['POST_NUMBER'],i['Title'],i['Body'],i['Karma'], i['Date and Time'], i['Subreddit'], i['Post Author'], i['Submission Link'], i['Submission URL'], i['Submission ID'], i['Top Comment'], i['Top Comment Author'], i['Top Comment Upvotes'], i['Datetime Collected'], i['Sort']) for i in dr]
cursor.executemany("INSERT INTO reddit_posts VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);", to_db)
connection.commit()
cursor.execute("SELECT * FROM reddit_posts")
print('fetchall: ')
result = cursor.fetchall()
for r in result:
print(r)