Entry
Delphi: Database: Sql: Query: Create: Simple: How to create a simple SQL query in Borland Delphi?
Aug 22nd, 2003 17:28
Knud van Eeden,
----------------------------------------------------------------------
--- Knud van Eeden - 23 August 2003 - 01:55 am -----------------------
Delphi: Database: Sql: Query: Create: Simple: How to create a simple
SQL query in Borland Delphi?
Steps: Overview:
1. Running your SQL query via component property 'SQL'
2. Running your SQL query programmatically via Delphi source code
---
Steps: Worked out:
1. Running your SQL query via component property 'SQL':
1.1. put a component 'TQuery' on the form (from palette 'BDE')
-double click on the property 'SQL', and fill in your SQL query
e.g.
SELECT COUNT( * ) FROM mytablename;
-set property 'Active' = true
-set property 'DatabaseName' to one of your aliases for databases
(e.g. 'mydatabasealias')
http://www.faqts.com/knowledge_base/view.phtml/aid/23861/fid/175
1.2. put a component 'TDataSource' on the form (from palette 'Data
Access')
-set property 'DataSet' = Query1
1.3. Put a component 'dBGrid' on the form (from palette 'Data
Controls')
-set property 'DataSource' = datasource1
1.4. Put a component 'dBNavigator' on the form (from palette 'Data
Controls')
-set property 'DataSource' = datasource1
---
This could for example show the output from applying this given SQL
query to
the given database table, in the dbgrid:
+---------------------------------------------------+
|count (*) | |
|----------| |
|1 | |
|----------+ |
+---------------------------------------------------+
---
---
Note:
To run another query, you can change the text in the property 'Sql' on
the component.
For example change this in:
SELECT * FROM mytablename;
---
---
2. Running your SQL query programmatically via Delphi source code:
If you want to input the SQL query via your Delphi source code,
you could for example put a button on your form:
2.1 Double click on this button, and put in there the following
text:
procedure TForm1.Button1Click(Sender: TObject);
var s : string;
begin
s := 'SELECT COUNT( * ) FROM mytablename;';
Query1.Active := false;
Query1.Sql.Clear;
Query1.Sql.Text := s;
Query1.ExecSQL;
Query1.Active := true;
end;
2.2 Run and compile this, then press the button.
---
This could for example show the output from applying this given SQL
query to
the given database table, in the dbgrid:
+---------------------------------------------------+
|count (*) | |
|----------| |
|1 | |
|----------+ |
+---------------------------------------------------+
----------------------------------------------------------------------