Python sqlite3

From wikinotes

sqlite is an embedded database that writes to a file rather than a server.
You can also write to memory.

Documentation

official docs https://docs.python.org/3/library/sqlite3.html?highlight=sqlite3#module-sqlite3

Connection

import sqlite3

conn = sqlite3.connect(':memory:')            # Connect/Create database in memory
conn = sqlite3.connect('/home/will/test.db')  # Connect/Create database on filesystem

conn.row_factory = sqlite3.Row

Cursor

cursor = conn.cursor()
cursor.execute('CREATE TABLE test (name TEXT, data TEXT)')
conn.commit()
conn.close()

By default cursors are a tuple.
If you'd like a dict, use the sqlite3.Row row factory.

conn.row_factory = sqlite3.Row

cursor = conn.cursor()
cursor.execute('SELECT * FROM table')
row = cursor.fetchone()

tuple(row)  # (1, 2, 3)
dict(row)   # {'a': 1, 'b': 2, 'c': 3}
row[0] == row['a'] == 1