Re: Delphi and mySQL
Posted by: András Lukács
Date: October 14, 2009 03:37AM
Don't take me wrong, but if that is an academic project, and you have never done this before, you could perhaps ask someone around your college or university because if you can't even connect to MySQL, you will have even more problems later, and the project may degenerate into being done not by you. Also, it's much easier to actually *show* it in person than to take the pains to put it to writing. I've got some time, so why not, but you won't perhaps look back, since you posted this a long time ago.
Here is how you do it. First, you must create a DSN (Data Source Name) in Windows for your database. You do it here: Start Menu/Settings/Control Panel/Administrator Tools/ODBC Data Sources
In the window that pops up, go to the User DSN tab. Click Add... (or Create..., I have no idea what it's called in the English version). A list will pop up. Select MySQL ODBC driver (if it's not there, you must install the MySQL client) and click Finish.
A Connector/ODBC window will pop up. Here is how you fill it in:
* Data Source Name: this is the name by which your db will be identified. Choose a name you like.
* Description: just ignore it.
* Server: localhost
* User: enter the MySQL user name you use to access your db
* Password: the password for your MySQL user name
* Database: select your database. The name of your db will only appear if the user you enter has access to it.
* click OK.
You will see that a new DSN has been created for your db. You can use it anywhere in Windows to connect to it.
Launch Delphi. Create a new VCL Forms Application. This is going to be rather minimalistic (no datamodules, etc.) but it will work as a first attempt to test if everything works all right. Put the following on your empty form from the component palette:
1. dbGo/TADOConnection. This component will provide a connection to the db. Double-click on the component. That makes the connectionstring window pop up. Select "Use Connection String", and click Build... In the window that pops up, select Microsoft OLE DB Provider for ODBC Drivers. Select the Connection tab (the next one). Select Use DSN Name (the default option), and select from the drop-down menu the DSN name you have just created for your db. Provide the user name and the password in the corresponding edit boxes. Click Test Connection to see if the component can connect to the db. If everything is fine, it should say so. Click OK to close the window. Then click OK again. Set the LoginPrompt property of the component to FALSE in the Object Inspector. If you fail to do that, you will have to provide the user name and password every time you want to connect to the db.
2. dbGo/TADOTable. Set its Connection property to the name of the TADOConnection component. Set its TableName property to the name of the table you want to display.
3. Data Access/TDataSource. Set its Dataset property to the name of the ADOTable component.
4. Data Controls/TDBGrid. Set its DataSource property to the DataSource's name.
Then select the ADOTable component, and set its Active property to TRUE. You will see that the DBGrid now displays the contents of the table whose name you specified.
This is a very simple app. You can display not only simple tables but the result set of any SELECT statement by using a dbGo/TADOQuery instead of TADOTable. Its settings are the same, except that you must provide the SELECT statement in its SQL property.
And of course, there is a whole lot more to Delphi and databases... Please be more specific in your future questions.