-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql.py
More file actions
20 lines (15 loc) · 772 Bytes
/
sql.py
File metadata and controls
20 lines (15 loc) · 772 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# sql.py - Create a SQLite3 table and populate it with data
import sqlite3
# create a new database if the database doesn't already exist
with sqlite3.connect('sample.db') as connection:
# get a cursor object used to execute SQL commands
c = connection.cursor()
c.execute("DROP TABLE posts")
# create the table
c.execute("CREATE TABLE posts(title TEXT, details TEXT)")
# insert dummy data into the table
c.execute('INSERT INTO posts VALUES("Good", "I\'m good.")')
c.execute('INSERT INTO posts VALUES("Well", "I\'m well.")')
c.execute('INSERT INTO posts VALUES("Hello", "Hello from the shell")')
c.execute('INSERT INTO posts VALUES("Excellent", "I\'m excellent.")')
c.execute('INSERT INTO posts VALUES("Okay", "I\'m okay.")')