[RESOLVED] OpenDatabase error when running executable but not in debug?

Just starting out? Need help? Post your questions and find answers here.
caussatjerome
New User
New User
Posts: 2
Joined: Thu Oct 31, 2024 11:51 am

[RESOLVED] OpenDatabase error when running executable but not in debug?

Post by caussatjerome »

Bonjour, lorsque je lance programme directement dans Purebasic, pas de soucis, le message Connexion réussi apparait, si je le compile et lance le programme, j'ai le message d'erreur ?

Ais-je fait une erreur ou est-ce effectivement comme je le pense un bug ?

---------------
Hello, when I launch the program directly in Purebasic, no worries, the message Connection successful appears, if I compile it and launch the program, I have the error message?

Did I make a mistake or is it actually a bug as I think?
---------------

Code: Select all

;test opendatabase compliler
host.s="XXX.XXX.XXX.XXX"; IP or HostName
port.s="3306"; port
debname.s="test"
username.s="user"
password.s="password"

UseMySQLDatabase()

tentative_co_sql:
db_connect.i=OpenDatabase(#PB_Any, "host="+host.s+" port="+port.s+" dbname='"+debname.s+"'", username.s, password.s,#PB_Database_MySQL)
If IsDatabase(db_connect.i)
  MessageRequester(title.s,"Connexion réussi à la base de données.",#PB_MessageRequester_Info|#PB_MessageRequester_Ok)
  Delay(30)
  CloseDatabase(db_connect.i)
Else;erreur de connexion
  dberreur.s=DatabaseError()
  If tentative_sql.i=3;il y a eu trois essais, on arrête la
    MessageRequester(title.s,"Echec de connexion à la base de données après 3 essais."+#CRLF$+dberreur.s,#PB_MessageRequester_Error|#PB_MessageRequester_Ok)
  Else;on a pas atteint trois essais, on retente
    tentative_sql.i+1
    Delay(300)
    Goto tentative_co_sql
  EndIf
EndIf
End 0

// Moved from "Bugs - Windows" to "Coding Questions" (Kiffi)
Last edited by caussatjerome on Thu Oct 31, 2024 1:33 pm, edited 1 time in total.
PBJim
Enthusiast
Enthusiast
Posts: 296
Joined: Fri Jan 19, 2024 11:56 pm

Re: OpenDatabase error when running executable but not in debug?

Post by PBJim »

caussatjerome wrote: Thu Oct 31, 2024 12:00 pm Bonjour, lorsque je lance programme directement dans Purebasic, pas de soucis, le message Connexion réussi apparait, si je le compile et lance le programme, j'ai le message d'erreur ? Ais-je fait une erreur ou est-ce effectivement comme je le pense un bug ?

Hello, when I launch the program directly in Purebasic, no worries, the message Connection successful appears, if I compile it and launch the program, I have the error message?
Bonjour, there is an optional parameter in the UseMySQLDatabase, where you can specify the location of the MySQL DLL. It doesn't need this parameter when the code is run within the IDE, as the path to the DLL is already determined.

Code: Select all

UseMySQLDatabase("C:\Program Files\...dll")
There are several other options too. For instance, you can put the DLL into the executable's directory.
User avatar
spikey
Enthusiast
Enthusiast
Posts: 778
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: OpenDatabase error when running executable but not in debug?

Post by spikey »

You could do something like this:

Code: Select all

Define.s LibPath
CompilerIf #PB_Compiler_Debugger
  LibPath = #PB_Compiler_Home + "Compiler" + #PS$ + "libmariadb.dll"
CompilerElse
  LibPath = GetPathPart(ProgramFilename()) + "libmariadb.dll"
CompilerEndIf
UseMySQLDatabase(LibPath)
This checks to see if the debugger is attached. If so, the library is loaded from the Purebasic installation folder. If not, it loads from the executable's folder.

It's not completely robust - it will still fail if you compile from the IDE with the debugger disabled to a temporary folder. You can get around that by using the "Create temporary executable in the source directory" compiler option and copying the lib dll into the source folder, if this is an issue.
User avatar
jacdelad
Addict
Addict
Posts: 2031
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: OpenDatabase error when running executable but not in debug?

Post by jacdelad »

Also: Don't use Goto!
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
caussatjerome
New User
New User
Posts: 2
Joined: Thu Oct 31, 2024 11:51 am

Re: OpenDatabase error when running executable but not in debug?

Post by caussatjerome »

spikey wrote: Thu Oct 31, 2024 1:17 pm You could do something like this:

Code: Select all

Define.s LibPath
CompilerIf #PB_Compiler_Debugger
  LibPath = #PB_Compiler_Home + "Compiler" + #PS$ + "libmariadb.dll"
CompilerElse
  LibPath = GetPathPart(ProgramFilename()) + "libmariadb.dll"
CompilerEndIf
UseMySQLDatabase(LibPath)
This checks to see if the debugger is attached. If so, the library is loaded from the Purebasic installation folder. If not, it loads from the executable's folder.

It's not completely robust - it will still fail if you compile from the IDE with the debugger disabled to a temporary folder. You can get around that by using the "Create temporary executable in the source directory" compiler option and copying the lib dll into the source folder, if this is an issue.
Thank's !!!
PBJim
Enthusiast
Enthusiast
Posts: 296
Joined: Fri Jan 19, 2024 11:56 pm

Re: OpenDatabase error when running executable but not in debug?

Post by PBJim »

spikey wrote: Thu Oct 31, 2024 1:17 pm It's not completely robust - it will still fail if you compile from the IDE with the debugger disabled to a temporary folder. You can get around that by using the "Create temporary executable in the source directory" compiler option and copying the lib dll into the source folder, if this is an issue.
Might this be a simpler solution spikey? Determine the presence of the DLL at run-time, which works through the IDE, either with or without debugger.

Code: Select all

Define.s LibPath

LibPath = GetPathPart(ProgramFilename()) + "libmariadb.dll"

If FileSize(LibPath) > 0                                                ; Lib exists in the programme directory
  UseMySQLDatabase(LibPath)                                             ; ... so use it
Else
  UseMySQLDatabase()                                                    ; Don't know location, works fine if run via IDE
EndIf
User avatar
spikey
Enthusiast
Enthusiast
Posts: 778
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: OpenDatabase error when running executable but not in debug?

Post by spikey »

PBJim wrote: Thu Oct 31, 2024 2:25 pm Might this be a simpler solution spikey? Determine the presence of the DLL at run-time, which works through the IDE, either with or without debugger.
It was just the first solution that came to mind. Yes, it might be an idea to check that the library file isn't missing so you can fail gracefully.
Post Reply