Benjamin Lu

everyday is cool

Keeping Site Notes in SQLite

Python 2.5 added sqlite3 to the standard library in September 2006. A small SQLite database is enough to try saving website notes without setting up a separate database server. Use Python 3 for this recreation.

import sqlite3

db = sqlite3.connect("notes.db")
db.execute("CREATE TABLE IF NOT EXISTS notes (title TEXT)")
db.execute("INSERT INTO notes (title) VALUES (?)", ("Check old links",))
db.commit()
for (title,) in db.execute("SELECT title FROM notes ORDER BY rowid"):
    print(title)
db.close()

Run the script twice and look for two rows on the second run. They’re still there because the database is a file and each run commits another insert. Try changing the title before the third run. The question mark keeps the SQL statement separate from the value you supply.

What’s New in Python 2.5