Databases
A database (.db, .sqlite, .sqlite3 or .db3) is where a game
keeps lots of records: every high score, a shop’s items, a saved game.
Databases in Making Games with Crash is about using one from your game.
The database editor is for looking inside it and changing it by hand: the
items a shop starts with, a table for a new kind of record, a score that
shouldn’t be there.
Right-click in the file list and pick New database… for an empty one, or open any database in the project. The editor reads it with the same SQLite your game uses, so what you see is what the game will find.
Around the database editor
- On the left, the database’s tables and views. New table… makes a table. Right-click one to edit it or delete it.
- Along the top, three pages: Rows, Structure and SQL, and Undo and Redo.
Nothing goes into the file until you save it, with Save in the bar, or Cmd+S (Ctrl+S on Windows and Linux), which saves every changed file. Until then the tab’s name has a dot beside it, and Undo goes back over everything you’ve done.
Rows
Rows shows the picked table’s rows, 200 at a time: ◀ and ▶ go through them, and the count says how many there are. Click a column’s name to sort by it (again to sort the other way, and a third time to stop).
- Double-click a cell to type into it: what’s there is picked, so typing replaces it. Enter puts it in, and Esc leaves it as it was. A number column only takes numbers: type a word into one and the editor says so, rather than keep it for your game to trip over. Right-click a cell for Set to NULL (nothing at all, not even 0), Copy and Edit.
- Click a row to pick it. Shift-click picks every row from the last one up to this one, and Command-click (Ctrl-click) picks one more. Duplicate copies the picked rows, and Delete (or the Delete key) deletes them.
- Add row… asks what each column of the new row holds: its default,
NULL, or a value you type. An
INTEGER PRIMARY KEYis numbered for you if you leave it. - Filter shows only some rows. Type what comes after
WHEREin SQL, likepoints > 1000orname LIKE 'A%', and press Enter. Clear shows them all again.
A view’s rows are only read: a view is a question asked of other tables, so change the tables instead. A BLOB (bytes, like a picture) shows how big it is, and can’t be typed into.
Structure
Structure shows the table’s columns, and the SQL that makes it. Edit table… (or New table…) opens the table’s form:
| Column | its name |
| Type | INTEGER (whole numbers), REAL (numbers with a point), TEXT, BLOB or NUMERIC. ▾ picks one, or type any other |
| Primary key | what tells the rows apart: no two rows share it. Tick it on more than one column for a key of several columns together |
| Counts up | AUTOINCREMENT, for an INTEGER primary key: a number is never given out twice, even after its row is deleted. Leave it off and rows are still numbered for you |
| Required | NOT NULL: every row must have one |
| Unique | no two rows have the same |
| Default | what a new row gets when it isn’t given one: a number, words (no quotes needed), NULL, CURRENT_TIMESTAMP, or SQL in brackets |
↑ and ↓ move a column, ✕ removes it (and what it holds), and
Add column adds one. Without rowid and Strict are SQLite’s
WITHOUT ROWID and STRICT, for when you know you want them.
Under the form, Applying runs shows the SQL the change takes, before anything happens, with anything you should know (a column going, and its index with it). Apply runs it all. If any of it fails, none of it is kept, and the editor says why: a column made Unique where two rows already share a value, say, or a Required column added to a table with rows and no Default to fill them with.
Renaming the table, renaming columns and adding columns at the end are done
in place. Anything else (removing or moving a column, changing a type or a
tick) builds the table again with the change and copies every row across,
keeping its indexes. A table made with things the form has no place for
(a CHECK, a foreign key, a COLLATE) can still be renamed and given new
columns here; for the rest, use SQL, so nothing is lost by accident.
SQL
SQL runs whatever you type, as many statements as you like: Run (or Cmd+Enter) runs them in order and shows what each one found, or how many rows it changed. They all run or, if one fails, none of them is kept. Undo takes back what SQL changed too.
When the game writes to it
A game that changes its database while it runs writes the file when it closes it. If the database is open in the editor with nothing changed, the editor reads it again. If you’ve changed it too, the editor asks: Read it again (your changes go) or Keep mine (saving puts them over the game’s).
In a browser, a database your game wrote comes straight into the project when the run ends, without asking: see Files the game saves in Running your game.