Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed simple example to use TAdoConnection to connect to MySql default database
    primarykey
    data
    text
    <p>Final answer:</p> <p>This was not a Delphi problem, just configuration. </p> <p>I use Xampp to provide the MySql server. </p> <pre><code>C:\xampp\mysql\bin&gt;mysql.exe --version mysql.exe Ver 14.14 Distrib 5.1.41, for Win32 (ia32) </code></pre> <p>so, the correct connection string is </p> <pre><code>'Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=mysql;User=root; Password=;Option=3;' </code></pre> <p>(and don't forget to load <a href="http://dev.mysql.com/downloads/mirror.php?id=393479#mirrors" rel="nofollow noreferrer">the MySql ODBC 5.1 driver</a>! </p> <p>Short code to do it :</p> <pre><code>procedure TForm1.Button1Click(Sender: TObject); var AdoConnection : TAdoConnection; DataBase : String; begin Try AdoConnection := TADOConnection.Create(nil); if AdoConnection.Connected then // already connected? begin MessageDlg('Already connected', mtInformation, [mbOK], 0); Exit; end; begin DataBase := 'mysql'; AdoConnection.LoginPrompt:=False;//dont ask for the login parameters AdoConnection.ConnectionString := 'Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=' + Database + ';User=root; Password=;Option=3;'; AdoConnection.Connected := True; //open the connection MessageDlg('Connected to databse "' + DataBase + '".', mtInformation, [mbOK], 0); end; Except On E: Exception do begin MessageDlg('Cannot connect to databse "' + DataBase + '"!.' + #13 + #10 + 'Please report this problem (is MySql running?)', mtError, [mbOK], 0); end; end; end; </code></pre> <hr> <p>I found some old code and am trying to figure it out. </p> <p>Here's some code to attempt to the default MySql d/b calld <code>mysql</code> as user <code>root</code> with no password..</p> <pre><code>const MYSQL_CONNECT_STRING_FROM_DELPHI = 'Driver={MySQL ODBC 3.51 Driver};Server=%s;Database=%s;User=%s; Password=%s;Option=3;'; var AdoConnection : TAdoConnection; AdoConnection.ConnectionString := Format(MYSQL_CONNECT_STRING_FROM_DELPHI,['localhost',DataBase,'root','']); AdoConnection.Connected := True; </code></pre> <p>(I could have used TADOCommand, but prefer just to use TAdoConnection throughout my code)</p> <p>When I run it I get <code>ELoException [Microsoft][ODBC Driver manager] Data source name not found and no default driver specified</code>.</p> <p>I have recently reinstalled both windows &amp; delphi 7. Did I perhaps forget to install something else, or is it the code which is at fault?</p> <hr> <p>Edit/update:</p> <p>I just realized that I need to Get MySql ODBC connector from <a href="http://dev.mysql.com/downloads/mirror.php?id=367506" rel="nofollow noreferrer">http://dev.mysql.com/downloads/mirror.php?id=367506</a></p> <p>I did that and the error is now <code>Unknown MySql Server host "localhost"</code> (I have Xampp running an Apache server, so localhost should be fine)</p> <hr> <p>Edit 2: This code previously worked on the development PC, but not on another. And, now after fresh install it does not work on the development machine. Maybe more a question of configuration than code, but does anyone have some code that I can compare, just in case?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload