PreviousTutorials and Guide to Samples (9.1 revision 1) Next

Lesson 3: ODBC Tutorials with Delphi

Show this topic in Library frames

Connecting to a database

  1. Place a TDatabase [optional], a TTable or TQuery, and a TDataSource on a form
  2. To place a TDatabase object on the form and set properties - Delphi

    1. Place the component on the form.
    2. Set the AliasName property to the ODBC DSN.
    3. Set the DatabaseName property to something mnemonic.
    4. Set the LoginPrompt property as desired.

To place a TQuery on a form and set properties

    1. Place the component on the form.
    2. Set the DatabaseName property to the name given to the TDatabase.
    3. Set the SQL property to a valid SQL statement, with or without parameters.
    4. If SQL contains parameters, set the Params strings to supply.
    5. Set the Active property to True to execute the query at design time.at design time.
  1. Set the TDatabase's properties, set Connected to True;
  2. Set the properties of the TTable or TQuery; set Active to True;
  3. Set the TDataSource to connect to the TTable or TQuery
  4. 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

  1. 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.
  2. 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

  1. Use a TQuery component.
  2. Set the SQL property to the update statement.
  3. 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

  1. Place a TQuery component on the form.
  2. Set the SQL property to the insert statement.
  3. 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

  1. You must use a TQuery component.
  2. Set the SQL property to a statement with parameters expressed as :Param, each beginning with a colon.
  3. In the Params property editor, supply a Type and default value for each parameter named in the SQL statement.
  4. 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

  1. You must use a TQuery component.
  2. Set the SQL property to an Update statement with parameters expressed as :Param, each beginning with a colon.
  3. In the Params property editor, supply a Type and default value for each parameter named in the SQL statement.
  4. 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

  1. Place a TQuery component on the form.
  2. Set the SQL property to an Insert statement with parameters expressed as :Param, each beginning with a colon.
  3. In the Params property editor, supply a Type and default value for each parameter named in the SQL statement.
  4. 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'; 

Chapter contents
Publication contents

Prev topic: Lesson 2: Btrieve API Tutorials with Delphi
Next topic: Advanced Pervasive.SQL Tutorials Using Visual Basic