MySQL data retrieval module

Developed or developing a new product in PureBasic? Tell the world about it.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

MySQL data retrieval module

Post by Fangbeast »

This is one I did a couple of years ago. Now reworked with PB4, Beta 7 and prototypes (Now that I got my head around them). It is a wrapperless solution based on code I found in the forum in the last 2 years.

As I don't have MySQL server here; nor any databases to test with; I suggest people bash it around and see if it works for them. The mysql dll is included.

http://www.penguinbyte.com/apps/pbwebst ... rieval.rar

**EDIT**
46 views and not one comment. That's great!! It means I don't have to do any work, can sit and read books all day. I love it!! Although, it ruins my reputation as people are going to think I can actually code and that's not true!! (evil grin(c) fang corporation)

***EDIT*** I finally had time to install MySql server and found that I had errors in this code so below is the fixed module. All it needs now is the addition of field names returned and added dynamically to the collumn headings in the ListIconGadget but I don't know how to do that yet.

Edited and tested with PB4, Beta 11

***EDIT*** Thanks to those geniuses Flype and El_Choni, columns now have the names of the returned data. Woohooo!!!

***EDIT*** I did away with the stringgadget as an entry box because some people may want to formulate complex queries and replaced it with an editor gadget. Keep typing your queries and pressing enter and nothing will happen until you enter a line with a semicolon in it!

Once a line is entered with a semicolon, the query is built, the querybox is cleared and the query entered.

P.s. I added a little logo box on the right. Replace my image with your own:):)

Code: Select all

;============================================================================================================================
; Original code found in the forums from El_Choni, Rings, Flype and Max2
;
; Extensively reworked, cleaned up, formatted and added to by fangbeast. No sleep, no food for 3 months!!
;
; Loads of help from Flype and El_Choni, those geniuses.
;
;
;============================================================================================================================
; XIncludeFile "MySQL Data test_Myprototypes.pb"                         ; All new prototype declarations
;============================================================================================================================

Prototype mysql_init(dbHandle.l)
Prototype mysql_errno(dbHandle.l)
Prototype mysql_error(dbHandle.l)
Prototype mysql_real_connect(dbHandle.l, host.s, user.s, password.s, dbName.s, port.l, Number.l, options.l)
Prototype mysql_real_query(dbHandle.l, Query.s, Querylen.l)
Prototype mysql_store_result(dbHandle.l)
Prototype mysql_field_count(dbHandle.l)
Prototype mysql_use_result(dbHandle.l)
Prototype mysql_fetch_row(*mysqlResult)
Prototype mysql_fetch_lengths(*mysqlResult)
Prototype mysql_free_result(*mysqlResult)
Prototype mysql_close(dbHandle.l)
Prototype mysql_affected_rows(dbHandle.l)
Prototype mysql_num_fields(*mysqlResult)
Prototype mysql_num_rows(*mysqlResult)
Prototype mysql_ping(dbHandle.l)
Prototype mysql_fetch_field(*mysqlResult)

;============================================================================================================================
;XIncludeFile "MySQL Data test_Mydeclarations.pb"                       ; My procedural declarations so procedures can be called out of sequence
;============================================================================================================================

Declare.s GetMySqlError(SetTitle.l)                            ; Get the last error returned by the current sql query
Declare   GetSqlData(Query.s)                                  ; Re-use this section to get the sql data into a structure
Declare   PingServer()                                         ; We need a ping thread to keep the server alive.
Declare   LoadLastLogin()                                      ; Load the last login saved by the user if there was one 
Declare   SaveCurrentLogin()                                   ; Save the current login details if the user has selected the checkbox 
Declare   CheckEnter()                                         ; Check if the ENTER key was pressed in the SQL query box 
Declare   ConnectToDatabase(Query.s)                           ; Try to connect to the database 
Declare   DumpListToDisk()                                     ; Dump the list to disk at the users' request 

;============================================================================================================================
;XIncludeFile "MySQL Data test_Constants.pb"                            ; visual designer created form
;============================================================================================================================

Enumeration 1
  #Window_mysqltest
EndEnumeration

#WindowIndex = #PB_Compiler_EnumerationValue

Enumeration 1
  #Gadget_mysqltest_mainframe
  #Gadget_mysqltest_datalist
  #Gadget_mysqltest_controlframe
  #Gadget_mysqltest_querybox
  #Gadget_mysqltest_loginframe
  #Gadget_mysqltest_hostlabel
  #Gadget_mysqltest_hostbox
  #Gadget_mysqltest_userlabel
  #Gadget_mysqltest_userbox
  #Gadget_mysqltest_passwordlabel
  #Gadget_mysqltest_passwordbox
  #Gadget_mysqltest_databaselabel
  #Gadget_mysqltest_databasebox
  #Gadget_mysqltest_portlabel
  #Gadget_mysqltest_portbox
  #Gadget_mysqltest_savelogin
  #Gadget_mysqltest_clearlist
  #Gadget_mysqltest_ilogo
  #Gadget_mysqltest_dumpbutton
  #Gadget_mysqltest_otherframe
  #Gadget_mysqltest_helpbutton
  #Gadget_mysqltest_exitbutton
EndEnumeration

#GadgetIndex = #PB_Compiler_EnumerationValue

Enumeration 1
  #Image_mysqltest_ilogo
EndEnumeration

#ImageIndex = #PB_Compiler_EnumerationValue

;============================================================================================================================
;XIncludeFile "MySQL Data test_Windows.pb"                              ; visual designer created form
;============================================================================================================================

Procedure.l Window_mysqltest()
  If OpenWindow(#Window_mysqltest,59,72,800,555,"MySQL - ",#PB_Window_ScreenCentered|#PB_Window_Invisible)
    If CreateGadgetList(WindowID(#Window_mysqltest))
      Frame3DGadget(#Gadget_mysqltest_mainframe,0,0,640,470,"")
      ListIconGadget(#Gadget_mysqltest_datalist,5,10,630,455,"itemslist",1000,#PB_ListIcon_GridLines|#PB_ListIcon_FullRowSelect|#PB_ListIcon_AlwaysShowSelection)
        GadgetToolTip(#Gadget_mysqltest_datalist,"All items returned from a properly formatted SQL query will end up in this list and be cleared in  the next call")
      Frame3DGadget(#Gadget_mysqltest_controlframe,0,470,640,85,"MySql query box")
      EditorGadget(#Gadget_mysqltest_querybox,5,485,630,60)
      Frame3DGadget(#Gadget_mysqltest_loginframe,645,0,155,510,"")
      TextGadget(#Gadget_mysqltest_hostlabel,655,15,135,15,"Host IP or host name")
      StringGadget(#Gadget_mysqltest_hostbox,655,30,135,20,"")
        GadgetToolTip(#Gadget_mysqltest_hostbox,"Type in the name of the system that hosts the database or the IP version")
      TextGadget(#Gadget_mysqltest_userlabel,655,55,135,15,"User name")
      StringGadget(#Gadget_mysqltest_userbox,655,70,135,20,"",#PB_String_LowerCase)
        GadgetToolTip(#Gadget_mysqltest_userbox,"Type in the name of the user who is registered for use with that database")
      TextGadget(#Gadget_mysqltest_passwordlabel,655,95,135,15,"Password")
      StringGadget(#Gadget_mysqltest_passwordbox,655,110,135,20,"",#PB_String_Password|#PB_String_LowerCase)
        GadgetToolTip(#Gadget_mysqltest_passwordbox,"Type in the login password of the user that is registered for use with that database")
      TextGadget(#Gadget_mysqltest_databaselabel,655,135,135,15,"Database name")
      StringGadget(#Gadget_mysqltest_databasebox,655,150,135,20,"",#PB_String_LowerCase)
        GadgetToolTip(#Gadget_mysqltest_databasebox,"Type in the name of the database that you wish to access on the remote system")
      TextGadget(#Gadget_mysqltest_portlabel,655,175,135,15,"Port number")
      StringGadget(#Gadget_mysqltest_portbox,655,190,135,20,"",#PB_String_Numeric)
        GadgetToolTip(#Gadget_mysqltest_portbox,"Type in the port number that the database server allows queries on")
      CheckBoxGadget(#Gadget_mysqltest_savelogin,655,220,135,15,"Save current login")
        GadgetToolTip(#Gadget_mysqltest_savelogin,"Check this box if you want to save the current login to be auto-loaded next time you start this program")
      CheckBoxGadget(#Gadget_mysqltest_clearlist,655,240,135,20,"Clear list before query")
        GadgetToolTip(#Gadget_mysqltest_clearlist,"Select this button to clear the list before the next SQL query")
      ImageGadget(#Gadget_mysqltest_ilogo,655,270,131,198,ImageID(#Image_mysqltest_ilogo),#PB_Image_Border)
      ResizeGadget(#Gadget_mysqltest_ilogo,655,270,131,198)
      ResizeImage(#Image_mysqltest_ilogo,131,198)
      SetGadgetState(#Gadget_mysqltest_ilogo,ImageID(#Image_mysqltest_ilogo))
      ButtonGadget(#Gadget_mysqltest_dumpbutton,655,480,135,20,"Dump list to disk")
        GadgetToolTip(#Gadget_mysqltest_dumpbutton,"press this button to dump the results of the query to a disk file")
      Frame3DGadget(#Gadget_mysqltest_otherframe,645,510,155,45,"")
      ButtonGadget(#Gadget_mysqltest_helpbutton,655,525,60,20,"Help")
        GadgetToolTip(#Gadget_mysqltest_helpbutton,"press this button to show any help file linked to this program")
      ButtonGadget(#Gadget_mysqltest_exitbutton,730,525,60,20,"Exit")
        GadgetToolTip(#Gadget_mysqltest_exitbutton,"press this button to exit this program immediately")
      HideWindow(#Window_mysqltest,0)
      ProcedureReturn WindowID(#Window_mysqltest)
    EndIf
  EndIf
EndProcedure

;============================================================================================================================
;XIncludeFile "MySQL Data test_Myconstants.pb"                          ; All my personal constants
;============================================================================================================================


;============================================================================================================================
; MySQL specific variables
;============================================================================================================================

#libmysql               = 1  
#MySQL_CLIENT_COMPRESS  = 32  

;============================================================================================================================
; My personal variables
;============================================================================================================================

#MainTitle              = "MySQL - " 

;============================================================================================================================
;
;============================================================================================================================

;============================================================================================================================
;
;============================================================================================================================

CatchImage(#Image_mysqltest_ilogo, ?_OPT_mysqltest_ilogo)

;============================================================================================================================
;
;============================================================================================================================

DataSection
  _OPT_mysqltest_ilogo  : IncludeBinary "Images\addrecord.bmp"
EndDataSection

;============================================================================================================================
; Program operation variables
;============================================================================================================================

Structure programdata
  quitvalue.l
  affectedrows.l
  numberoffields.l
  numberofrows.l
  mysqlping.l
  connect.l
  connecttries.l
  mylib.l
  dbhandle.l
EndStructure

;============================================================================================================================
; Server data variables
;============================================================================================================================

Structure serverdata
  hostname.s
  username.s
  password.s
  database.s
  portaddr.s
EndStructure

;============================================================================================================================
; Field definitions structure for MySql. Flype
;============================================================================================================================

Structure MYSQL_FIELD
  name.s
  org_name.s
  table.s
  org_table.s
  db.s
  catalog.s
  def.s
  length.l
  max_length.l
  name_length.l
  org_name_length.l
  table_length.l
  org_table_length.l
  db_length.l
  catalog_length.l
  def_length.l
  flags.l
  decimals.l
  charsetnr.l
EndStructure
 
;============================================================================================================================
; All global variables
;============================================================================================================================

Global program.programdata, server.serverdata

Global myInit.mysql_init, myErrno.mysql_errno, myError.mysql_error, myRealConnect.mysql_real_connect
Global myRealQuery.mysql_real_query, myStoreResult.mysql_store_result, myFieldCount.mysql_field_count
Global myUseResult.mysql_use_result, myFetchRow.mysql_fetch_row, myFetchLengths.mysql_fetch_lengths
Global myFreeResult.mysql_free_result, myClose.mysql_close, myAffectedRows.mysql_affected_rows
Global myNumFields.mysql_num_fields, myNumRows.mysql_num_rows, myPingServer.mysql_ping, myFetchField.mysql_fetch_field

;============================================================================================================================
; Any resources included in the program
;============================================================================================================================

DataSection
  libmySQL_dll : IncludeBinary "\Development\Resources\Libraries\libmySQL.dll"
EndDataSection

;============================================================================================================================
;XIncludeFile "MySQL Data test_Myprocedures.pb"                         ; All program procedures
;============================================================================================================================



;============================================================================================================================
; Get the last error returned by the current sql query
;============================================================================================================================

Procedure.s GetMySqlError(SetTitle.l)
  If myErrno(program\dbHandle) > 0  
    *ErrorPointer = myError(program\dbHandle)
    ErrorMessage.s = PeekS(*ErrorPointer)
    If SetTitle.l  
      SetWindowTitle(#Window_mysqltest, #MainTitle + ErrorMessage.s)  
    EndIf  
  EndIf  
EndProcedure  

;============================================================================================================================
; Re-use this section to dump the sql data straight to a list. Could also dump to a linked list for later
;============================================================================================================================

Procedure GetSqlData(Query.s)                                                   ; If the query worked, get the data
  ;------------------------------------------------------------------------------------------------
  ClearGadgetItemList(#Gadget_mysqltest_datalist)                                ; Clear all previous list items
  ;------------------------------------------------------------------------------------------------
  If program\numberoffields <> 0                                                 ; Remove columns from previous query 
    For ColumnRemove = 1 To program\numberoffields - 1                           ; Total number of fields found
      RemoveGadgetColumn(#Gadget_mysqltest_datalist, 1) 
    Next ColumnRemove 
    program\numberoffields = 0 
  EndIf 
  ;------------------------------------------------------------------------------------------------
  Result = myRealQuery(program\dbHandle, Query.s, Len(Query.s))                  ; Run the given query 
  ;------------------------------------------------------------------------------------------------
  If Result                                                                      ; If the query didn't work, give an error
    GetMySqlError(program\dbHandle)
  Else
    ;----------------------------------------------------------------------------------------------
    *MySqlResult = myUseResult(program\dbHandle)                                 ; If data returned, use the result
    If *MySqlResult = 0
      If myFieldCount(program\dbHandle)                                          ; No fields are returned so that's an error
        GetMySqlError(program\dbHandle) 
      Else
        GetMySqlError(program\dbHandle)                                          ; Fields are returned, so no error but query didn't return data 
      EndIf 
    Else
      ;--------------------------------------------------------------------------------------------
      program\affectedrows   = myAffectedRows(program\Dbhandle)                  ; How many rows affected
      program\numberoffields = myNumFields(*MySqlResult)                         ; How many fields
      program\numberofrows   = myNumRows(*MySqlResult)                           ; How many rows
      ;--------------------------------------------------------------------------------------------
      For ColumnAdd = 1 To program\numberoffields - 1                           ; Add the number of columns to listicongadget
        AddGadgetColumn(#Gadget_mysqltest_datalist, 1, "", 100) 
      Next ColumnAdd 
      ;--------------------------------------------------------------------------------------------
      For ColumnNames = 0 To program\numberoffields - 1                          ; Add the column names to the list
       *Field.MYSQL_FIELD     = myFetchField(*MySqlResult)
        FieldName.s = *Field\Name
        SetGadgetItemText(#Gadget_mysqltest_datalist, -1, FieldName.s, ColumnNames) 
      Next ColumnNames
      ;--------------------------------------------------------------------------------------------
      Repeat                                                                     ; Sequentially process all returned data
        *MysqlRow = myFetchRow(*MySqlResult) 
        If *MysqlRow <> 0 
          *MysqlLen = myFetchLengths(*MySqlResult)          
          CurrentRow.s = "" 
          For CountFields = 1 To program\numberoffields                          ; Length of given field
            FieldLength  = PeekL(*MysqlLen + 4 * (CountFields - 1))            
            FieldPointer = PeekL(*MysqlRow + 4 * (CountFields - 1)) 
            If FieldPointer > 0 
              ContentToGet.s = PeekS(FieldPointer, FieldLength)                   ; Is there data in the field?
            Else 
              ContentToGet = "NULL"                                               ; Zero pointer returned means empty field
            EndIf 
            CurrentRow.s = CurrentRow.s + ContentToGet + "|"                      ; Content (individual collumns)
          Next CountFields
            RowString.s = ReplaceString(CurrentRow, "|", Chr(10), 1, 1) 
            AddGadgetItem(#Gadget_mysqltest_datalist, -1, RowString.s)            ; Dump the row to the listbox 
        EndIf 
      Until *MysqlRow = 0                                                         ;No more rows to process?
      Result.l = myFreeResult(*MySqlResult)
      myClose(program\dbHandle)                                                   ; Close the database when no longer needed 
    EndIf
  EndIf
  ;------------------------------------------------------------------------------------------------
  For WidthSet = 0 To program\numberoffields - 1 
    SendMessage_(GadgetID(#Gadget_mysqltest_datalist), #LVM_SETCOLUMNWIDTH, WidthSet, #LVSCW_AUTOSIZE) 
  Next WidthSet 
  ;------------------------------------------------------------------------------------------------
  SetWindowTitle(#Window_mysqltest, #MainTitle + "Found all matching data, nothing left to do.") 
EndProcedure

;============================================================================================================================
; We need a ping thread to keep the server alive. Depends on Mysql_.pb (not being used at the moment)
;============================================================================================================================

Procedure PingServer()
  PingTop:
    Delay(50000)                                        ; Delay 5 minutes before the ping event
    program\mysqlping = 0                               ; Initial value should be empty
    program\mysqlping = myPingServer(program\dbHandle)  ; Ping the server now and get the result
    If program\mysqlping = 0
      program\connect = 1
    Else
      program\connect = 0
      program\connecttries = 0
      Repeat
        program\connecttries + 1
        program\connect = myRealConnect(program\dbHandle, server\hostname, server\username, server\password, server\database, Val(server\portaddr), 0, #MySQL_CLIENT_COMPRESS)
        If program\connect
        EndIf
      Until Result Or program\connecttries > 3
    EndIf
  Goto PingTop
EndProcedure

;============================================================================================================================
; Load the last login saved by the user if there was one 
;============================================================================================================================

Procedure LoadLastLogin()
  If OpenFile(0, "LastLogin.txt") <> 0 
    SetGadgetText(#Gadget_mysqltest_hostbox,     ReadString(0)) 
    SetGadgetText(#Gadget_mysqltest_userbox,     ReadString(0)) 
    SetGadgetText(#Gadget_mysqltest_passwordbox, ReadString(0)) 
    SetGadgetText(#Gadget_mysqltest_databasebox, ReadString(0)) 
    SetGadgetText(#Gadget_mysqltest_portbox,     ReadString(0)) 
    If ReadString(0) = "1" 
      SetGadgetState(#Gadget_mysqltest_savelogin, 1) 
    EndIf 
    CloseFile(0) 
    SetWindowTitle(#Window_mysqltest, #MainTitle + "Last saved login loaded") 
  Else 
    SetWindowTitle(#Window_mysqltest, #MainTitle + "Couldn't find last login file") 
  EndIf 
EndProcedure

;============================================================================================================================
; Save the current login details if the user has selected the checkbox 
;============================================================================================================================

Procedure SaveCurrentLogin()
  If GetGadgetState(#Gadget_mysqltest_savelogin) = 1 
    If CreateFile(0, "LastLogin.txt") <> 1 
      WriteStringN(0, GetGadgetText(#Gadget_mysqltest_hostbox)) 
      WriteStringN(0, GetGadgetText(#Gadget_mysqltest_userbox)) 
      WriteStringN(0, GetGadgetText(#Gadget_mysqltest_passwordbox)) 
      WriteStringN(0, GetGadgetText(#Gadget_mysqltest_databasebox)) 
      WriteStringN(0, GetGadgetText(#Gadget_mysqltest_portbox)) 
      WriteStringN(0, "1") 
      CloseFile(0) 
    EndIf 
  EndIf 
EndProcedure

;============================================================================================================================
; Check if the ENTER key was pressed in the SQL query box and run query if it was
;============================================================================================================================

Procedure CheckEnter()
  FocusID = GetFocus_()                                         ; Get the id of the window/object that has focus 
  Select FocusID                                               ; Use the id in a gadget selection 
    Case GadgetID(#Gadget_mysqltest_querybox)                   ; Gadget is the query builder box 
      TestString.s = GetGadgetText(#Gadget_mysqltest_querybox)  ; Get the text from the query builder box 
      If Right(TestString.s, 1) = ";"
        NumLines = CountGadgetItems(#Gadget_mysqltest_querybox)
        For LineItems = 0 To NumLines - 1
          GetLine.s + GetGadgetItemText(#Gadget_mysqltest_querybox, LineItems, 0) + " "
        Next LineItems
        ConnectToDatabase(GetLine.s)                                ; Reusable data return routine 
        GetLine.s = ""
        ClearGadgetItemList(#Gadget_mysqltest_querybox)
      EndIf
      SetActiveGadget(#Gadget_mysqltest_querybox)
  EndSelect                                                    ; End the selection 
EndProcedure

;============================================================================================================================
; Try to connect to the database  and run the query
;============================================================================================================================

Procedure ConnectToDatabase(Query.s)
  If program\dbhandle
    server\hostname    = GetGadgetText(#Gadget_mysqltest_hostbox)
    server\username    = GetGadgetText(#Gadget_mysqltest_userbox)
    server\password    = GetGadgetText(#Gadget_mysqltest_passwordbox)
    server\database    = GetGadgetText(#Gadget_mysqltest_databasebox)
    server\portaddr    = GetGadgetText(#Gadget_mysqltest_portbox)
    If myRealConnect(program\dbHandle, server\hostname, server\username, server\password, server\database, Val(server\portaddr), 0, #MySQL_CLIENT_COMPRESS) <> 0 
      SetWindowTitle(#Window_mysqltest, #MainTitle + "Connected and getting your data, please wait") 
      GetSqlData(Query.s)
    Else 
      GetMySqlError(program\dbHandle)
      program\connect = 0 
    EndIf 
  Else 
    SetWindowTitle(#Window_mysqltest, #MainTitle + "SQL library was never loaded so we cannot connect to the database!!") 
  EndIf 
EndProcedure

;============================================================================================================================
; Dump the list to disk at the users' request with column headings
;============================================================================================================================

Procedure DumpListToDisk()
  Lines.l = CountGadgetItems(#Gadget_mysqltest_datalist)                    ; How many lines to save 
  DumpList.s = SaveFileRequester("Save returned query", "QueryDump.txt", "Text (*.txt)|*.txt|All files (*.*)|*.*", 0) 
  If CreateFile(0, DumpList.s) <> 0                                         ; Can we create the file?
    For Headings = 0 To program\numberoffields - 1                          ; Write and get the column headings too
      currentHeading.s = GetGadgetItemText(#Gadget_mysqltest_datalist, -1, Headings)
      outHeading.s + currentHeading.s + ";" 
    Next Headings
    WriteStringN(0, outHeading.s)                                           ; Write the heading string out
    For ListItems = 0 To Lines - 1                                          ; Now dump the data, lines first
      For totalFields = 0 To program\numberoffields - 1                     ; Now each column on a line
        currentField.s = GetGadgetItemText(#Gadget_mysqltest_datalist, ListItems, totalFields) 
        outString.s + currentField.s + ";" 
      Next totalFields 
      WriteStringN(0, outString.s) 
      outString.s = "" 
    Next ListItems 
    CloseFile(0) 
  Else 
    SetWindowTitle(#Window_mysqltest, #MainTitle + "Cannot save the list to disk, something went wrong") 
  EndIf 
EndProcedure

;============================================================================================================================
; Initialise the MySQL library
;============================================================================================================================

program\myLib                        = LoadLibraryM(?libmySQL_dll)

If program\myLib
  myInit.mysql_init                  = GetProcAddressM(program\myLib, "mysql_init")
  myErrno.mysql_errno                = GetProcAddressM(program\myLib, "mysql_errno")
  myError.mysql_error                = GetProcAddressM(program\myLib, "mysql_error")
  myRealConnect.mysql_real_connect   = GetProcAddressM(program\myLib, "mysql_real_connect")
  myRealQuery.mysql_real_query       = GetProcAddressM(program\myLib, "mysql_real_query")
  myStoreResult.mysql_store_result   = GetProcAddressM(program\myLib, "mysql_store_result")
  myFieldCount.mysql_field_count     = GetProcAddressM(program\myLib, "mysql_field_count")
  myUseResult.mysql_use_result       = GetProcAddressM(program\myLib, "mysql_use_result")
  myFetchRow.mysql_fetch_row         = GetProcAddressM(program\myLib, "mysql_fetch_row")
  myFetchLengths.mysql_fetch_lengths = GetProcAddressM(program\myLib, "mysql_fetch_lengths")
  myFreeResult.mysql_free_result     = GetProcAddressM(program\myLib, "mysql_free_result")
  myClose.mysql_close                = GetProcAddressM(program\myLib, "mysql_close")
  myAffectedRows.mysql_affected_rows = GetProcAddressM(program\myLib, "mysql_affected_rows")
  myNumFields.mysql_num_fields       = GetProcAddressM(program\myLib, "mysql_num_fields")
  myNumRows.mysql_num_rows           = GetProcAddressM(program\myLib, "mysql_num_rows")
  myPingServer.mysql_ping            = GetProcAddressM(program\myLib, "mysql_ping")
  myFetchField.mysql_fetch_field     = GetProcAddressM(program\myLib, "mysql_fetch_field")
  program\dbhandle = myInit.mysql_init(dbHandle)
Else
  MessageRequester("Critical Error", "The MySQL DLL library could not be found or started", 0)
  End
EndIf  

;============================================================================================================================
; Main event handling
;============================================================================================================================

If Window_mysqltest()
  LoadLastLogin()                                                   ; Load the last login saved by the user if there was one
  SetActiveGadget(#Gadget_mysqltest_querybox)
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow                                    ; Any close window events
        Select EventWindow()
          Case #Window_mysqltest              : program\quitvalue = 1
        EndSelect
      Case #PB_Event_Gadget                                         ; Any onscreen gadgets
        Select EventGadget()
          Case #Gadget_mysqltest_datalist     : 
          Case #Gadget_mysqltest_savelogin    : SaveCurrentLogin()  ; Save the current login details if the user has selected the checkbox 
          Case #Gadget_mysqltest_dumpbutton   : DumpListToDisk()    ; Dump the list to disk at the users' request 
          Case #Gadget_mysqltest_helpbutton   : 
          Case #Gadget_mysqltest_exitbutton   : program\quitvalue = 1
          Case #Gadget_mysqltest_querybox     : CheckEnter()        ; Check if the ENTER key was pressed in the SQL query box 
        EndSelect
    EndSelect
  Until program\quitvalue
  CloseWindow(#Window_mysqltest)
EndIf
End
Last edited by Fangbeast on Sat Apr 22, 2006 3:07 pm, edited 2 times in total.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

you're welcome fangbeast :wink:
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
CSAUER
Enthusiast
Enthusiast
Posts: 188
Joined: Mon Oct 18, 2004 7:23 am
Location: Germany

Post by CSAUER »

I am not able to get it run with PB4 B11. I receive always following error message:

Line 448: LoadLibraryM() is not a function, array, macro or linked list

Do I need to install any User Lib?
Any other idea what I am doing wrong?
Thanks for your support in advance.

Best Regards

CSAUER
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Post by Fangbeast »

CSAUER wrote:I am not able to get it run with PB4 B11. I receive always following error message:

Line 448: LoadLibraryM() is not a function, array, macro or linked list

Do I need to install any User Lib?
Any other idea what I am doing wrong?
Thanks for your support in advance.

Best Regards

CSAUER
Sorry, forgot to tell everyone about the DLL I was using. It's from PBOSL libs, specifically LoadDllMemory. You can get them from a link at purearea.net. I like loading a dll from memory instead of having it sit on the disk. Less for people to lose.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
CSAUER
Enthusiast
Enthusiast
Posts: 188
Joined: Mon Oct 18, 2004 7:23 am
Location: Germany

Post by CSAUER »

Thanks. I got it. This is a great program.

I can learn very much from your example. Thanks a lot.

I am wondering, that EXE-filesize is very big and the DLL is additionally required. I thought it is included into the EXE via IncludeBinary. But the EXE will not run without DLL.

Cheers
CSAUER
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Post by Fangbeast »

CSAUER wrote:Thanks. I got it. This is a great program.

I can learn very much from your example. Thanks a lot.

I am wondering, that EXE-filesize is very big and the DLL is additionally required. I thought it is included into the EXE via IncludeBinary. But the EXE will not run without DLL.

Cheers
CSAUER
The source code above is older than what I have now but from what I remember, the exe itself is large only with the DLL compiled inside it. The dll was around 1.2 meg (v5.x) if I remember correctly. Without it, the compiled exe is les than 25k (I think).

I should have some time tonight to have a look. The only other thing that's making the exe bigger is the PBOSL LoadDllFromMemory module but that's pretty small itself.

I'll have another look soon and update it for you. It will be today.

P.s. was this the link you got it from?? (This is the current exe) http://www.penguinbyte.com/apps/pbwebst ... rieval.rar
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Back from shopping

Post by Fangbeast »

Just tested, recompiled and tested the compiled code and it doesn't need the extra dll as it's already compiled inside the exe. Don't know what happened to your copy. Even my older source (Which I presume is the one you downloaded) works here without that problem.

If you want, I will upload another compiled (and working exe) to PureStorage for you to test.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
zenoncount
New User
New User
Posts: 2
Joined: Sun May 07, 2006 11:01 pm

Post by zenoncount »

Hi Fangbeast,

congrats for the good work. I just tested your code and wondered why the client keeps telling me "acces denied for xxx@localhost".

Debugging the values you send to the myRealConnect Procedure i saw, that my password (a case-sensitive one) was for some purpose always "lowercased" --> so instead of

Code: Select all

StringGadget(#Gadget_mysqltest_passwordbox,655,110,135,20,"",#PB_String_Password|#PB_String_LowerCase)
one should use

Code: Select all

StringGadget(#Gadget_mysqltest_passwordbox,655,110,135,20,"",#PB_String_Password)


and the problem is gone :-)

Thx for sharing your code!




Greetings from Bremen/Germany
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Post by Fangbeast »

Thanks. Improvements are always good but I can't rememebr what I did 5 months ago because when I code at all, I am using sqlite now.

MySQL is nice but I ended up not needing client/server any more.

The issue was that I would have had to fork out a lot of money to use it commercially in a project and I hadn't actually made any money yet (grin).

I made a clone of this for SqLite databases.
User avatar
HAnil
User
User
Posts: 87
Joined: Thu Feb 26, 2004 5:42 pm
Location: 28:58E 41:01N

Post by HAnil »

Welcome Fangbeast :D
PureBasic v5.22 LTS & Mac & Windows8
Post Reply