all Python 3sqlite3term
sqlite3
module
The embedded SQLite database in a single file or in memory, via the DB-API 2.0 interface, no server.
Syntax
import sqlite3
sqlite3 fits when you need a real SQL database without a separate server: local app storage, a cache, tests against ':memory:'. Pass values through ? placeholders — building SQL with f-strings invites injection. A common mistake is forgetting commit: without it changes are lost when the connection closes. Many concurrent writers call for a server database instead.
Examples
import sqlite3
con = sqlite3.connect(':memory:')
con.execute('CREATE TABLE users (name TEXT, age INTEGER)')
con.executemany('INSERT INTO users VALUES (?, ?)', [('Ann', 30), ('Bob', 25)])
print(con.execute('SELECT name FROM users WHERE age > ?', (26,)).fetchall()) # → [('Ann',)]
con.close()
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy