Chapter 31 Key Takeaways
-
Databases replace flat files for applications with growing data, complex queries, integrity requirements, and relationships between data types.
-
SQLite is an embedded, serverless database in a single file — no installation, no configuration, ideal for desktop apps.
-
The SQLdb component chain: TSQLite3Connection (database) > TSQLTransaction (safety) > TSQLQuery (SQL execution) > TDataSource (bridge) > TDBGrid (display).
-
Use
Query.Openfor SELECT (returns rows to navigate),Query.ExecSQLfor INSERT/UPDATE/DELETE/CREATE (modifies data, no result set). -
Always commit transactions. Changes are not permanent until
Transaction.Commitis called. UseRollbackin exception handlers. -
Data-aware controls (TDBGrid, TDBEdit, TDBMemo, TDBNavigator) bind to TDataSource and automatically display/edit the current record.
-
ALWAYS use parameterized queries. Never concatenate user input into SQL strings. Use
:paramplaceholders andParams.ParamByName. This prevents SQL injection. -
Switching databases (SQLite to PostgreSQL to MySQL) requires only changing the connection component. All other SQLdb code remains identical.
-
Database design: Use primary keys for identity, foreign keys for relationships, NOT NULL for required fields, UNIQUE for unique constraints. Normalize to eliminate redundancy.
-
SQL is a declarative language — you describe what you want, not how to get it.
SELECT * FROM expenses WHERE category = 'Food' ORDER BY date DESCreplaces dozens of lines of procedural code.