Chapter 31 Key Takeaways

  1. Databases replace flat files for applications with growing data, complex queries, integrity requirements, and relationships between data types.

  2. SQLite is an embedded, serverless database in a single file — no installation, no configuration, ideal for desktop apps.

  3. The SQLdb component chain: TSQLite3Connection (database) > TSQLTransaction (safety) > TSQLQuery (SQL execution) > TDataSource (bridge) > TDBGrid (display).

  4. Use Query.Open for SELECT (returns rows to navigate), Query.ExecSQL for INSERT/UPDATE/DELETE/CREATE (modifies data, no result set).

  5. Always commit transactions. Changes are not permanent until Transaction.Commit is called. Use Rollback in exception handlers.

  6. Data-aware controls (TDBGrid, TDBEdit, TDBMemo, TDBNavigator) bind to TDataSource and automatically display/edit the current record.

  7. ALWAYS use parameterized queries. Never concatenate user input into SQL strings. Use :param placeholders and Params.ParamByName. This prevents SQL injection.

  8. Switching databases (SQLite to PostgreSQL to MySQL) requires only changing the connection component. All other SQLdb code remains identical.

  9. Database design: Use primary keys for identity, foreign keys for relationships, NOT NULL for required fields, UNIQUE for unique constraints. Normalize to eliminate redundancy.

  10. SQL is a declarative language — you describe what you want, not how to get it. SELECT * FROM expenses WHERE category = 'Food' ORDER BY date DESC replaces dozens of lines of procedural code.