Page 1 of 1

Queries and Data Grids

Posted: Sat Jan 23, 2016 12:55 am
by smith8867
I have been trying to get to grips with the LiveCode and databases and I'm getting there, slowly... I can get connected, add data and select it. However, I am struggling to display the data nicely.

Whilst viewing this: http://lessons.livecode.com/m/4071/l/70 ... l-database
I was under the impression a datagrid is the best way forward, however I cannot seem to be able to get them working. If someone could explain to me how to set up the data grid columns and get the data from the query into the columns, it would be much appreciated.
Thanks guys.

Re: Queries and Data Grids

Posted: Sun Jan 24, 2016 3:26 pm
by Klaus
Hi Mr. Smith,

OK, lets start with an easy example.
You have a database with 3 db fields
1. name
2. address
3. phone

When you execute this in Livecode:
select * from your_table
you get a TAB and CR delimited (text) list like:
name1 TAB address1 TAB phone1name2 TAB address2 TAB phone2
etc...

OK, so far?

Now you need to set up a datagrid of type TABLE*** with 3 columns in the same order as the fields in the database:
1. name
2. address
3. phone

*** This type only supports ONE Line data per column, if you have db fields that contain more than 1 line, the display will get messed up!
However type FORM is a BIT more complicated, see below 8)

Then you can simply put the result of your query into the datagrid like:

Code: Select all

...
put "seelect * from your_table" into tSQL
put revDataFromQuery(tab, return, tConnectionId, tSQL) into tDBResult
## Error checking:
if tDBResult begins with "revdberr" then
  answer "error!"
  exit to top
end if
## Success, now "fill datagrid with data:
set the DGTEXT of grp "your datagrid name here" to tDBResult
...
Et voila, nicely displayed records in a datagrid.
Hope this gets you started!

Load the complete Datagrid docs in PDF here: http://lessons.livecode.com/m/datagrid
and work through it a couple of times, Datagrid are higly complex beasts, to say the least.

Best

Klaus

Re: Queries and Data Grids

Posted: Sun Jan 24, 2016 10:19 pm
by smith8867
Hi Klaus,
Thanks for replying. Do I have to set the different columns in the datagrid before hand? I can't seem to find where I can do that.

Re: Queries and Data Grids

Posted: Sun Jan 24, 2016 10:28 pm
by smith8867
Okay that worked. Its just that the query data columns in the grid are labelled "Col 1" "Col 2" etc, is there a way to change that to the field names ?

Re: Queries and Data Grids

Posted: Mon Jan 25, 2016 12:58 am
by quailcreek
smith8867,
If you're running LC 6 or 7 the columns are managed in the DG property inspector. LC 8 doesn't have this yet.

Re: Queries and Data Grids

Posted: Mon Jan 25, 2016 8:57 am
by smith8867
quailcreek wrote:smith8867,
If you're running LC 6 or 7 the columns are managed in the DG property inspector. LC 8 doesn't have this yet.
Yea I have LiveCode 8, that'll be why then. Thanks.