Code: Select all
Declare CheckDatabaseUpdate(Database.I, Query.S)
Declare CreateDemoDB()
Declare OpenWindow0(x = 0, y = 0, width = 600, height = 400)
Declare LoadParentList()
Declare LoadChildList()
UseSQLiteDatabase()
Global DatabaseFile.S = GetTemporaryDirectory() + "Database.sqlite"
Enumeration FormWindow
#Window0
EndEnumeration
Enumeration FormGadget
#ParentList
#ChildList
EndEnumeration
Procedure CheckDatabaseUpdate(Database.I, Query.S)
Result = DatabaseUpdate(Database, Query)
If Result = 0
Debug DatabaseError()
EndIf
ProcedureReturn Result
EndProcedure
Procedure CreateDemoDB()
If CreateFile(0, DatabaseFile)
CloseFile(0)
If OpenDatabase(0, DatabaseFile, "", "")
CheckDatabaseUpdate(0, "CREATE TABLE food (foodid INTEGER, name TEXT)")
CheckDatabaseUpdate(0, "CREATE TABLE attributes (attribid INTEGER, foodid INTEGER , name TEXT, value TEXT)")
CheckDatabaseUpdate(0, "INSERT INTO food (foodid, name) VALUES (1, 'apple'), (2, 'pear'), (3, 'banana');")
CheckDatabaseUpdate(0, "INSERT INTO attributes VALUES (1, 1, 'colour', 'red'), (2, 1, 'weight', '10'), (3, 1, 'taste', 'appley');")
CheckDatabaseUpdate(0, "INSERT INTO attributes VALUES (4, 2, 'colour', 'green'), (5, 2, 'weight', '5'), (6, 2, 'taste', 'peary');")
CheckDatabaseUpdate(0, "INSERT INTO attributes VALUES (7, 3, 'colour', 'yellow'), (8, 3, 'weight', '20'), (9, 3, 'taste', 'bananary');")
Else
Debug "Can't create the database file !"
End
EndIf
EndIf
EndProcedure
Procedure OpenWindow0(x = 0, y = 0, width = 600, height = 400)
OpenWindow(#Window0, x, y, width, height, "", #PB_Window_SystemMenu)
ListIconGadget(#ParentList, 10, 10, 580, 180, "Food", 100)
ListIconGadget(#ChildList, 10, 200, 580, 180, "Attribute", 100)
AddGadgetColumn(#ChildList, 1, "Value", 100)
BindGadgetEvent(#ParentList, @LoadChildList(), #PB_EventType_Change)
EndProcedure
Procedure LoadParentList()
ClearGadgetItems(#ParentList)
Query.S = "SELECT * FROM food;"
Index = 0
If DatabaseQuery(0, Query)
ClearGadgetItems(#ParentList)
While NextDatabaseRow(0)
; Add the parent item to the list.
AddGadgetItem(#ParentList, Index, GetDatabaseString(0, 1))
; Store the parent row id in the gadget for later.
SetGadgetItemData(#ParentList, Index, GetDatabaseLong(0, 0))
Index + 1
Wend
EndIf
FinishDatabaseQuery(0)
EndProcedure
Procedure LoadChildList()
ParentRow = GetGadgetState(#ParentList)
If ParentRow = -1
; Stop if no parent value is selected.
ProcedureReturn
EndIf
; Retrieve the parent id from the parent gadget.
ParentId = GetGadgetItemData(#ParentList, ParentRow)
Query.S = "SELECT * FROM attributes WHERE foodid = " + StrU(ParentId) + ";"
If DatabaseQuery(0, Query)
ClearGadgetItems(#ChildList)
While NextDatabaseRow(0)
; Add the child item to the list.
AddGadgetItem(#ChildList, Index, GetDatabaseString(0, 2) + #LF$ + GetDatabaseString(0, 3))
Wend
EndIf
FinishDatabaseQuery(0)
EndProcedure
; Main
If FileSize(DatabaseFile) = -1
CreateDemoDB()
EndIf
OpenDatabase(0, DatabaseFile, "", "")
OpenWindow0(50, 50)
LoadParentList()
Repeat
Until WaitWindowEvent(10) = #PB_Event_CloseWindow