Page 1 of 1

MySQL and the PB Database commands?

Posted: Mon May 19, 2008 8:51 pm
by PBUser
Hi Guys,

i want to connect to a mysql database and manage it with the pb-included Database commands. I already used the search in the german and in the english forum, but there was no entry that worked with PureBasic 4.20.

I hope, you can help me.
Thanks in advance,

PBUser

Posted: Mon May 19, 2008 9:01 pm
by ts-soft
Install MyODBC 3.51

Posted: Mon May 19, 2008 10:03 pm
by PBUser
OK... Installed. What shall I do now?

//edit: Does this work on Windows Vista? If it doesn't work, we'll have to switch to the libmysql :-(

Posted: Wed May 21, 2008 10:57 am
by PBUser
Anybody? ;-)

Posted: Wed May 21, 2008 1:23 pm
by Flype
Yes this should work on Vista.
ODBC is the Microsoft Database Layer, so it would be seriously bad if it doesn't , but i did not test (successfully tested on 2K, XP, 2K3 Server).

Once MyODBC is installed, you have to configure (manually or programmaticaly) a database connection.

for that, follow instructions :

1/
start -> execute -> odbcad32.exe

2/
make sure you have MySQL ODBC 3.51 Driver in 'ODBC Driver' page

3/
system data source -> add -> select MyODBC

4/
Data Source Name = test
Description =
Server = localhost (or external hosts/IP but make sure port 3306 is opened, so check your firewall and mysql user/rights)
User = root
Password =
Database = mysql

5/
click on 'test' button to check if connection is ok

6/
ok your odbc connection is ready, now let's see what to do inside purebasic

7/
just execute this sample :

Code: Select all

;-------------------------------------------------------------------------
; Database Exemple for PureBasic 4.2x.
; This minimalist example require a 'test' data source.
; Data source must be first configured in the ODBC manager (odbcad32.exe).
;-------------------------------------------------------------------------


; Initialize the ODBC engine

If UseODBCDatabase()
  
  ; Connect to the MySQL database
  
  If OpenDatabase(0, "test", "root", "")
    
    ; Execute a SQL query
    
    If DatabaseQuery(0, "SELECT help_topic_id, name FROM mysql.help_topic")
      
      ; Fetching rows
      
      While NextDatabaseRow(0)
        Debug GetDatabaseString(0, 0) + " ==> " + GetDatabaseString(0, 1)
      Wend
      
    Else
      
      MessageRequester("Error", "DatabaseQuery() failed !" + #CRLF$ + DatabaseError())
      
    EndIf
    
    ; Once fetching done, closing opened database.
    
    CloseDatabase(0)
    
  Else
    
    MessageRequester("Error", "OpenDatabase() failed !" + #CRLF$ + DatabaseError())
    
  EndIf
  
Else
  
  MessageRequester("Error", "UseODBCDatabase() failed !")
  
EndIf

End

Posted: Wed May 21, 2008 1:45 pm
by Flype
and there's also very old tutorials here that i took years ago :
PB_SQL.zip