[Implemented] GetDatabaseNumber()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
USCode
Addict
Addict
Posts: 923
Joined: Wed Mar 24, 2004 11:04 pm
Location: Seattle

[Implemented] GetDatabaseNumber()

Post by USCode »

Implemented as DatabaseColumIndex()

A function like GetDatabaseNumber(#Database, column name) might be useful;

Then you could get database columns using the column's "name" as opposed to it's number. Maybe something like this:

With myDBStructureVar
\FIELD1 = GetDatabaseString(#Database, GetDatabaseNumber(#Database, "FIELD1"))
\OTHERFIELD = GetDatabaseString(#Database, GetDatabaseNumber(#Database, "OTHERFIELD"))
...

Probably not the most efficient way to do things but later if you added a NEW field to your DB table somewhere other than the bottom then you don't have to renumber the 2nd parameter to all your GetDatabaseString() calls ... like you would in the following example if you added a column to your table between FIELD1 and OTHERFIELD:

With myDBStructureVar
\FIELD1 = GetDatabaseString(#Database, 1)
\OTHERFIELD = GetDatabaseString(#Database, 2)
...
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: GetDatabaseNumber()

Post by IdeasVacuum »

Hi USCode
For now, Instead of using hard-coded numbers in the function, define the column names as constants, then you have only one area of code to modify and the code body is a bit easier to read too.

Code: Select all

;Original
#FIELD1     = 1
#OTHERFIELD = 2

\FIELD1 = GetDatabaseString(#Database, #FIELD1)
\OTHERFIELD = GetDatabaseString(#Database, #OTHERFIELD)

;Edited
#FIELD1     = 1
#NEW FIELD  = 2
#OTHERFIELD = 3

\FIELD1 = GetDatabaseString(#Database, #FIELD1)
\OTHERFIELD = GetDatabaseString(#Database, #OTHERFIELD)
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
USCode
Addict
Addict
Posts: 923
Joined: Wed Mar 24, 2004 11:04 pm
Location: Seattle

Re: GetDatabaseNumber()

Post by USCode »

Thanks IdeasVacuum, I appreciate the feedback.
However you did give me an idea for something more flexible and self-updating, albeit a bit slower, that I'm going to try out ... :idea:
User avatar
Kiffi
Addict
Addict
Posts: 1484
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: GetDatabaseNumber()

Post by Kiffi »

i have build a little Helper-Proc to get the values by passing the fieldname:

Code: Select all

Procedure.s GetDatabaseStringByFieldname(DB, Fieldname.s)
  Protected Counter
  For Counter = 0 To DatabaseColumns(DB) - 1
    If LCase(DatabaseColumnName(DB, Counter)) = LCase(Fieldname)
      ProcedureReturn GetDatabaseString(DB, Counter)
    EndIf
  Next
  Debug Fieldname.s + " not found"
EndProcedure

myFieldValue.s = GetDatabaseStringByFieldname(myDB, "myFieldname")
so i do not take care of the order of fields in my SQL-Query.

Greetings ... Kiffi
Hygge
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: GetDatabaseNumber()

Post by IdeasVacuum »

That is top-notch Kiffi :mrgreen:
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
swhite
Enthusiast
Enthusiast
Posts: 785
Joined: Thu May 21, 2009 6:56 pm

Re: GetDatabaseNumber()

Post by swhite »

In order to make this more efficient it would be better if this could be done once when the table is opened and the results stored in a MAP. Here is a sample I tried using ODBC. I think I would create a function that opens the table and then automatically loads the fields so they could be used to retrieve the values.

Code: Select all

If UseODBCDatabase()
   If OpenDatabase(1,"AMProAdv","","")
      If DatabaseQuery(1,"Select * From AMSched")
         NewMap AMSched.i()
         For ln=0 To DatabaseColumns(1)
            Debug DatabaseColumnName(1,ln)
            AddMapElement(AMSched(),DatabaseColumnName(1,ln))
            AMSched() = ln
         Next
         ForEach AMSched()
            Debug MapKey(AMSched())
            Debug AMSched()
         Next 
         While NextDatabaseRow(1)
            Debug GetDatabaseString(1,AMSched("DATE"))+" "+GetDatabaseString(1,AMSched("TIME1"))
         Wend
         FinishDatabaseQuery(1)
      EndIf
      CloseDatabase(1)
   EndIf
EndIf
Simon White
dCipher Computing
User avatar
Kiffi
Addict
Addict
Posts: 1484
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: GetDatabaseNumber()

Post by Kiffi »

swhite wrote:In order to make this more efficient it would be better if this could be done once when the table is opened and the results stored in a MAP.
good solution! Thanks!

Greetings ... Kiffi
Hygge
Fred
Administrator
Administrator
Posts: 18161
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: GetDatabaseNumber()

Post by Fred »

Just for the record, the following lines:

Code: Select all

AddMapElement(AMSched(),DatabaseColumnName(1,ln))
AMSched() = ln
can be written

Code: Select all

AMSched(DatabaseColumnName(1,ln)) = ln
AddMapElement() is almost never needed, unless you do very specific stuff.
swhite
Enthusiast
Enthusiast
Posts: 785
Joined: Thu May 21, 2009 6:56 pm

Re: GetDatabaseNumber()

Post by swhite »

Thanks I did not know that.

Simon
Simon White
dCipher Computing
Post Reply