Lesson 3: ODBC Tutorials with Delphi
Show this topic in Library frames
Connecting to a database
- Place a TDatabase [optional], a TTable or TQuery, and a TDataSource on a form
To place a TDatabase object on the form and set properties - Delphi
- Place the component on the form.
- Set the AliasName property to the ODBC DSN.
- Set the DatabaseName property to something mnemonic.
- Set the LoginPrompt property as desired.
To place a TQuery on a form and set properties
- Place the component on the form.
- Set the DatabaseName property to the name given to the TDatabase.
- Set the SQL property to a valid SQL statement, with or without parameters.
- If SQL contains parameters, set the Params strings to supply.
- Set the Active property to True to execute the query at design time.at design time.
- Set the TDatabase's properties, set Connected to True;
- Set the properties of the TTable or TQuery; set Active to True;
- Set the TDataSource to connect to the TTable or TQuery
- To avoid making an automatic connection at program startup, leave the TTable/TQuery's Active property set to False, and change it to True in code at run time when the connection is desired.
Running Queries using SQL
- Using a TQuery component with an optional TDatabase and a TDatasource, set the TQuery's SQL property to a valid SQL statement that expresses the query.
- Set the TQuery's Active property to True to execute the query at design time, or close and open the TQuery at run time.
Example
begin
TitleQuery.Active := FALSE;
TitleQuery.SQL.Clear;
if IDRadioButton.Checked then begin
TitleQuery.SQL.Add('select a.*, b.Description from
Titles a, Categories b ');
TitleQuery.SQL.Add('where CategoryID = Category ');
TitleQuery.SQL.Add('order by a.TitleID');
end else begin
TitleQuery.SQL.Add('select a.*, b.Description from
Titles a, Categories b ');
TitleQuery.SQL.Add('where CategoryID = Category ');
TitleQuery.SQL.Add('order by a.Title');
end;
TitleQuery.Active := TRUE;
end;
Binding to controls
Query1.Params[1] := 'Clyde';
Query1.Params[2] := '1234 First Street';
Query1.Params[3] := '';
Query1.Params[4] := 'Austin';
Query1.Params[5] := 'TX';
Query1.Params[6] := '78743';
Query1.Params[7] := '512-555-1234';
Query1.ExecSQL;
Updating with SQL
- Use a TQuery component.
- Set the SQL property to the update statement.
- Call the ExecSQL method.
Example
TitleUpdateQuery.SQL.Clear;
TitleUpdateQuery.SQL.Add('UPDATE titles ');
TitleUpdateQuery.SQL.Add('set TitleID = ' +
intToStr(Titles.TitleQuery.FieldValues['TitleID']) + ',
');
TitleUpdateQuery.SQL.Add('Title = ''' + TitleEdit.Text
+ ''', ');
TitleUpdateQuery.SQL.Add('Category = ' +
intToStr(CategoryDBComboBox.ItemIndex + 1) + ', ');
TitleUpdateQuery.SQL.Add('Price = ' + PriceEdit.Text +
', ');
TitleUpdateQuery.SQL.Add('Term = ' + TermEdit.Text + '
');
TitleUpdateQuery.SQL.Add('where TitleID = ' +
intToStr(Titles.TitleQuery.FieldValues['TitleID']) + '
');
TitleUpdateQuery.ExecSQL;
Inserting using SQL
- Place a TQuery component on the form.
- Set the SQL property to the insert statement.
- Call the TQuery's ExecSQL method.
Example
TitleUpdateQuery.SQL.Clear;
TitleUpdateQuery.SQL.Add('INSERT into titles values (');
TitleUpdateQuery.SQL.Add('0, ''' + TitleEdit.Text + ''',
');
TitleUpdateQuery.SQL.Add(intToStr(CategoryDBComboBox.It
emIndex + 1) + ', ');
TitleUpdateQuery.SQL.Add(PriceEdit.Text + ', ');
TitleUpdateQuery.SQL.Add(TermEdit.Text + ' )');
TitleUpdateQuery.ExecSQL;
Using Parameterized Queries
- You must use a TQuery component.
- Set the SQL property to a statement with parameters expressed as :Param, each beginning with a colon.
- In the Params property editor, supply a Type and default value for each parameter named in the SQL statement.
- At run time, set the Params property in code:
Example
Query1.Close;
Query1.SQL.Add('select * from customers ');
Query1.SQL.Add('where LastName = :LName and FirstName =
:FName');
[ . . . ]
Query1.Params[0] := 'Henderson';
Query1.Params[1] := 'Clyde';
Query1.Open;
Using Parameterized Updates
- You must use a TQuery component.
- Set the SQL property to an Update statement with parameters expressed as :Param, each beginning with a colon.
- In the Params property editor, supply a Type and default value for each parameter named in the SQL statement.
- At run time, set the Params property in code:
Example
Query1.Close;
Query1.SQL.Clear;
Query1.SQL.Add('update customers ');
Query1.SQL.Add('set LastName = :LName, FirstName =
:FName ');
Query1.SQL.Add('where CustID = :CustID');
[ . . . ]
Query1.Params[0] := 'Henderson';
Query1.Params[1] := 'Clyde';
Query1.Params[2] := iCustID;
Query1.ExecSQL;
Using Parameterized Inserts
- Place a TQuery component on the form.
- Set the SQL property to an Insert statement with parameters expressed as :Param, each beginning with a colon.
- In the Params property editor, supply a Type and default value for each parameter named in the SQL statement.
- At run time, set the Params property in code:
Example
Query1.Close;
Query1.SQL.Clear;
Query1.SQL.Add('insert into customers ');
Query1.SQL.Add('values (0, :LName, :FName, :Address1,
:Address2, ');
Query1.SQL.Add(':City, :State, :Zip, :Phone));
[ . . . ]
Query1.Params[0] := 'Henderson';