Binding DataGrid to SQLite DB

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
townsend
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 430
Joined: Sun Feb 13, 2011 8:43 pm

Binding DataGrid to SQLite DB

Post by townsend » Sat Feb 26, 2011 12:27 am

I'm very impressed with LiveCode's built in Datagrid.

How would I go about binding a Datagrid to a SQLite database?

Or do I have to monitor the onChange Message (event) for each row,
and update the database with a Handler (bit of code).

Here's a stack with highlights all the main features.
http://www.runrev.com/templates/runrev/ ... dsTour.zip

Here's a stack that goes over all the Datagrid commands.
http://revonline2.runrev.com/download/s ... o-datagrid

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1005
Joined: Sat Apr 08, 2006 3:06 pm
Contact:

Re: Binding DataGrid to SQLite DB

Post by trevordevore » Mon Feb 28, 2011 1:39 pm

You need to manually insert the changes into the database. Take a look at this lesson:

http://lessons.runrev.com/spaces/lesson ... ta-Source-
Trevor DeVore
ScreenSteps - https://www.screensteps.com

LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder

townsend
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 430
Joined: Sun Feb 13, 2011 8:43 pm

Re: Binding DataGrid to SQLite DB

Post by townsend » Mon Feb 28, 2011 7:51 pm

Thanks for the link Trevor, but it's still a little early for me to understand the exact mechanics. For instance:
When calling EditFieldText with all three parameters (which is what a data grid column does by default) the data grid will automatically save the text that the user enters in the dgData array. That means that all you need to do is save the text of the editor field to your external data source in the CloseFieldEditor message
(Note to other newbies: the CloseFieldEditor message is the event you use to update the database.)
This seems to imply that you can link an array to a DataGrid, but unless you can also link or bind the array to a database, why bother?
The DataGrid is an array in and of itself already. Right? At this point I'm thinking three handlers are needed:
  • 1- Read database & copy into DataGrid.
    2- When user changes row in grid,
    3- that row is updated to database.
That's without using an array? Is this the most efficient route? What does using the array get me?

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1005
Joined: Sat Apr 08, 2006 3:06 pm
Contact:

Re: Binding DataGrid to SQLite DB

Post by trevordevore » Mon Feb 28, 2011 8:09 pm

There is no binding that goes on with a data grid (nor with any other control in LiveCode). The control houses the data that it displays and you update data in an external source independently of the control itself. dgData is the property the returns the the internal array that the data grid is using as it's data source.

The lesson simply shows how to intercept a message that is sent when the user edits a value in the data grid so that you can update the value in an outside source - Steps 2 and 3 in the list you provided. It may help to read up a little bit more on how the built-in field editor works. Here is a link to the entire chapter:

http://lessons.runrev.com/spaces/lesson ... pters/2433

That being said, the data grid has rudimentary support for bypassing the need to assign an array to the data grid. This can be used for passing the values of rows in a database cursor directly into the data grid as needed. Here is a lesson that provides some more details:

http://lessons.runrev.com/spaces/lesson ... ts-of-Data
Trevor DeVore
ScreenSteps - https://www.screensteps.com

LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder

townsend
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 430
Joined: Sun Feb 13, 2011 8:43 pm

Re: Binding DataGrid to SQLite DB

Post by townsend » Mon Feb 28, 2011 9:10 pm

The control houses the data that it displays and you update data in an external source independently of the control itself. dgData is the property the returns the the internal array that the data grid is using as it's data source.
Great! That makes perfect sense!
That being said, the data grid has rudimentary support for bypassing the need to assign an array to the data grid. This can be used for passing the values of rows in a database cursor directly into the data grid as needed. Here is a lesson that provides some more details:
Okay-- I see- that's a possible short cut. I need to study this code before deciding whether or not to use it.
Aside from the added complexity, are there any pros and cons us newbies need to be aware of?

At this point I will say-- that's is a great little database example.
One of the best I've seen. (From the lesson of your last link)
http://www.bluemangolearning.com/downlo ... abases.zip

Interesting-- the link to download the example is from Blue Mango.
Which means-- you must have written that lesson. Thanks for your help!
89 movies db.JPG
89 movies db.JPG (55.24 KiB) Viewed 6700 times

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1005
Joined: Sat Apr 08, 2006 3:06 pm
Contact:

Re: Binding DataGrid to SQLite DB

Post by trevordevore » Tue Mar 01, 2011 2:21 pm

townsend wrote:Aside from the added complexity, are there any pros and cons us newbies need to be aware of?
Speed. I've used the technique when I need to display large amounts of records in a data grid. I've used it in production work with > 25,000 records in a SQLite database. If all of those records had to be converted to an array and loaded into the data grid it would be very slow. Using an open database cursor and passing the row values directly to the data grid is much faster.
townsend wrote:Interesting-- the link to download the example is from Blue Mango.
Which means-- you must have written that lesson. Thanks for your help!
You're welcome.
Trevor DeVore
ScreenSteps - https://www.screensteps.com

LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder

Post Reply