• Wiki
  • Delphi SQL, SQL Lite

Delphi SQL, SQL Lite

How To Use TQuery Component

Publish date:
Discover more of what matters to you

Overview

Delphi provides powerful tools for interacting with databases through its SQL language. This article will show you two helpful tools, TQuery and TTable, that let you manage and change information stored in databases. We’ll explain what they do, how they work with different databases, and when to use each one. By the end, you’ll know the exact tool to use for your next project!

Delphi TQuery Component

The TQuery component in Delphi acts as a bridge between your application and a database. This is done using SQL statements. It allows you to:

  • Retrieve data: You can construct SQL SELECT queries to fetch specific data from tables.
  • Manipulate data: TQuery enables editing existing data or deleting unwanted records using UPDATE and DELETE statements.
  • Navigate data: The component offers functionalities to move through the retrieved data set.

TQuery works seamlessly with various database management systems (DBMS) supported by Delphi, including native access methods like InterBase or external connections through BDE or data providers like Delphi SQL Lite.TQuery datasets are typically read-only by default. However, you can enable live updates for specific scenarios.

Work with Windows Registry via Embarcadero Delphi VCL App
Check out our article
Check out

TQuery Examples

Here are some practical examples demonstrating how to use TQuery for common database operations:

Creating a Database (using Delphi SQL commands within your application):

123456789
procedure CreateDatabase;
begin
SQL := 'CREATE DATABASE MyDatabase';
try
Query1.ExecSQL;
except
ShowMessage('Error creating database: ' + TSQLError(ExceptionInfo).Message);
end;
end;

Inserting Data:

12345678910111213
procedure InsertData;
begin
SQL.Clear;
SQL.Add('INSERT INTO Customers (Name, Address, City) VALUES (:Name, :Address, :City)');
Query1.Params.ParamByName('Name').AsString := 'John Doe';
Query1.Params.ParamByName('Address').AsString := '123 Main St';
Query1.Params.ParamByName('City').AsString := 'Anytown';
try
Query1.ExecSQL;
except
ShowMessage('Error inserting data: ' + TSQLError(ExceptionInfo).Message);
end;
end;

Deleting Data:

12345678910111213
procedure InsertData;
begin
SQL.Clear;
SQL.Add('INSERT INTO Customers (Name, Address, City) VALUES (:Name, :Address, :City)');
Query1.Params.ParamByName('Name').AsString := 'John Doe';
Query1.Params.ParamByName('Address').AsString := '123 Main St';
Query1.Params.ParamByName('City').AsString := 'Anytown';
try
Query1.ExecSQL;
except
ShowMessage('Error inserting data: ' + TSQLError(ExceptionInfo).Message);
end;
end;

Editing Data:

12345678910111213
procedure InsertData;
begin
SQL.Clear;
SQL.Add('INSERT INTO Customers (Name, Address, City) VALUES (:Name, :Address, :City)');
Query1.Params.ParamByName('Name').AsString := 'John Doe';
Query1.Params.ParamByName('Address').AsString := '123 Main St';
Query1.Params.ParamByName('City').AsString := 'Anytown';
try
Query1.ExecSQL;
except
ShowMessage('Error inserting data: ' + TSQLError(ExceptionInfo).Message);
end;
end;

These examples above provide practical usage of TQuery components in Delphi for database operations. This includes snippets for creating a database, inserting data, deleting data, and editing data. These sample codes show how Delphi SQL commands can be used within an application and to help with error handling to control any issue that might arise during the running of the application.

TTable vs TTable: Choosing the Right Tool

Is there an ideal tool to facilitate the Delphi migration process?
Check out our article
Check out

What is TTable?

The TTable component in Delphi also represents a dataset but functions differently from TQuery. It directly connects to a database table, allowing you to:

  • Browse and edit data: You can navigate through records, modify existing data, and add new entries directly to the table.
  • Real-time updates: Changes made through the TTable component are reflected immediately in the database.

This makes TTable a straightforward way to interact with a single database table. The ability of TTable to directly connect and update tables in real-time makes it a great tool for applications that need frequent data manipulation and feedback. 

Can TTable be used as an alternative to TQuery?

While both components work with databases, they cater to different use cases:

  • Use TQuery when you need to perform complex data retrievals involving joins or filtering from multiple tables. It’s also suitable for scenarios where you don’t require real-time updates to the database.
  • Use TTable for simpler operations like browsing existing data or directly modifying a single table. It’s ideal when real-time data synchronization is crucial.

It is clear that both TQuery and TTable serve different purposes in database interactions. TQuery does well with complex queries and multi-table operations, while TTable excels with real-time modifications of databases in a single table. 

Comparison Table

There are various features that distinguish TTable and TQuery, including their data sources, methods of data manipulation, and modes of update. Here is a table describing the differences between these features.

FeatureTQueryTTable
Data SourceSQL statementSingle database table
Data ManipulationLimited (mainly SELECT statements)Full editing capabilities (INSERT, UPDATE, DELETE)
Update ModeRead-only by default (live updates can be enabled)Real

Both components offer distinct approaches to database interactions, thereby making them valuable for different database operations and for different needs. 

Conclusion

In conclusion, TQuery and TTable are both valuable components in a Delphi developer’s toolkit when working with databases. Understanding their strengths and weaknesses allows you to make informed decisions.

  • If your application requires complex data retrieval or manipulation involving multiple tables, TQuery is the way to go.
  • For scenarios where real-time data editing and browsing within a single table are essential, TTable is your champion.

Whatever the database operation you are looking to work on, getting a good grasp of the right components for your database interactions could be the next step to your Delphi SQL and Delphi SQL Lite development success. 

Softacom Delphi software development services help plan, design, develop, integrate, test, manage and evolve software solutions
Delphi engineers fully engaged in your project
Check it out

Subscribe to our newsletter and get amazing content right in your inbox.

This field is required
This field is required Invalid email address
By submitting data, I agree to the Privacy Policy

Thank you for subscribing!
See you soon... in your inbox!

confirm your subscription, make sure to check your promotions/spam folder

Subscribe to our newsletter and get amazing content right in your inbox.

You can unsubscribe from the newsletter at any time

This field is required
This field is required Invalid email address

You're almost there...

A confirmation was sent to your email

confirm your subscription, make sure to check
your promotions/spam folder