forked from justmarkham/DAT4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
17_sql.py
36 lines (24 loc) · 858 Bytes
/
17_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
import sqlite3 as lite
# python package to interface with database files
# connect to a local database
con = lite.connect('../data/tweets.db')
# create a Cursor object
cur = con.cursor()
# select everything from tweets
cur.execute('SELECT * from Tweets')
cur.fetchall()
# insert a record
cur.execute("INSERT INTO Tweets VALUES(9,'Investors are claiming that $TSLA will only continue to fall!', -.67)")
# need ot commit to make sure that all changes are done
con.commit()
# select only a few columns
cur.execute('SELECT Text, Id from Tweets')
cur.fetchall()
# select with an id
cur.execute('SELECT Text, Sentiment from Tweets WHERE Id = 5')
cur.fetchone()
# grab all tweets with negative sentiment
cur.execute('SELECT Text, Sentiment from Tweets WHERE Sentiment < 0')
cur.fetchall()
# close the connection if we are done with it.
con.close()