Grids & data & arrays, oh my...

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Grids & data & arrays, oh my...

Post by bogs » Sun Feb 11, 2018 6:31 pm

Not feeling particularly bright today, so bear with me. Having figured out how to pop a db query into the dg using sql statements (different post), I wanted to see if I could figure out how to do it using (mostly) Lc's basic language.

I read the nifty lessons posted about it, and was curious to see if the 'array' method might suit my purposes, and whether it was as fast or faster than using pure sql.

The program consists of 4 cards, each displaying 1 table from a sqlite db. Each card is named the same as the table. My understanding of the array lesson is that you put one column into one keyed array, then set the dgData to that variable once populated. Since I want as much of this process automated as possible, I wrote it like this -

Code: Select all

   // set the variables used for this...
   put "" into tmpRecords // clear the variable...
   put 1 into tmpColNum // set up the first key...
   put "" into tmpColName // make sure the name is empty...
   // get the item to select ...
   repeat for each item x in tmpColumns
      if x is "Cover" then
         // drop it, this is retrieved when a row is selected...
      else
         put x into tmpColName
         // setup the sql statement...
         put "SELECT " & "`" & x & "`" &  " FROM " & tmpTableName into tmpSql
         // load up the column into an array...
         put revDataFromQuery(,return,tmpConnId,tmpSql) into tmpRecords[tmpColNum][tmpColName]         
      end if 
      // filter for impurities using rev instead of sql...
      // filter tmpRecords[tmpColNum][tmpColName] without tab
      // keep count of each item and use that number to put the information into a column in the dg...
      add one to tmpColNum
   end repeat   
   /* combine the array into datagrid format... 
   I tried the 'combine with return and tab', didn't work...*/
   // set the data into the dg...
   set the dgData of group "lstRecords" to tmpRecords
Walking through this in the debugger, it all seems to work great until that last line setting the dgData. Here are some shots of what it looks like -
populated variables -
Image
keys and data show up as I *think* it should look -
Image

Unfortunately, when it gets to the datagrid, it only appears one of two ways, a single line across the columns, correctly formatted but only showing one record, or diagonally across the grid, showing just one record.

I am sure I am missing one impossibly simple thing, if anyone can point it out, I'd sure appreciate it.
Image

Klaus
Posts: 13824
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Grids & data & arrays, oh my...

Post by Klaus » Sun Feb 11, 2018 7:27 pm

Hi Bogs,

not sure I can follow****, but the array needs to looks like this if you want to fill a datagrid with it:
tmpRecords
1
Title
Year
Format
...
2
Title
Year
Format
...
***Or do you want to deal with database fields that contain CRs (and/or TABs)?

Best

Klaus

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Grids & data & arrays, oh my...

Post by bogs » Sun Feb 11, 2018 7:49 pm

Oh I see, I'm constructing the array incorrectly! DOH :oops:

Hm, I think a 2nd repeat line will be in order for this. Thanks Klaus!
Image

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Grids & data & arrays, oh my...

Post by bogs » Sun Feb 11, 2018 8:27 pm

Just out of curiosity, since I haven't been able to find anything that says so, is there a way to deposit data into a dg by column number?

For instance, you have dgColumn "Name" (col. 1) and "address" (col. 2), you scoop up all the data from the db for "Name" and transfer it directly to dg col. 1 ?
Image

Post Reply

Return to “Talking LiveCode”