how do i refer to a database field by name?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
jesse
Posts: 205
Joined: Thu Nov 11, 2010 6:32 pm

how do i refer to a database field by name?

Post by jesse » Fri Jun 24, 2011 5:19 pm

I am using the following code to return one record from a dataabase. My answer ptext shows every field and
of course answer the second item shows the second field but how can i refer to the fields by name like
answer description of ptext?

Additionally using the second item shows only part of the second item because there is a tab or space within the data
that confuses LC. anyway to address that? I also included my raw data that live code returns below you can
see how the description should be one field but it being treated as two apparently.

Code: Select all

on doRead productId  ----------------------------------------------------------
   if not Connected() then exit to top

   put "SELECT * FROM products  WHERE ID = '"& productId&"'" into tSql
   answer tSql
     put revQueryDatabase( conID, tSQL) into theCursor
     put the result into temp
     if not (the result >0) then
          answer "ERROR=" & the result
          exit to top
     end if
     put revDatabaseColumnNames(theCursor) into theFields -- get field names from table
     put the result into temp
     if the result > 0 then
     end if
     put revdb_querylist(,,conID,tSQL) into ptext
     answer ptext
     answer the second item of ptext
end doRead

1011 1 3/8" Bison Belt Made from American bison leather, the #1011 belt is 1 3/8" with double edge stitching. Features U.S. made solid brass buckle, as shown. Buckle end of belt is machine-sewn with buckle permanently attached. The belt is about 1/4 inch thick in the middle, and tapers to about 3/32nds of an inch to the edge

ID: 1011
TITLE: 1 3/8" Bison Belt
DESCRIPTION: Made from American bison leather, the #1011 belt is 1 3/8" with double edge stitching. Features U.S. made solid brass buckle, as shown. Buckle end of belt is machine-sewn with buckle permanently attached. The belt is about 1/4 inch thick in the middle, and tapers to about 3/32nds of an inch to the edge
IMAGE: 1011-120.jpg
Deving on WinVista Home Prem. SP2 32 bit. Using LiveCode 4.6.1 Pro Build 1392

dglass
Posts: 519
Joined: Thu Sep 24, 2009 9:10 pm
Contact:

Re: how do i refer to a database field by name?

Post by dglass » Fri Jun 24, 2011 5:49 pm

Code: Select all

put revdb_querylist(,,conID,tSQL) into ptext
Those two commas you have at the front of the parameter list are telling LC to use the default delimiters for fields (tab) and records (return/CR). If you have tabs or returns in your data you need to specify different delimiters.

Although, to be honest, I've looked at this (and other posts that have the same code), and have yet to understand why you are hitting your database twice for the same information.

You create your SQL and use revQueryDatabase to get a cursor/recordset that has the data in it, then you use that for getting the field names, only to then query the database again to get the exact same data.

Is there a particular reason you are doing it that way?

jesse
Posts: 205
Joined: Thu Nov 11, 2010 6:32 pm

Re: how do i refer to a database field by name?

Post by jesse » Fri Jun 24, 2011 7:13 pm

To be honest this was adapted from an example stack another user posted. Its helped me to learn how to use databases with datagrids.
I always thought the revdb_querylist was required to put the data into a workable fashion to put it in the datagrid. of course
the revDatabaseColumnNames puts the column names into the datagrid and the revdb_querylist puts the columns contents
into the datagrid. Is this wrong?
Deving on WinVista Home Prem. SP2 32 bit. Using LiveCode 4.6.1 Pro Build 1392

Post Reply