-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwitter_db_utils.py
More file actions
35 lines (31 loc) · 1.22 KB
/
twitter_db_utils.py
File metadata and controls
35 lines (31 loc) · 1.22 KB
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
from python_utils import sqlite_utils as dbx
def insert_tweets(df, db, table):
if len(df) > 0:
wildcards = ','.join(['?'] * 16)
data = []
for ix in df.to_records(index=False):
data.append((
str(ix['created_at']),
str(ix['favorites']),
str(ix['hashtags']),
str([i.encode('utf-8') for i in ix['links']]),
str(ix['mentions']),
str(ix['reply']),
str(ix['replying']),
str(ix['retweets']),
str(ix['text'].encode('utf-8')),
str(ix['tweet_id']),
str(ix['user_id']),
str(ix['user_name'].encode('utf-8')),
str(ix['user_screen_name']),
'NaN',
'NaN',
'NaN'
))
conn = dbx.connect_db(db)
c = conn.cursor()
c.executemany("INSERT OR IGNORE INTO %s VALUES(%s)" % (table, wildcards), data)
conn.commit()
conn.close()
else:
print('No records to insert.')