Python Glossary Русский

all Python 3sqlite3function

sqlite3.connect()

connections and queries

Opens an SQLite database and returns a Connection. The string ':memory:' creates a temporary in-memory database that vanishes when closed.

Syntax

sqlite3.connect(database, *, timeout=5.0, detect_types=0, autocommit=...)

Every connect(':memory:') is a separate empty database: a table created through one connection is invisible to another. connect silently creates a missing file, so a typo in the path gives an empty database and then no such table. Pass every parameter except database by keyword: a positional timeout warns with DeprecationWarning since 3.13 and is a TypeError from 3.15. Close the connection with close(), not with.

Examples

import sqlite3
a = sqlite3.connect(':memory:')
b = sqlite3.connect(':memory:')
a.execute('CREATE TABLE t(x)')
try:
    b.execute('SELECT * FROM t')
except sqlite3.OperationalError as e:
    print(e)  # → no such table: t — у b своя база
a.close()
b.close()
# Python 3.15+
import sqlite3
try:
    sqlite3.connect(':memory:', 5.0)  # timeout позиционно
except TypeError as e:
    print(type(e).__name__)  # → TypeError
sqlite3.connect(':memory:', timeout=5.0).close()  # так правильно

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy