Page 1 of 1

after doing a DatabaseUpdate program shuts down

Posted: Tue May 28, 2024 1:48 am
by RSrole
No errors are returned from anything, but after the DatabaseUpdate, it does the FinishDatabaseQuery and closedatabase. When it returns to the main loop it quits, without an error. Any thoughts?

Pi3Settings.pbf

Code: Select all

; Form Designer for Purebasic - 6.10
; Warning: this file uses a strict syntax, if you edit it, make sure to respect the Form Designer limitation or it won't be opened again.

;
; This code is automatically generated by the FormDesigner.
; Manual modification is possible to adjust existing commands, but anything else will be dropped when the code is compiled.
; Event procedures needs to be put in another source file.
;

Global Window_0

Global ExplorerList_0, ListIcon_0


Procedure OpenWindow_0(x = 0, y = 0, width = 490, height = 390)
  Window_0 = OpenWindow(#PB_Any, x, y, width, height, "Pi3 Settings", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
  ExplorerList_0 = ExplorerListGadget(#PB_Any, 10, 10, 460, 160, "", #PB_Explorer_AlwaysShowSelection | #PB_Explorer_GridLines | #PB_Explorer_FullRowSelect | #PB_Explorer_NoFolders | #PB_Explorer_NoParentFolder | #PB_Explorer_NoDirectoryChange | #PB_Explorer_NoDriveRequester | #PB_Explorer_AutoSort)
  ListIcon_0 = ListIconGadget(#PB_Any, 10, 180, 460, 200, "Column 1", 100, #PB_ListIcon_GridLines | #PB_ListIcon_FullRowSelect | #PB_ListIcon_AlwaysShowSelection)
EndProcedure

Procedure Window_0_Events(event)
  Select event
    Case #PB_Event_CloseWindow
      ProcedureReturn #False

    Case #PB_Event_Menu
      Select EventMenu()
      EndSelect

    Case #PB_Event_Gadget
      Select EventGadget()
      EndSelect
  EndSelect
  ProcedureReturn #True
EndProcedure
Pi3Settings.pb

Code: Select all

EnableExplicit
XIncludeFile "Pi3Settings.pbf"
Declare.i ProcDb(filename$)
Declare FixClipListRefresh(filename$)
UseSQLiteDatabase()

OpenWindow_0()
SetGadgetItemAttribute(ExplorerList_0, 0, #PB_Explorer_ColumnWidth , 130, 3)

RemoveGadgetColumn(listicon_0, 0)
Define w = GadgetWidth(listicon_0)
AddGadgetColumn(listicon_0, 0,"Setting", w/2)
AddGadgetColumn(listicon_0, 1,"Value", w/2)
;AddGadgetColumn(listicon_0, 1,"Count", 100)

Debug GetCurrentDirectory()
SetGadgetText(ExplorerList_0, GetCurrentDirectory() + "*.pi3")
Define event
Define CurForm
Define EvntType
Define GadgetNumber
Define i,txt$
;OnErrorGoto(?ErrorHandler)
Repeat
  Event = WaitWindowEvent() 
  EvntType = EventType()
  Select EventWindow() 
    Case window_0
      Select event
        Case #PB_Event_CloseWindow
          Break
        Case #PB_Event_Gadget           
          GadgetNumber = EventGadget()
          Select GadgetNumber
            Case ExplorerList_0
              If EvntType = #PB_EventType_Change
                i = GetGadgetState(GadgetNumber)
                If i < 0:Break:EndIf
                txt$ = GetGadgetItemText(GadgetNumber,i)
                If ProcDb(txt$) < 0:Continue:EndIf
                FixClipListRefresh(txt$)
              EndIf
          EndSelect
      EndSelect
  EndSelect
ForEver


End

Procedure.i ProcDb(filename$)
  ClearGadgetItems(listicon_0)
  If Len(filename$) = 0:ProcedureReturn:EndIf
  If OpenDatabase(0, filename$, "", "",#PB_Database_SQLite) = 0:ProcedureReturn:EndIf
  
  If DatabaseQuery(0, "SELECT * FROM Settings") = 0
    FinishDatabaseQuery(0)     
    CloseDatabase(0)        
    ProcedureReturn
  EndIf   

  
  Protected NewList Settings$()
  While NextDatabaseRow(0)
    AddElement(Settings$())
    Settings$() = GetDatabaseString(0, 0)+Chr(10)+GetDatabaseString(0,1)
  Wend
  FinishDatabaseQuery(0)
  CloseDatabase(0) 
  SortList(settings$(),#PB_Sort_Ascending | #PB_Sort_NoCase)
  
  Protected sel = -1
  Protected index  
  ForEach Settings$()
    AddGadgetItem(listicon_0, -1, settings$())
    If FindString(Settings$(),"Cliplist Refresh",0,#PB_String_NoCase)
      sel = index
    EndIf
    index = index + 1
  Next
  
  If sel = -1:ProcedureReturn:EndIf
  
  SetGadgetItemState(listicon_0, sel, #PB_ListIcon_Selected )
  SendMessage_(GadgetID(listicon_0), #LVM_SCROLL, 0, sel*18)
  SetActiveGadget(listicon_0)
  
  Protected txt$ = GetGadgetItemText(listicon_0,sel,1)
  
  If Abs(Val(txt$)) <> 2 
    ProcedureReturn -1
  Else
    ProcedureReturn sel
  EndIf
EndProcedure

Procedure FixClipListRefresh(filename$)  
  Protected cmd$ = "Insert or Replace into Settings(Setting,Data) VALUES ('Cliplist Refresh','0')"
  If OpenDatabase(1, filename$, "", "",#PB_Database_SQLite) 
    If DatabaseUpdate(1, cmd$) <> 0 
      MessageRequester("Pi3 Settings","Changed 'Cliplist Refresh' from FTP to Serial")
    Else
      MessageRequester("Pi3 Settings", "Database error: " + DatabaseError())
    EndIf            
    FinishDatabaseQuery(1)     
    CloseDatabase(1)
  EndIf  
EndProcedure

Re: after doing a DatabaseUpdate program shuts down

Posted: Tue May 28, 2024 6:12 am
by Fips
I cant test right now if that causes the problem but DatabaseUpdate() does not require a FinishDatabaseQuery().

Re: after doing a DatabaseUpdate program shuts down

Posted: Tue May 28, 2024 9:06 am
by infratec
And FinishDatabase() needs only to be called if DataBaseQuery() returns successfull.
It should not be called when the query failed.

Re: after doing a DatabaseUpdate program shuts down

Posted: Tue May 28, 2024 9:23 pm
by RSrole
Thanks

I removed FinishDatabaseQuery() after the DatabaseUpdate() and still quits

And the DatabaseQuerys are successful so I don't think that's a factor.

It's a mystery to me. I think I'm doing something wrong, but can't spot it.

Re: after doing a DatabaseUpdate program shuts down

Posted: Tue May 28, 2024 9:50 pm
by Comfort

Code: Select all

If i < 0:Break:EndIf
txt$ = GetGadgetItemText(GadgetNumber,i)
If ProcDb(txt$) < 0:Continue:EndIf
Recode this, removing Break and maybe losing that Continue.

Re: after doing a DatabaseUpdate program shuts down

Posted: Wed May 29, 2024 12:43 am
by boddhi
Hi,
RSrole wrote: It's a mystery to me. I think I'm doing something wrong, but can't spot it.
• First, could you put an example of your database with fake data if needed?

• Secondly, a few practical tips (non-mandatory, of course :wink: ):
    • Group together a few instruction sets whenever possible, which might save you writing, e.g:

Code: Select all

Procedure OpenWindow_0(x = 0, y = 0, width = 490, height = 390)
  Window_0 = OpenWindow(#PB_Any, x, y, width, height, "Pi3 Settings", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
  ExplorerList_0 = ExplorerListGadget(#PB_Any, 10, 10, 460, 160, "", #PB_Explorer_AlwaysShowSelection | #PB_Explorer_GridLines | #PB_Explorer_FullRowSelect | #PB_Explorer_NoFolders | #PB_Explorer_NoParentFolder | #PB_Explorer_NoDirectoryChange | #PB_Explorer_NoDriveRequester | #PB_Explorer_AutoSort)
  ListIcon_0 = ListIconGadget(#PB_Any, 10, 180, 460, 200, "Column 1", 100, #PB_ListIcon_GridLines | #PB_ListIcon_FullRowSelect | #PB_ListIcon_AlwaysShowSelection)
EndProcedure
and

Code: Select all

SetGadgetItemAttribute(ExplorerList_0, 0, #PB_Explorer_ColumnWidth , 130, 3)
RemoveGadgetColumn(listicon_0, 0)                               ; *
Define w = GadgetWidth(listicon_0)
AddGadgetColumn(listicon_0, 0,"Setting", w/2)               ; *
AddGadgetColumn(listicon_0, 1,"Value", w/2)
can be “shortened” like this:

Code: Select all

Procedure OpenWindow_0(x = 0, y = 0, width = 490, height = 390)
  Window_0 = OpenWindow(#PB_Any, x, y, width, height, "Pi3 Settings", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
  ExplorerList_0 = ExplorerListGadget(#PB_Any, 10, 10, 460, 160, "", #PB_Explorer_AlwaysShowSelection | #PB_Explorer_GridLines | #PB_Explorer_FullRowSelect | #PB_Explorer_NoFolders | #PB_Explorer_NoParentFolder | #PB_Explorer_NoDirectoryChange | #PB_Explorer_NoDriveRequester | #PB_Explorer_AutoSort)
  ListIcon_0 = ListIconGadget(#PB_Any, 10, 180, 460, 200, "Setting", 100, #PB_ListIcon_GridLines | #PB_ListIcon_FullRowSelect | #PB_ListIcon_AlwaysShowSelection)
  SetGadgetItemAttribute(ExplorerList_0, 0, #PB_Explorer_ColumnWidth , 130, 3)
  Debug GetCurrentDirectory()
  SetGadgetText(ExplorerList_0, GetCurrentDirectory() + "*.pi3")
  Define w = GadgetWidth(listicon_0)
  SetGadgetItemAttribute(listicon_0, 0, #PB_Explorer_ColumnWidth, w/2,0)    ; *
  AddGadgetColumn(listicon_0, 1,"Value", w/2)
EndProcedure
    Here:
      1) create your ListIcon with column 0 'Setting'.
      2) Then calculate the width of the gadget
      3) Resize column 0
      4) Add column 1 'Value
    This saves you to delete all the columns (the one) and recreating them.

    • In the event loop, for the same object and the same event, when you have a lot of lines of code, prefer a procedure instead, place the code in it and call it from the event loop. It makes the code easier to read and costs nothing :wink: :) .

    • Open your database only once and close it only when you quit the application or when you really don't need it anymore. That way, you don't have to do a whole lot of opening and closing. It won't interfere with access to the database by other applications, if needed.

• Third, here:

Code: Select all

  If DatabaseQuery(0, "SELECT * FROM Settings") = 0
    FinishDatabaseQuery(0)     ; useless since the query failed!
    CloseDatabase(0)        
    ProcedureReturn
  EndIf   
• Fourth, did you execute the code in debug mode? to check possible bugs?

Re: after doing a DatabaseUpdate program shuts down

Posted: Wed May 29, 2024 12:56 am
by boddhi
PS: To avoid repetition, can you delete your other post on the same subject, with the same title? :wink:

Re: after doing a DatabaseUpdate program shuts down

Posted: Wed May 29, 2024 7:12 am
by Kiffi
boddhi wrote: Wed May 29, 2024 12:56 amcan you delete your other post on the same subject, with the same title?
[X] Done

Re: after doing a DatabaseUpdate program shuts down

Posted: Wed May 29, 2024 8:18 am
by C87
I've said it before and I will say it again.

If you are developing a database application then use a database language designed specifically for the job and this sort of issue will not occur.
MS-Access is a sound RAD that will allow you to create applications as indicated by its name - Rapidly (very rapidly)

Also I strongly advise you to look at Harbour and Harbour MinuGUI. Both of which are rock solid and HUGELY comprehensive database development software environments. They are totally free and can be compiled into executables on Windows, Linux, Apple and also used to create Android applications. Harbour and HMG are still being updated today and shows no sign of that not continuing. They will also shorten your development time considerably.

Re: after doing a DatabaseUpdate program shuts down

Posted: Wed May 29, 2024 1:36 pm
by blueb
I think if you're going to use SQLite, you really don't need to learn a new language.

Try out DB Browser for SQLite (DB4S) https://sqlitebrowser.org/

Anything you want to do with SQLite can be done in this environment. They even have a version that works with encryption. Combined with PureBasic as a front-end.. What more could you want?

If you do have a larger need, there's always: MariaDB and PureBasic. :D

Re: after doing a DatabaseUpdate program shuts down

Posted: Wed May 29, 2024 3:10 pm
by boddhi
blueb wrote: I think if you're going to use SQLite, you really don't need to learn a new language.
+1

I use it in many of my apps and all work very fine.

Re: after doing a DatabaseUpdate program shuts down

Posted: Wed May 29, 2024 8:12 pm
by RSrole
Okay, I found the problem(s)

Code: Select all

          Select GadgetNumber
            Case ExplorerList_0
              If EvntType = #PB_EventType_Change
                i = GetGadgetState(GadgetNumber)
                If i < 0:Break:EndIf
                txt$ = GetGadgetItemText(GadgetNumber,i)
                If ProcDb(txt$) < 0:Continue:EndIf
                FixClipListRefresh(txt$)
                While WindowEvent() 
                  Delay(10)
                Wend
                ProcDb(txt$)
              EndIf
          EndSelect
After the FixClipListRefresh(txt$) I had to add the While/wend to eat any events and then reload the Db table in the iconlist. In that case the setting I had changed now shows as 0 so no processing is done. I think I was creating a stack overflow.

Thanks to all for your suggestions