FinishDatabase()

Just starting out? Need help? Post your questions and find answers here.
Tranquil
Addict
Addict
Posts: 952
Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe

FinishDatabase()

Post 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. :-(
Tranquil
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: FinishDatabase()

Post 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.
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: FinishDatabase()

Post 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.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: FinishDatabase()

Post by blueznl »

Yes, I understand, but WHY? Because if I don't use it, it still seems to work...
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: FinishDatabase()

Post by srod »

Presumably because you will be causing memory leaks if you do not issue the call.
I may look like a mule, but I'm not a complete ass.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: FinishDatabase()

Post by blueznl »

Perhaps, perhaps not. We need a dev to tell us...
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: FinishDatabase()

Post by Fred »

If we said it's necessary, that's for a reason ;). You will have a leak if not used.
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Re: FinishDatabase()

Post 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!
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: FinishDatabase()

Post 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.)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: FinishDatabase()

Post 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).
nigel
User
User
Posts: 62
Joined: Tue Feb 23, 2010 8:39 pm
Location: Canada

Re: FinishDatabase()

Post 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
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: FinishDatabase()

Post 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)
Last edited by netmaestro on Sat Aug 27, 2011 6:09 am, edited 1 time in total.
BERESHEIT
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: FinishDatabase()

Post 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.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: FinishDatabase()

Post 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.
Last edited by netmaestro on Sat Aug 27, 2011 6:11 am, edited 2 times in total.
BERESHEIT
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: FinishDatabase()

Post 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
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
Post Reply