· 8 years ago · Aug 07, 2018, 12:38 AM
1import sqlite3
2
3# open connection and get a cursor
4conn = sqlite3.connect(':memory:')
5c = conn.cursor()
6
7# create schema for a new table
8c.execute('CREATE TABLE IF NOT EXISTS sometable (name, age INTEGER)')
9conn.commit()
10
11# insert a new row
12c.execute('INSERT INTO sometable values (?, ?) ', ('John Doe', 37))
13conn.commit()
14
15# extend schema during runtime
16c.execute('ALTER TABLE sometable ADD COLUMN gender TEXT')
17conn.commit()
18
19# add another row
20c.execute('INSERT INTO sometable values (?, ?, ?) ', ('Jane Doe', 34, 'female'))
21conn.commit()
22
23# get a single row
24c.execute('SELECT name, age FROM sometable WHERE name = ?', ('John Doe', ))
25row = list(c)[0]
26john = dict(name=row[0], age=row[1])