2
votes

I have a database and want to select an entry with DELPHI and FIREMONKEY CROSSOVER. But I never worked with SQL on DELPHI. And I cant figure out how to connect to the DB with DELPHI. I dont want a hand full of components. It should be an easy connection, like it is with PHP.

I use XAMPP - mySQL. Let's say we have a database, "db_x", with a tabel "users", in that table there is the following entry: name: Michael; surname: Schneider; age: 22.

The Database is on "localhost" or somewhere else, login is "root" and password is "rootpw". To connect to the DB in PHP we use:

mysql_connect("localhost","root","rootpw") or die ("Connection Error");
mysql_select_db("db_x") or die ("Error DB");

Now I wanna get the surname from a user named "Michael":

$query= mysql_query("SELECT surname FROM users WHERE name = 'Michael'")  
or die  
(mysql_error());   

while($zeile = mysql_fetch_array( $query )) 
{ 
    echo $zeile['surname']."<br>";
}  

Now this is about PHP, but what about DELPHI? I have the same database and want to show the surname of the user but I dont even know how to connect with DELPHI (and it should also support Firemonkey and work on iOS).

Sorry for the grammar misstakes I made.

Thanks for all incoming answers and regards from germany.

2
You shouldn't be using the mysql_* functions, they're deprecated in all but name. Please look into switching to mysqli or PDOGordonM

2 Answers

1
votes

For the sake of completeness on this dinosaur.

I didn't found the function I used back then but it was something similar to this:

http := TIdHttp.Create(nil);
http.HandleRedirects := true;
http.ReadTimeout := 6000;
jsonToSend := TStringList.create;
jsonToSend.Text := 'json={"food":"Pizza"}';
Memo1.Lines.Text := http.Post('http://www.mypage.com/mydata.php', jsonToSend);
jsonToSend.free;
http.free;

and simple mysql and json encode in the mydata.php

0
votes

There are various methods in Delphi, in this example I have the following:

I have a TSQLConnection object on the form (but this could be created in code). I set the db connection and database settings on the component.

Then, in code...

//variables
var Q: TSQLQuery;

//in function
Q := TSQLQuery.Create(nil);
try
  Q.SQLConnection := myConnectionObjectOnForm;
  Q.SQL.Text := "SELECT some stuff from TABLE";
  Q.Active := true;

  if not Q.Eof then
    //Do stuff eg
    result := Q.Fields[0].AsString;  //or reference FieldByName

  Q.Close;
finally
  Q.Free;
end;

The TSQLQuery and TSQLDataSet are most commonly used for data retrieval