C87 wrote:Hello, Being new to PB I am wondering what I can use to create a data browser screen that is filled with data from an SQLite table.
In access you can create a browse screen by containing a datasheet onto a form. FRom that you can then move up and down and allow the user to make selections on a record with the mouse or a key press. There was a simpler version called browse in dBASE and Clipper that basically read the table onto the screen. Some refer to this type of screen as a GRID.
From the available HELP and other PB manuals, PB Survival Guide etc I cannot find anything like them at all, nor any Grid functions.
If someone could point me in the right direction I'd greatly appreciate it.
Many thanks, C87
Hello and welcome to the forum!
dBASE and Clipper like the old good days
Actually in PureBasic as so simple as TBrowse() was in Clipper 5, but it is more poweful.
Check the following code. It is not very elegant but I hope that gives you an idea:
Code: Select all
UseSQLiteDatabase()
EnableExplicit
Procedure CheckDatabaseUpdate(Database, Query$)
Protected Result
Result = DatabaseUpdate(Database, Query$)
If Result = 0
Debug DatabaseError()
EndIf
ProcedureReturn Result
EndProcedure
Procedure CreateDB(Database$)
If CreateFile(0, Database$)
CloseFile(0)
If OpenDatabase(0, Database$, "", "")
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')")
CloseDatabase(0)
Else
Debug "Can't open database !"
EndIf
Else
Debug "Can't create the database file !"
EndIf
EndProcedure
Procedure Browse(Database$, Table$, lstGadget)
Protected i
Protected TotalFields
Protected CurrRecord
Protected sRow.s
;~ Open Database
If OpenDatabase(0, Database$, "", "")
;~ Query the table and read all records
If DatabaseQuery(0, "SELECT * FROM " + Table$)
;~ Count the database fields
TotalFields = DatabaseColumns(0)
;~ Walk through the columns
For i = 0 To TotalFields - 1
;~ Begin to add columns from 1, because the column 0 ir reserved for record counter
AddGadgetColumn(lstGadget, i+1, Trim(DatabaseColumnName(0, i)), 100)
Next
;~ Initialize the counter and retrieve the records
CurrRecord = 1
While NextDatabaseRow(0)
;~ The Chr(10) is column separator
sRow = Trim(Str(CurrRecord)) + Chr(10)
;~ Retrieve all the fields of record
For i = 0 To TotalFields - 1
sRow + Trim(GetDatabaseString(0, i)) + Chr(10)
Next
;~ Add line to "Browse"
AddGadgetItem(lstGadget, -1, sRow)
;~ Increase the record counter
CurrRecord + 1
Wend
FinishDatabaseQuery(0)
CloseDatabase(0)
EndIf
EndIf
EndProcedure
Procedure BrowseDemo(Database$)
If OpenWindow(0, 0, 0, 700, 300, "Browse Demo", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ListIconGadget(0, 10, 10, 680, 280, "Counter", 75, #PB_ListIcon_GridLines|#PB_ListIcon_FullRowSelect)
;~ "Browse" the table
Browse(Database$, "food", 0)
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
EndProcedure
;~ Create the temporary database
Define Database$ = GetTemporaryDirectory()+"Database.sqlite"
CreateDB(Database$)
;~ Call the main function
BrowseDemo(Database$)
Just run it.
It creates and browse a SQLite database.
Regards.
Thanos