Delphi
FireDAC Database Access
Query SQL databases with FireDAC.
By EZ4Code Team
firedacdatabasesql
Code
uses
FireDAC.Comp.Client, FireDAC.Comp.Dataset, FireDAC.Stan.Param;
var
Conn: TFDConnection;
Query: TFDQuery;
begin
Conn := TFDConnection.Create(nil);
Query := TFDQuery.Create(nil);
try
Conn.DriverName := 'SQLite';
Conn.Params.Database := 'app.db';
Conn.Connected := True;
Query.Connection := Conn;
// Parameterized query (prevents SQL injection)
Query.SQL.Text := 'SELECT * FROM users WHERE age > :min_age ORDER BY name';
Query.ParamByName('min_age').AsInteger := 18;
Query.Open;
// Iterate
while not Query.Eof do
begin
ShowMessage(Query.FieldByName('name').AsString + ': ' +
Query.FieldByName('age').AsString);
Query.Next;
end;
// Execute (no result set)
Query.SQL.Text := 'UPDATE users SET active = 1 WHERE id = :id';
Query.ParamByName('id').AsInteger := 42;
Query.ExecSQL;
finally
Query.Free;
Conn.Free;
end;
end;Explanation
FireDAC is Delphi's modern multi-database framework. Use ParamByName with parameters (never string concatenation — SQL injection risk). Open returns a cursor (call Next to iterate); ExecSQL runs INSERT/UPDATE/DELETE. Always wrap in try/finally to free resources. Connection pooling: use TFDManager for high-throughput apps.
More Delphi Snippets
Units and Classes
Define units with interface and implementation sections.
VCL Form Basics
Create a form with event handlers in Delphi VCL.
Properties and Events
Define properties and event handlers in Delphi.
Generics
Type-safe containers with generics in Delphi.
Interfaces and Reference Counting
Define interfaces with automatic reference counting.
Exception Handling
Try/Except/Finally in Delphi.