Documentation

Working with records

Home · Русский

Inserting records

insert(table_name, **values) inserts a record. Pass field values as keyword arguments.

player_id = db.insert("players", login="ada", level=3)

The method returns lastrowid - the identifier of the last inserted row (int or None). If no fields are supplied, it inserts a row using default values.

Reading records

Method Result
select(table_name, query="", *args) A list of dictionaries; [] if there are no matches.
select_one(table_name, query="", *args) One matching record as a dictionary, or None.
count(table_name, query="", *args) The number of matching records (int).
players = db.select("players")
player = db.select_one("players", "login = ?", "ada")
total = db.count("players", "level >= ?", 5)

Without a condition, read methods operate on all rows in the table. To retrieve a specific record, use a condition on a primary key or unique field: without an explicit order, SQL does not guarantee which of several matching rows will be returned first.

Search conditions

Pass an SQL expression in query without the WHERE keyword, followed by positional arguments for the ? placeholders.

db.select("players", "level BETWEEN ? AND ?", 3, 10)
db.select_one("players", "login = ? AND level >= ?", "ada", 5)

Do not insert user-supplied values directly into an SQL string; pass them through ? parameters. The SQL condition itself must come from trusted code.

In flexible tables, names of additional top-level fields are automatically translated into JSON lookups.

Updating records

update(table_name, query, *args, **values) changes the specified fields in matching records.

changed = db.update("players", "login = ?", "ada", level=10)

The method returns the number of affected rows, or 0 if no rows match. You must provide at least one new value and a non-empty condition.

Deleting records

delete(table_name, query, *args) deletes matching rows and returns their number.

removed = db.delete("players", "login = ?", "ada")

A non-empty condition is required.

Updating or deleting all records

Use update_all(table_name, **values) or delete_all(table_name) to target every row. Both methods return the number of affected rows. update_all() requires at least one new value.

changed = db.update_all("players", level=0)
removed = db.delete_all("players")

Errors and limitations

Writing to an unknown column in a regular table raises KeyError. In a table with flexible=True, additional fields are stored as JSON - see Flexible fields.

SQLite constraints (UNIQUE, NOT NULL, PRIMARY KEY, and others) still apply. For example, violating a uniqueness constraint raises sqlite3.IntegrityError.

If several records must be changed as a single unit, use a transaction.