Ideal would be an array as a return.
However, since PB neither return the numbers of columns ands rows,
the array should be dimensioned constantly dynamic. But I do not like that, even for performance reasons.
Now I have the idea to save the data into a map and use as key the combination of row and column.
Are there any reasons that speak against this practice?
Code: Select all
;
; ------------------------------------------------------------
;
; PureBasic - Database example file
;
; (c) Fantaisie Software
;
; ------------------------------------------------------------
;
UseSQLiteDatabase()
Procedure CheckDatabaseUpdate(Database, Query$)
Result = DatabaseUpdate(Database, Query$)
If Result = 0
Debug DatabaseError()
EndIf
ProcedureReturn Result
EndProcedure
DatabaseFile$ = GetTemporaryDirectory()+"/Database.sqlite"
Global NewMap SQLData.s()
If CreateFile(0, DatabaseFile$)
CloseFile(0)
If OpenDatabase(0, DatabaseFile$, "", "")
CheckDatabaseUpdate(0, "CREATE TABLE food (name CHAR(50), weight INT)")
CheckDatabaseUpdate(0, "INSERT INTO food (name, weight) VALUES ('apple', '10')")
CheckDatabaseUpdate(0, "INSERT INTO food (name, weight) VALUES ('pear', '5')")
CheckDatabaseUpdate(0, "INSERT INTO food (name, weight) VALUES ('banana', '20')")
If DatabaseQuery(0, "SELECT name, weight FROM food")
n = 0
While NextDatabaseRow(0)
Debug "Add Row: " + Str(n) + " | " + GetDatabaseString(0, 0) + ";" + GetDatabaseString(0, 1)
SQLData(Str(n) + ";0") = GetDatabaseString(0, 0)
SQLData(Str(n) + ";1") = GetDatabaseString(0, 1)
n + 1
Wend
FinishDatabaseQuery(0)
EndIf
CloseDatabase(0)
Else
Debug "Can't open database !"
EndIf
Else
Debug "Can't create the database file !"
EndIf
Debug "Row 0 = " + SQLData("0;0") + ";" + SQLData("0;1")
Debug "Row 1 = " + SQLData("1;0") + ";" + SQLData("1;1")
Debug "Row 2 = " + SQLData("2;0") + ";" + SQLData("2;1")


