Python is often referred to as a language that has batteries included, thanks to its very complete standard library, and one of the best features it provides is reading and writing from a full-featured relational database.
Python ships with the SQLite
library built in, meaning that we can save and read database files stored by SQLite
.
The usage is pretty straightforward and most of it actually just involves sending SQL for execution.
For this recipes, the steps are as follows:
- Using the
sqlite3
module, it's possible to create a new database file, create a table, and insert entries into it:
import sqlite3 with sqlite3.connect('/tmp/test.db') as db: try: db.execute('''CREATE TABLE people ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name TEXT, surname TEXT, language TEXT )''') except sqlite3.OperationalError: # Table already exists pass sql = 'INSERT INTO...