· 8 years ago · Nov 28, 2017, 11:30 PM
1import sqlite3
2
3db = sqlite3.connect("student.db")
4#cursor allows me to send SQL statements
5cursor = db.cursor()
6sql = '''CREATE TABLE IF NOT EXISTS students (id INTEGER PRIMARY KEY, first TEXT, last TEXT, gradyear INTEGER)'''
7cursor.execute(sql)
8
9def displaydata():
10 sql = '''SELECT * FROM students'''
11 cursor.execute(sql)
12 results = cursor.fetchall()
13 for result in results:
14 print("First:",result[1],"Last:",result[2],result[3])
15
16sql = '''INSERT INTO students (first, last, gradyear) VALUES ("Samantha","Daws",2018)'''
17cursor.execute(sql)
18
19# sql = '''DELETE FROM students WHERE last="Katz"'''
20# cursor.execute(sql)
21sql = '''SELECT * FROM students'''
22displaydata()
23sql = '''UPDATE students SET gradyear=2019 WHERE first="George"'''
24cursor.execute(sql)
25displaydata()
26
27db.commit()
28db.close()