Page 1 of 2

FinishDatabase()

Posted: Sat Sep 12, 2009 8:09 am
by Tranquil
Hello Forum.

I need to adapt my old sources (porting them from 4.20 to 4.31 now).
With 4.30 FinishDatabase is introduced so I need to rework my code to free resources. But when exactly do I need this?

1.) Do I need to FinishDatabase-Queries on Update Commands using DatabaseQuery()?
2.) If so, do I also need to FinishDatabase if Update failes?
3.) Do I need to call FinishDatabase if a SELECT Command fails in DatabaseQuery?

This is not mentioned in the Docs. :-(

Re: FinishDatabase()

Posted: Sat Sep 12, 2009 9:11 am
by blueznl
I just ran into the same question, and did a little testrun. It doesn't seem to matter if you use it or not.

I assume PureBasic does a FinishDatabaseQuery() every time you do another DataBaseQuery() but yes, it's a bit unclear.

Re: FinishDatabase()

Posted: Sat Sep 12, 2009 12:52 pm
by Fred
No, you need it everytime you have finished to handle a query result. The doc here show how to use it: http://www.purebasic.com/documentation/ ... query.html. You only need to call it if the query call succeed.

Re: FinishDatabase()

Posted: Sat Sep 12, 2009 5:28 pm
by blueznl
Yes, I understand, but WHY? Because if I don't use it, it still seems to work...

Re: FinishDatabase()

Posted: Sat Sep 12, 2009 7:24 pm
by srod
Presumably because you will be causing memory leaks if you do not issue the call.

Re: FinishDatabase()

Posted: Sat Sep 12, 2009 9:09 pm
by blueznl
Perhaps, perhaps not. We need a dev to tell us...

Re: FinishDatabase()

Posted: Sun Sep 13, 2009 1:10 am
by Fred
If we said it's necessary, that's for a reason ;). You will have a leak if not used.

Re: FinishDatabase()

Posted: Sun Sep 13, 2009 3:27 am
by Rook Zimbabwe
OK so do you think the placement in my procedure is correct?

Code: Select all

Procedure SendTIKDB()
  ; ***
  Result = CountGadgetItems(#ListIcon_TICKET)
  Result = Result - 1
  itz$ = Str(Result)
  ticketnumber$ = GetGadgetText(#Text_TIKNUM)
  If ticketnumber$ = ""
    MessageRequester("NO TICKET NUMBER", "Please get a ticket number" + Chr(10) + "from SERVER!")
    ProcedureReturn #False
  EndIf
  tablenumber$ = GetGadgetText(#Text_TABNUM)
  If tablenumber$ = ""
    MessageRequester("NO TABLE NUMBER", "Please Select a Table!")
    ProcedureReturn #False
  EndIf
  EMPLOYEE$ = GetGadgetText(#Text_EMP)
  ticketnumber$ = GetGadgetText(#Text_TIKNUM)
  tablenumber$ = GetGadgetText(#Text_TABNUM)
  dater$ = Str(Date())
  status$ = "OPEN"
  For lx = 0 To Result
    item$ = GetGadgetItemText(#ListIcon_TICKET, lx, 2)
    cost$ = GetGadgetItemText(#ListIcon_TICKET, lx, 3)
    If cost$ = ""
      cost$ = "0"
    EndIf
    cog$ = GetGadgetItemText(#ListIcon_TICKET, lx, 4)
    If cog$ = ""
      cog$ = "0"
    EndIf
    taxes$ = GetGadgetItemText(#ListIcon_TICKET, lx, 5)
    If taxes$ = ""
      taxes$ = "0"
    EndIf
    ASTtype$ = GetGadgetItemText(#ListIcon_TICKET, lx, 6)
    If ASTtype$ = ""
      ASTtype$ = "99"
    EndIf
    AST$ = GetGadgetItemText(#ListIcon_TICKET, lx, 7)
    If AST$ = ""
      AST$ = "0"
    EndIf
    
    If GetGadgetItemText(#ListIcon_TICKET, lx, 1) < Chr(167)
      query$ = "INSERT INTO DAYTICKET ([CHECKNUMBER], [SERVERID], [TABLE], [ITEM], [COST], [COG],[TAXES],[ASTTYPE],[AST],[STATUS], [TIMER]) VALUES ('" + ticketnumber$ + "', '" + eID$ + "', '" + tablenumber$ + "', '" + item$ + "', '" + cost$ + "', '" + cog$ + "', '" + taxes$ + "','" + asttype$ + "','" + ast$ + "','" + status$ + "', '" + dater$ + "');" ; ***
      DatabaseQuery(#DatabaseTIK, query$)
    EndIf
    
  Next
  FinishDatabaseQuery(#DatabaseTIK) ; or should I put it in the FOR/NEXT LOOP
  
EndProcedure
Or should it be inside the for/next loop?

It seems to work either way!

Re: FinishDatabase()

Posted: Sun Sep 13, 2009 10:09 am
by blueznl
Fred wrote: If we said it's necessary, that's for a reason ;). You will have a leak if not used.
Okay.

To complete the question, would a new DatabaseQuery() automatically call FinishDatabase() before executing the query?

(I will update the Survial Guide.)

Re: FinishDatabase()

Posted: Sun Sep 13, 2009 12:24 pm
by Fred
Yes it does, but it's a good idea to release the resources (connection, memory) as soon as possible (and you will have a problem with your last query anyway).

Re: FinishDatabase()

Posted: Thu Aug 25, 2011 9:13 pm
by nigel
Am I correct in thinking that exiting a purebasic application automatically closes any open database connections ?

Am I correct in thinking that "FinishDatabaseQuery" is not required in relation to the "DatabaseUpdate" statement and applies only to the "DatabaseQuery" statement ?

Although I can understand why "FinishDatabaseQuery" should always be used after completion of a query, it also makes sense that the last query could be finished
by placing a "FinishDatabaseQuery" statement before any "End" statement which causes a program exit (Assuming I am not talking hogwash here). I'm not recommending
this as a good practice but just thinking this might catch anything that has been accidentally missed by the coder.

I am assuming the "FinishDatabaseQuery" statement does not trigger an error condition of any sort if it is used without executing a database query beforehand.

Thanks

Re: FinishDatabase()

Posted: Fri Aug 26, 2011 4:55 am
by netmaestro
Am I correct in thinking that "FinishDatabaseQuery" is not required in relation to the "DatabaseUpdate" statement and applies only to the "DatabaseQuery" statement?
Sorry, that's wrong. If you use DatabaseUpdate you need FinishDatabaseQuery or you will have a leak.

[edit] Sorry, I'm wrong about you being wrong. It's wrong squared! :D

Seriously, Fred put it to rest and said FinishDatabaseQuery() only applies to DatabaseQuery() (which actually makes sense)

Re: FinishDatabase()

Posted: Fri Aug 26, 2011 6:57 am
by Fangbeast
netmaestro wrote:
Am I correct in thinking that "FinishDatabaseQuery" is not required in relation to the "DatabaseUpdate" statement and applies only to the "DatabaseQuery" statement?
Sorry, that's wrong. If you use DatabaseUpdate you need FinishDatabaseQuery or you will have a leak.
The PB help manual doesn't mention this for DatabaseUpdate, only databasequery. Where did Fred says this needs to be done? (I must have missed it somewhere).

If this is right, I have a lot of updating (no pun intended) to be done.

Re: FinishDatabase()

Posted: Fri Aug 26, 2011 7:01 am
by netmaestro
Look at the sample code provided with DatabaseUpdate...

[edit] Look closer than me... I didn't see everything... :oops:

Oh Shut up, frogs are people too :mrgreen:

hehe. Good you got your answer.

Re: FinishDatabase()

Posted: Fri Aug 26, 2011 9:31 am
by Fangbeast
netmaestro wrote:Look at the sample code provided with DatabaseUpdate...
I'm using PB4.51 and the example leads me to believe that the FinishDatabaseQuery(#Database) statement matches the first statement which is DatabaseQuery(#Database????

Would there not then, be two FinishDatabaseQuery(#Database), one for each statement or:

a: I am getting this totally wrong or
b: We are talking about pb 4.6 (which I am not running or
c: All of the above?

From pb 4.51 help:

Code: Select all

  ; First, connect to a database with an employee table  ;
  If DatabaseQuery(#Database, "SELECT * FROM employee")
      While NextDatabaseRow(#Database)
      DatabaseUpdate(#Database, "UPDATE employee SET checked=1 WHERE id="+GetDatabaseString(#Database, 0)) 
    Wend
    FinishDatabaseQuery(#Database)
  EndIf