Tweet RSS

 Example Delphi Using Variables in SQL:

Div

 Example SELECT:

procedure TFrmExemplar.mnuDispIPRangeClick(Sender: TObject);
var
  Temp, MySQL, Input: String; //The variable (Input) to be used in the SQL statement. 
begin
  RchOut.Clear;
  Temp:= 'Country Code:'+#9+'IP Bottom:'+#9+'IP Top:'+#13;
  
  Input:= Uppercase(Inputbox('Please Enter:','Country Code'+#13+'E.G: ZA = South Africa','ZA'));
//This is how the user can define his own variable value (through an input-box)  
  MySQL:= 'SELECT CountryCode, IPBottom, IPTop FROM IPRange WHERE CountryCode LIKE
    "'+Input+'"';  
//Notice how the variable is contained within the " ' & " ' " this can become quite hard 
  Temp:= Temp+(Query(MySQL));
  RchOut.Lines.Add(Temp);
end;
Div

 Example DELETE:

procedure TFrmExemplar.mnuDeleteCountryClick(Sender: TObject);
Var
  Input, MySQL: String; //The variable (Input) to be used in the SQL statement. 
begin  
  Input:= Uppercase(Inputbox('Please Enter:','A Unique Country Code'+#13+'E.G: ZA = South  Africa','ZA'));
//This is how the user can define his own variable value (through an input-box)  
  MySQL:= 'DELETE FROM IPRange WHERE CountryCode LIKE "'+Input+'"';
//Notice how the variable is contained within the " ' & " ' " this can become quite hard 
  Change(MySQL);
//Because we are only DELETING records we only need to call the Change procedure as their is no return value. 
  Showmessage('Country Code: '+Input+' DELETED!!');
end; 

 Linking Databases to Delphi:

<< Previous Tutorial

div

Current Tutorial:

</> Download Source Code

Div

Current Tutorial Info:

Us variables in SQL is a vital part when extracting data out of a database. By using variables the user can assign his own SELECT<Field>, FROM <Table> and WHERE<Condition>, etc. It is almost impossible to DELETE/ INSERT/UPDATE a record from a database or without using variables in SQL.

However using variables in SQL within Delphi become quite tricky due to the multiple " " & ' '.   

Div

Current Tutorial Principle:

Whether you are using a variable in the WHRE <Condition>, SELECT <Field> or the FROM <Table>. This basic principle of how to use the variable remains the same.