How to align the display records in the polygrid?

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

lemodizon
Posts: 219
Joined: Thu Apr 05, 2018 3:33 pm

How to align the display records in the polygrid?

Post by lemodizon » Tue May 28, 2024 4:08 pm

hello everyone,

I was able to get the records from the database using xamp mysql and display it to the polygrid.

unfortunately the records displayed scattered in my polygrid. please help me how to arrange

here's my code

Code: Select all

function SQLStatement
   global gConnectionID,gPrjID
   local tSQLStatement, tlist
   put "SELECT cpsn, vps FROM tblartistproj WHERE ID = '"& gPrjID &"'  " into tSQLStatement
   put revDataFromQuery(tab, return, gConnectionID, tSQLStatement) into tlist
   
end SQLStatement
   
 command SQLQueryDisplay
   local tlist
   put SQLStatement() into tlist
   
   set the pgText of widget "PgDetails" of stack "ViewArtistOutputSheet" to tlist
 
end SQLQueryDisplay
here is the
Image

it doesn't aligned in each item.
Attachments
polyfrid_displayed.png
Thank you & God Bless Everyone :wink:

Regards,
lemodizon

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Re: How to align the display records in the polygrid?

Post by bn » Tue May 28, 2024 5:24 pm

Hi lemodizon,

make a stack with a PolyGrid and a button

set the script of the button to

Code: Select all

on mouseUp
   put the colorNames into tCNames
   repeat with i = 1 to 20
      repeat with j = 1 to 5
         put any line of tCNames & tab after tCollect
      end repeat
      delete char -1 of tCollect
      put cr after tCollect
   end repeat
   delete char -1 of tCollect
   set the pgText of widget 1 to tCollect
end mouseUp
This goes to show how to populate a PolyGrid using pgText.
It populates the body of the Polygrid.

Make sure your result from the database query is tab delimited.

Kind regards
Bernd

Edited for an error I made thinking that the first line of pgText is the columnNames; it is not.

lemodizon
Posts: 219
Joined: Thu Apr 05, 2018 3:33 pm

Re: How to align the display records in the polygrid?

Post by lemodizon » Wed May 29, 2024 5:57 pm

Hi Bernd,

This is my revised code based from the suggested code you've given to me.

Code: Select all

####################################################################
function SQLStatement
   global gConnectionID,gPrjID
   local tSQLStatement, tlist , 
   set the itemDelimiter to tab
   put "SELECT cpsn, vps FROM tblartistproj WHERE ID = '"& gPrjID &"'  " into tSQLStatement
   put revDataFromQuery(tab,return, gConnectionID, tSQLStatement) into tlist
   
   
end SQLStatement

####################################################################

//// SQL STATEMENT QUERY - DISPLAY

command SQLQueryDisplay
   local tlist
   
   put SQLStatement() into tlist
   
   set the itemDelimiter to tab
   --column 2 column will do
   repeat with i = 1 to 3 
      
      --rows
      repeat with j = 1 to 2
         
         put any line of tlist  &tab after  tCollect
         
      end repeat
      delete char -1 of tCollect
      put  cr after tCollect
      
   end repeat
   delete char -1 of tCollect
   set the pgText  of widget 1 to tCollect
   

end SQLQueryDisplay


it displayed the records from the database but can you teach me on how to fix the ff:

1. how can i remove extra tab in the column of polygrid
2. every time i clicked the button there is changes in displaying the records sometimes the record is in the col1 next click it's in the col3. (random displayed)
3. how to remove the duplicates coz i noticed it duplicates because of the repeat.
4. in my database i separated the fields of the cps and vps. do i have to merge the two fields? or separate fields?


i'm kinda complicated and also confuse

thanks for the time for helping me.

Image
Attachments
polygrid2.png
Thank you & God Bless Everyone :wink:

Regards,
lemodizon

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Re: How to align the display records in the polygrid?

Post by bn » Wed May 29, 2024 7:59 pm

Hi lemondizon,

It all depends how the data you get from the sql-query.

How are the columns delimited? By comma, by tab or what delimiter.

Here I assume that they are comma delimited
rigtest,10.5
r4,4.5
r4,4.5
IronMan,10.5
Captain America,5.5
SpiderMan,5.0
Superman,6.6
put this into a field "fData"


###############
To set the header (column names) make a button:

Code: Select all

on mouseUp
   put "cpsn,vps" into tHeader
   set the pgColumnNames of widget 1 to tHeader
end mouseUp
This sets up two columns.

###############
To fill in data make a button:

Code: Select all

on mouseUp
   SQLQueryDisplay
end mouseUp

command SQLQueryDisplay
   local tlist
   
   ## use your sql list instead of my "getList"
   put getList() into tList
   
   set the itemDelimiter to tab
   -- lines
   repeat with i = 1 to the number of lines of tList
      
      --rows
      repeat with j = 1 to 2
         put item j of line i tList & tab after tCollect
      end repeat
      delete char -1 of tCollect ## here a tab
      put  cr after tCollect
      
   end repeat
   delete char -1 of tCollect
   set the pgText of widget 1 to tCollect
   
end SQLQueryDisplay

function getList 
   put field "fData" into tData
   ## here the list is changed from a comma delimited list to a tab delimited list
   replace comma with tab in tData
   return tData
end getList

It would be easier if you could post a sample of what you get from your sql-query.

KInd regards
Bernd

lemodizon
Posts: 219
Joined: Thu Apr 05, 2018 3:33 pm

Re: How to align the display records in the polygrid?

Post by lemodizon » Thu May 30, 2024 4:45 am

Hi Bernd,

this is the sql-qry output from my data

Image

I will revise my code hope i can get it right. thanks


based from your example your sql-qry has comma but mine has no comma i guess thats why it doesn't display properly

Image
sql.png
here is the format from the db that should display to the polygrid

Image
Attachments
db.png
db.png (7.08 KiB) Viewed 4077 times
sql-qry.png
Thank you & God Bless Everyone :wink:

Regards,
lemodizon

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Re: How to align the display records in the polygrid?

Post by bn » Thu May 30, 2024 9:34 am

Hi lemodizon,
based from your example your sql-qry has comma but mine has no comma i guess thats why it doesn't display properly
I looked at your sql-query and see the the column delimiter is set to tab and the line delimiter is set to return.

You should be able to use the code I provided without "replace comma with tab" since your sql result is already tab / return formatted.

If you put your query in a field it should display well but in your first image it looks funny.
Maybe something is not working in your database/query? I do not use databases and have no experience with sql.

You could zip a simple .sqlite file as zipped file and I could try to read it.

Kind regards
Bernd

stam
Posts: 3072
Joined: Sun Jun 04, 2006 9:39 pm

Re: How to align the display records in the polygrid?

Post by stam » Thu May 30, 2024 3:27 pm

I would argue that instead of using this method, use a database cursor and add the columns to the keys of an array.

I do this regularly - due to a month of near-daily on-calls, there is not chance for me to get any coding done, but can post an example of what I do at some point when I have time (probably in the next week or 2 if this hasn't been solved)

The benefit of using a cursor -> array is you don't have to worry about delimiters or any such tricky subject - just assign the the resting array directly to the pgData.
This means you would be able to include text with returns or other delimiters without breaking the process.

Stam

lemodizon
Posts: 219
Joined: Thu Apr 05, 2018 3:33 pm

Re: How to align the display records in the polygrid?

Post by lemodizon » Sun Jun 02, 2024 5:21 pm

bn wrote:
Thu May 30, 2024 9:34 am
Hi lemodizon,
based from your example your sql-qry has comma but mine has no comma i guess thats why it doesn't display properly
I looked at your sql-query and see the the column delimiter is set to tab and the line delimiter is set to return.

You should be able to use the code I provided without "replace comma with tab" since your sql result is already tab / return formatted.

If you put your query in a field it should display well but in your first image it looks funny.
Maybe something is not working in your database/query? I do not use databases and have no experience with sql.

You could zip a simple .sqlite file as zipped file and I could try to read it.

Kind regards
Bernd

Hi Bernd,

I guess i have a problem in my database i will send this wk the sqlite database thanks for helping me out
Thank you & God Bless Everyone :wink:

Regards,
lemodizon

lemodizon
Posts: 219
Joined: Thu Apr 05, 2018 3:33 pm

Re: How to align the display records in the polygrid?

Post by lemodizon » Sun Jun 02, 2024 5:24 pm

stam wrote:
Thu May 30, 2024 3:27 pm
I would argue that instead of using this method, use a database cursor and add the columns to the keys of an array.

I do this regularly - due to a month of near-daily on-calls, there is not chance for me to get any coding done, but can post an example of what I do at some point when I have time (probably in the next week or 2 if this hasn't been solved)

The benefit of using a cursor -> array is you don't have to worry about delimiters or any such tricky subject - just assign the the resting array directly to the pgData.
This means you would be able to include text with returns or other delimiters without breaking the process.

Stam
Hi Stam,

thanks for the information hmm how do i do that in my current code


thank you stam
Thank you & God Bless Everyone :wink:

Regards,
lemodizon

stam
Posts: 3072
Joined: Sun Jun 04, 2006 9:39 pm

Re: How to align the display records in the polygrid?

Post by stam » Mon Jun 03, 2024 5:59 pm

I’ll post an example as soon as I can - unfortunately I’m stuck covering 1 month of nearly daily on-calls ending this week.

stam
Posts: 3072
Joined: Sun Jun 04, 2006 9:39 pm

Re: How to align the display records in the polygrid?

Post by stam » Tue Jun 04, 2024 8:09 pm

Hi Lemodizon,

in order for my code to make sense, please see this explanation first. I have been collecting tips and tricks for LiveCode either by arriving at solutions myself or copying other's. I regret I have no recollection where I borrowed this from and slightly edited (if you are the author please let me know), but it explains how to retrieve SQL data both ways (with a simple TSV variable or a database cursor). I plan to create a subreddit to post these all at some point and open it to others to post clear solutions/algorithms, but for now these reside on my own machine.


When you connect to the database, you receive a connection ID (a number - if not a number then contains error information) that you can use to read data from the database or manipulate the data stored therein.

There are two functions for getting information out of the database:

The first function is revDataFromQuery, which returns a single variable, with the selected fields ofthe records that fit the criteria in your SQL query in TSV text format.

Example:

Code: Select all

put "SELECT cust_id,cust_name FROM Customers" into tQuery
put revDataFromQuery(return,tab,tConnectionID,tQuery) into tData
put tData into field "Table Field"
You would have a return-and-tab-delimited list like this:
000001<tab>Jane Doe<return>
000002<tab>Jeff Doe<return>
000003<tab>John Doe<return>
This function is great if you're looking to display some data easily and quickly. But it would be hard to parse out individual fields, and that's where the next function comes to play.



The second function is revQueryDatabase, which executes the query and returns a cursor ID (a number) - this doesn't contain the data itself, but the ID identifies a collection of pointers to the records in the database that fulfil the SQL query's requirements. You can think of this as navigating cards in a stack:

To determine how many records there are and which is the current one
revNumberOfRecords(<cursor id>)
revCurrentRecord(<cursor id>)

To navigate the records in the result set (much like navigating cards in a stack), you use the commands:
revMoveToFirstRecord <cursor id>
revMoveToPreviousRecord <cursor id>
revMoveToNextRecord <cursor id>
revMoveToLastRecord <cursor id>

To determine what fields are in those records, you use:
revDatabaseColumnCount(<cursor id>)
revDatabaseColumnNames(<cursor id>)

To fetch the individual fields of the current record, you use:
revDatabaseColumnNumbered(<cursor id>,<column number>)
revDatabaseColumnNamed(<cursor id>,<column name>)

To release the result set from memory, you use:
revCloseCursor <cursor id>

stam
Posts: 3072
Joined: Sun Jun 04, 2006 9:39 pm

Re: How to align the display records in the polygrid?

Post by stam » Thu Jun 06, 2024 3:27 pm

It was harder than I thought locating the full example of what I've used before (mainly because I most often use LiveCloud, not SQL/SQLite nowadays).

However here is an example of how to extract records from an SQL cursor to an array. It is function so you can set the pgData (or dgData or dataContent, depending on which grid you're using) directly to this function.

Code: Select all

function ConvertSQLCursorToArray pCursor
    local tColumnNames, tArray, x
    put revDatabaseColumnNames(pCursor) into tColumnNames 
    if tColumnNames begins with "revdberr," then return "Error:" && tColumnNames // exit with error
    revMoveToFirstRecord pCursor
    if the result is false then return "Error: no records in cursor" // no records - exit with error
    repeat until revQueryIsAtEnd(pCursor)
        add 1 to x	
        repeat for each item tColumn in tColumnNames
            put revDatabaseColumnNamed(pCursor, tColumn) into tArray[x][tColumn]
        end repeat	
        revMoveToNextRecord pCursor
    end repeat
    return tArray
end ConvertSQLCursorToArray

The built-in function revQueryDatabase returns a cursor variable, you just use with this function to create a numerically keyed 1-based array.
If you add the above function to the stack script or a library, you can just use it as below:

Code: Select all

local tCursor, tArray
put revQueryDatabase(currentDB, field "Query") into tCursor
if tCursor is not a number then 
   answer "Error:" && tCursor
else
   put ConvertSQLCursorToArray(tCursor) into tArray
   if word 1 of tArray is "Error:" then 
      answer error tArray 
      exit to top
   end if
   set the pgData of widget "myPolyGrid" to tArray
end if
I haven't tested this code so there may be some minor error but it should work...

lemodizon
Posts: 219
Joined: Thu Apr 05, 2018 3:33 pm

Re: How to align the display records in the polygrid?

Post by lemodizon » Thu Jun 13, 2024 2:46 am

Hi stam,

Thanks for the explanation I will try it thanks again i appreciate your help guys.
Thank you & God Bless Everyone :wink:

Regards,
lemodizon

lemodizon
Posts: 219
Joined: Thu Apr 05, 2018 3:33 pm

Re: How to align the display records in the polygrid?

Post by lemodizon » Thu Jun 13, 2024 7:14 am

bn wrote:
Thu May 30, 2024 9:34 am
Hi lemodizon,
based from your example your sql-qry has comma but mine has no comma i guess thats why it doesn't display properly
I looked at your sql-query and see the the column delimiter is set to tab and the line delimiter is set to return.

You should be able to use the code I provided without "replace comma with tab" since your sql result is already tab / return formatted.

If you put your query in a field it should display well but in your first image it looks funny.
Maybe something is not working in your database/query? I do not use databases and have no experience with sql.

You could zip a simple .sqlite file as zipped file and I could try to read it.

Kind regards
Bernd

Hi Bernd,

this is the sqlite db of my database if you have time you check it

Thank you for helping me
Attachments
zigzag.rar
(555 Bytes) Downloaded 93 times
Thank you & God Bless Everyone :wink:

Regards,
lemodizon

Klaus
Posts: 14193
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: How to align the display records in the polygrid?

Post by Klaus » Thu Jun 13, 2024 9:51 am

Looks like this is an EMPTY database!? :shock:

Post Reply