Page 1 of 1

relative field names from db

Posted: Sat Feb 10, 2024 1:51 pm
by nad
Completely new to spider so this may already been answered.
How do i reference field names from a db query by name instead of using relative numbers.
instead of using DatabaseField(1, 1),, I would like to use something like DatabaseField(itemNum) .
Problem is if I change the db table the relative numbers would require me changing all programs that use that table.

Thanks for the help

Re: relative field names from db

Posted: Sat Feb 10, 2024 1:55 pm
by Peter
You can use DatabaseColumnIndex():

Code: Select all

ReturnValue.s = GetDatabaseString(YourDatabase, DatabaseColumnIndex(YourDatabase, "YourTableField"))

Re: relative field names from db

Posted: Sat Feb 10, 2024 2:43 pm
by nad
Being new to this, will this work.
Query$ = "SELECT * FROM items"

; Retrieve column names dynamically
For FieldIndex = 1 To DatabaseColumns(1)
Local FieldName$ = ColumnName(1, FieldIndex)
SendString("<th>" + FieldName$ + "</th>")
; Store field name and index in the mapping array
FieldIndexMap{FieldName$} = FieldIndex
Next

Then I should be able to retrieve the data as;
DatabaseField(1,"itemNum"}
DatabaseFoe;d(1,onHand"}

is this correct..
Thanks for the help