Access Database on Windows

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by wongchunkit.

Dear All,

I'm using Windows XP/98. I've a Access database called "Data.mdb" with a table "Data". I want to use PB to do something on it. I've read the example program "database.db". However, I don't want to open the dialog box to select the database file each time I run the program.

Is is possible that I use the "Data.mdb" on my PureBasice program directly?
It means that the end-user do not know which database they will access.

Thanks.

A Registered Beginer on PureBasic,
Wong Chun Kit
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Rings.

If you can use the example to show some tables/content of your Database,
it should also be possible to open without requester.
He Paul, that's a question for you ?

Getting better with a little help from my friends....thx Siggi
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

It's perfectly possible, I use it to access my movie database :). Just add an ODBC entry in the ODBC manager (see panel control) and use the OpenDatabase() commands with right params (including password if needed).

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by wongchunkit.

Thanks Fred,

Could you show me the sample pb code on OpenDatabase() commands?

I'm not sure what I should input in it.

A Registered Beginer on PureBasic,
Wong Chun Kit
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

Just use it like that:


If OpenDatabase(0, "movieodbc", "", "")
MessageRequester("Info", "Nice !", 0)
EndIf

But before, you must add a new entry call 'movieodbc' in the OBDC manager in the config panel of Windows. Add a new system entry, using the Access driver and select your database. I've done database tests with MySQL and Access and both works perfectly so I guess it should work for you too.

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by cor.

It should be nice if you programmatically can put an entry call in the ODBC datamanager within Purebasic, so no entry are needed from the user in ODBC manager which gonna use your program.


Cor

Just use it like that:


If OpenDatabase(0, "movieodbc", "", "")
MessageRequester("Info", "Nice !", 0)
EndIf

But before, you must add a new entry call 'movieodbc' in the OBDC manager in the config panel of Windows. Add a new system entry, using the Access driver and select your database. I've done database tests with MySQL and Access and both works perfectly so I guess it should work for you too.

Fred - AlphaSND
Using Windows 98 SE
Registered PB version : 2.90 (Windows)
--------------------------
C. de Visser
Author of Super Guitar Chord Finder
http://www.ready4music.com
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

Here is a little database explorer you can use to see your data.. Written in a hurry but should works well :). UPDATED: Resizeable GUI and new style for ListIconGadget (much better for Database).

Code: Select all

;
; Little Database Explorer written by AlphaSND
;
; Fully Resizeable GUI - Easy to do :)
;

If InitDatabase() = 0 
  MessageRequester("Error", "ODBC v3.0+ can't be opened.", 0)
  End 
EndIf

InitGadget(10)

;If OpenDatabaseRequester(0)
If OpenDatabase(0, "Ludo", "backup14", "1234")

  Procedure ResizeGUI()
    ResizeGadget(0,  10, 10, WindowWidth()-25, WindowHeight()-75)
    ResizeGadget(1,  10, WindowHeight()-51, 100, 22)
    ResizeGadget(2, 110, WindowHeight()-55, WindowWidth()-175, 22)
    ResizeGadget(3, WindowWidth()-65, WindowHeight()-55, 50, 22)
  EndProcedure
  
  Procedure WindowCallback(Window, Message, wParam, lParam)
    If Message = #WM_SIZE
      ResizeGUI()
      ProcedureReturn -1 ; Tell PureBasic internal handler than the message is already processed
    EndIf
  EndProcedure

  If OpenWindow(0, 0, 100, 640, 480, #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MinimizeGadget| #PB_Window_MaximizeGadget, "PureBasic DataBase Explorer - v1.0")

    If CreateGadgetList(WindowID())
      ListIconGadget(0, 0, 0, 0, 0, "",0)
      TextGadget    (1, 0, 0, 0, 0, "SQL Request:")
      StringGadget  (2, 0, 0, 0, 0, "Select * from lcr_employe")
      ButtonGadget  (3, 0, 0, 0, 0, "Go !")
      
      ResizeGUI()
    EndIf
    
    SetWindowCallback(@WindowCallback())
    
    ActivateGadget(2)
    
    Repeat
      EventID = WaitWindowEvent()
      
      Select EventID
        Case #PB_EventGadget
        
          Select EventGadgetID()
            Case 3
              Gosub ExecuteQuery
        
          EndSelect
      EndSelect
      
    Until EventID = #PB_EventCloseWindow
    
  EndIf
Else
  MessageRequester("Error", "The database can't be opened", 0)
EndIf

End

ExecuteQuery:
  
  #LVM_SETEXENDEDLISTVIEWSTYLE = #LVM_FIRST+54
  #LVS_EX_GRIDLINES = 1
  #LVS_EX_FULLROWSELECT = $20
  
  If DatabaseQuery(GetGadgetText(2))

    FreeGadget(0)
    *LI = ListIconGadget(0,  10, 10, WindowWidth()-20, WindowHeight()-75,DatabaseColumnName(1), 80)
    SendMessage_(*LI, #LVM_SETEXENDEDLISTVIEWSTYLE, #LVS_EX_GRIDLINES, -1)
    SendMessage_(*LI, #LVM_SETEXENDEDLISTVIEWSTYLE, #LVS_EX_FULLROWSELECT, -1)
    
    NbDatabaseColumns = DatabaseColumns()
    Dim ColumnType.b(NbDatabaseColumns)

    ColumnType(1) = DatabaseColumnType(1)
    For k=2 To NbDatabaseColumns
      AddGadgetColumn(0, k, DatabaseColumnName(k), 80)
      ColumnType(k) = DatabaseColumnType(k)
    Next
    
    CurrentLine = 0
    While (NextDatabaseRow())
    
      Content$ = ""
      For k=1 To NbDatabaseColumns
        
        Select ColumnType(k)
          Case 1
            Content$+Str(GetDatabaseLong(k))
            
          Case 2
            Content$+GetDatabaseString(k)

          Case 3
            Content$+StrF(GetDatabaseFloat(k),10)
          
          Default
            Content$+"UNKNOW"
            
        EndSelect
        
        Content$+Chr(10)
      Next

      AddListIconGadgetItem(0, CurrentLine, Content$, 0)
      CurrentLine+1
    Wend
  Else
    MessageRequester("Error", "Bad Query", 0)
  EndIf
  
Return

Edited by - fred on 15 February 2002 15:18:56
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by blueb.

Fred,
Very, very nice example.
Before today, I have not been very successful using ODBC and PureBasic.

It is appreciated,
--Bob
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by blueb.

One question on the Purebasic ODBC functions.

Some databases such as MS Access have many tables inside of them.

How can I 'list' them, so that I know which table to use a SQL query with?

Example: Access file called Extreme.MDB

Tables: Employee, Contacts, Dealers, Students, etc.

While it's easy to open Extreme.mdb with ODBC, how do you know what's in this file?
Unless you know the table names ahead of time, that is.

Regards,
--Bob
Post Reply