relative field names from db

Just starting out? Need help? Post your questions and find answers here.
nad
Posts: 11
Joined: Sat Feb 10, 2024 1:41 pm

relative field names from db

Post 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
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: relative field names from db

Post by Peter »

You can use DatabaseColumnIndex():

Code: Select all

ReturnValue.s = GetDatabaseString(YourDatabase, DatabaseColumnIndex(YourDatabase, "YourTableField"))
nad
Posts: 11
Joined: Sat Feb 10, 2024 1:41 pm

Re: relative field names from db

Post 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
Post Reply