Recent

Author Topic: [SOLVED] Beginner's problem: connection to mysql database  (Read 2031 times)

pjtuloup

  • New Member
  • *
  • Posts: 45
[SOLVED] Beginner's problem: connection to mysql database
« on: June 15, 2024, 07:11:14 pm »
Hi all.
I would first like to salute the Lazarus development team and their amazing work: I have been following Lazarus since a version 0.9 but with the 3.4 that I have just discovered it is the first time that I find it really usable.

I come from C++ Builder because over time licenses become horribly expensive and with less features with the basic versions; So I came back to Lazarus even though I have no real experience in Pascal.

So to test I wanted to create a small application to connect to a Mysql database and I adapted some code for beginners that I found on developpez.com, where the Mysql functions to be used in the program are grouped in a DataModule.

Here is the code:

Code: Pascal  [Select][+][-]
  1. unit DataAccess;
  2.  
  3. {$mode ObjFPC}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, mysql80conn, SQLDB, Dialogs,db;
  9.  
  10. type
  11.  
  12.   { TDataModule1 }
  13.  
  14.   TDataModule1 = class(TDataModule)
  15.     SQLConnection1: TMySQL80Connection;
  16.     SQLTransaction: TSQLTransaction;
  17.   private
  18.  
  19.   public
  20. function Login: Boolean;
  21. procedure Logoff;
  22.   end;
  23.  
  24. var
  25.   DataModule1: TDataModule1;
  26.  
  27. implementation
  28.  
  29. {$R *.lfm}
  30.  
  31. { TDataModule1 }
  32.  
  33. function TDataModule1.Login: Boolean;
  34.  
  35. begin
  36. Result := true;
  37. SQLConnection1.Hostname := '*';
  38. SQLConnection1.DatabaseName := '*';
  39. SQLConnection1.UserName := '*';
  40. SQLConnection1.Password := '*';
  41. try
  42.   SQLConnection1.Connected := true;
  43.   SQLTransaction.Active := true;
  44. except
  45.   on e : ESQLDatabaseError do
  46.   begin
  47.     MessageDlg('Erreur de connexion à la base :'#10#10#13 + IntToStr(e.ErrorCode) + ' : ' + e.Message + #10#10#13'Application terminée.',mtError,[mbOK],0);
  48.     Result := false;;
  49.   end;
  50.   on e : EDatabaseError do
  51.   begin
  52.     MessageDlg('Erreur de connexion à la base :'#10#10#13'Application terminée.',mtError,[mbOK],0);
  53.     Result := false;;
  54.   end;
  55. end;
  56. if Result = true then
  57.    MessageDlg('Base disponible!',mtError,[mbOK],0);
  58. end;
  59.  
  60. procedure TDataModule1.Logoff;
  61. begin
  62. if SQLTransaction.Active then
  63.    SQLTransaction.Active := false;
  64. if SQLConnection1.Connected  then
  65.     SQLConnection1.Connected := false;
  66. end;
  67.  
  68. end.
  69.  

The problem is that this code does nothing: no dialog box appears either in case of error or in case of successful connection (The form is created first in the Project options)

I then wanted to test the connection with the properties of SQLConnexion (Connected) and SQLTransaction (Active) but then I get the message "Unable to load libmysql.dll".

After a lot of research I was able to get libmysql.dll (64 bits for Windows 11, Version 8.037) and I placed it in several places, with the .exe of my application, the Lazarus.exe, various other places in the path and nothing helps, I always get the same message.

In C++ Builder you can specify the location of libmysql.dll in the properties of a component but in Lazarus I couldn't find anything like that?

Could someone tell me if

1° Is my code correct?
2° If so, why does it seem not to be executed?
3° Where to place the libmysqll.dll so that it may be recognized by Lazarus
4° If the two problems are linked?

Thank you very much in advance!
« Last Edit: June 16, 2024, 06:41:04 pm by pjtuloup »

TRon

  • Hero Member
  • *****
  • Posts: 3615
Re: Beginner's problem: connection to mysql database
« Reply #1 on: June 15, 2024, 08:10:31 pm »
Hello and welcome pjtuloup,

First of all there is a database portal in the wiki that links to many tutorials (SQLDb tutorials).

Secondly, it is nearly impossible to tell what is wrong with your code because developing databases with visual controls also entails setting all the properties of the control(s) in use and the code that you posted does not show any of that (other then some visible modifications made in code).

Other then that the database name has to be an actual name of a database (not an asterisk).

So please provide a full example including your mainform so that it is possible to see how you call the code in the datamodule (if you do that at all).

How to install mysql on Windows is a bit out of scope for both the forums and me (since I do not use windows anymore) but perhaps the wiki or someone else is able to answer that for you. What I do know is that your library has to match the Lazarus bitness, so  in case your windows is 64 bit and you installed the 64 bit version of the mysql libraries then your Lazarus installation must be 64-bit as well.

« Last Edit: June 15, 2024, 08:23:48 pm by TRon »
This tagline is powered by AI

jamie

  • Hero Member
  • *****
  • Posts: 6733
Re: Beginner's problem: connection to mysql database
« Reply #2 on: June 15, 2024, 08:52:05 pm »
I looked at the source code and the name is correct for the lib but it specifies no path, so this means the DLL must be installed in the System32 folder for 64 bit or in the project's folder.

 I see there are MSI install options for that, maybe you can look for the version you need.
The only true wisdom is knowing you know nothing

pjtuloup

  • New Member
  • *
  • Posts: 45
Re: Beginner's problem: connection to mysql database
« Reply #3 on: June 15, 2024, 08:56:43 pm »
756 / 5 000
Hi TRon,
Thank you very much for your answer.

Of course, the name of my database is not "*", I just put asterisks in place of the database name, user and password for confidentiality reasons.

And yes, the Lazarus, libmysqll.dll and Windows versions are consistent in 64 bits.

So please provide a full example including your mainform so that it is possible to see how you call the code in the datamodule (if you do that at all).

I think I'll study this more closely: because for the moment the code in the datamodule is not called in the MainForm: because the datamodule is loaded first I thought that was enough (which seemed to me when even strange).

Thanks for the suggestion; I'll keep you informed.

pjtuloup

  • New Member
  • *
  • Posts: 45
Re: Beginner's problem: connection to mysql database
« Reply #4 on: June 15, 2024, 09:01:23 pm »
Thanks Jamie

My libmysql.dll is indeed in the project folder. I'm going to put one in the Windows system directories...

dseligo

  • Hero Member
  • *****
  • Posts: 1406
Re: Beginner's problem: connection to mysql database
« Reply #5 on: June 15, 2024, 09:06:49 pm »
I think I'll study this more closely: because for the moment the code in the datamodule is not called in the MainForm: because the datamodule is loaded first I thought that was enough (which seemed to me when even strange).

Double click somewhere in the datamodule and 'DataModuleCreate' method will be created and opened by Lazarus.
You can call your Login function there.
It should look like that:
Code: Pascal  [Select][+][-]
  1. procedure TDataModule1.DataModuleCreate(Sender: TObject);
  2. begin
  3.   // * code above is generated by Lazarus.
  4.   If not Login then
  5.   begin
  6.     // something ...
  7.   end;
  8. end;

dseligo

  • Hero Member
  • *****
  • Posts: 1406
Re: Beginner's problem: connection to mysql database
« Reply #6 on: June 15, 2024, 09:09:36 pm »
My libmysql.dll is indeed in the project folder. I'm going to put one in the Windows system directories...

Check where your 'exe' is created. If it is actually in your project directory, or in a subdirectory of your project 'lib\x86_64-win64'.

TRon

  • Hero Member
  • *****
  • Posts: 3615
Re: Beginner's problem: connection to mysql database
« Reply #7 on: June 15, 2024, 09:20:16 pm »
Of course, the name of my database is not "*", I just put asterisks in place of the database name, user and password for confidentiality reasons.
Fair enough but had to mention it just in case.

Quote
And yes, the Lazarus, libmysqll.dll and Windows versions are consistent in 64 bits.
Good  :)

Other than that follow the advise provided by others.

Quote
I think I'll study this more closely: because for the moment the code in the datamodule is not called in the MainForm: because the datamodule is loaded first I thought that was enough (which seemed to me when even strange).
As dseligo's example shows you need to manually call the login and logoff methods of the datamodule.

Just make sure your datamodule is created before your main-form before calling the datamodule methods or as shown by dseligo's example, at the create/destroy events of the datamodule.

PS: one thing I noticed (but that is the problem of not having provided the lfm) is that the transaction and the database do not seem to be linked (at least not in code).
« Last Edit: June 15, 2024, 09:25:35 pm by TRon »
This tagline is powered by AI

af0815

  • Hero Member
  • *****
  • Posts: 1378
Re: Beginner's problem: connection to mysql database
« Reply #8 on: June 16, 2024, 08:04:39 am »
The libmySQL.dll and the project must have the same bitness. You can make on a win64 system with a win32 Lazarus a win64 application. But you must know the rules.

And rule number one is: Project and dll/so must have the same bitness.
regards
Andreas

pjtuloup

  • New Member
  • *
  • Posts: 45
Re: Beginner's problem: connection to mysql database
« Reply #9 on: June 16, 2024, 05:24:02 pm »
Thank you dseligo, in fact my code was not executed due to lack of a procedure to call it.
Thanks af0815, that's the case, system, Lazarus and libmysql.dll are all 64 bits
Thanks Tron, the database and the transaction are well linked in the properties

Now that my code is executed I have the same error 'unable to load libmysqll.dll' as when I try to connect the database through the properties, and this regardless of where I put it, whether it is in the path or not, in the project or not, with the executable or not. There must be a problem with this file which I nevertheless recovered with the installation of Mysql.

I read on the forums that some people who had the same problem solved it by installing Visual C++ redistributable... I'll try that

af0815

  • Hero Member
  • *****
  • Posts: 1378
Re: Beginner's problem: connection to mysql database
« Reply #10 on: June 16, 2024, 05:29:49 pm »
Thanks af0815, that's the case, system, Lazarus and libmysql.dll are all 64 bits
I miss the Project/Application bitness here :-)

The point is - you can have System, Lazarus and libmysql.dll in 64bit, but compiling and using a win32 application with the 64 bit Lazarus and then you are wrong and have to use a 32bit libmysql.dll.
regards
Andreas

pjtuloup

  • New Member
  • *
  • Posts: 45
Re: Beginner's problem: connection to mysql database
« Reply #11 on: June 16, 2024, 05:57:38 pm »
All right ! I never thought I could create a win32 application!

I looked in Project Options->Compiler Options->Configuration and Target; I specified Win64 for the destination OS and unchecked "Win32 graphics application" but it remains "Current graphics package: win32" and I don't know which other LCL graphics package to choose.

 For now, nothing has changed.

dseligo

  • Hero Member
  • *****
  • Posts: 1406
Re: Beginner's problem: connection to mysql database
« Reply #12 on: June 16, 2024, 06:06:04 pm »
All right ! I never thought I could create a win32 application!

I looked in Project Options->Compiler Options->Configuration and Target; I specified Win64 for the destination OS and unchecked "Win32 graphics application" but it remains "Current graphics package: win32" and I don't know which other LCL graphics package to choose.

 For now, nothing has changed.

Leave "Win32 graphics application" checked. Otherwise command prompt will also be opened every time you start your application.
It's probably VC++ redistributable you mentioned.

P.S.:
You could try to install HeidiSQL and copy libmysql.dll from there (I think that one is compiled without need of VC++ redistributable).
« Last Edit: June 16, 2024, 06:08:02 pm by dseligo »

pjtuloup

  • New Member
  • *
  • Posts: 45
Re: Beginner's problem: connection to mysql database
« Reply #13 on: June 16, 2024, 06:20:32 pm »
I will try ... (Installing Visua C++ 2015-2022 Redistributable was without effect)

pjtuloup

  • New Member
  • *
  • Posts: 45
Re: Beginner's problem: connection to mysql database
« Reply #14 on: June 16, 2024, 06:35:33 pm »
Dseligo, you saved my life ! the HeidiSQL's libmysql.dll works perfectly. So this was the one I got from the Mysql server installation that must have been corrupted.

Thank you all for your help and advice; I hope I haven't wasted too much of your time; I also hope to progress quickly enough to be able to help in my turn... one day

Happy end of the day!

 

TinyPortal © 2005-2018