Updataing Database with Values from DataGrid

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
Gage
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 224
Joined: Tue Mar 26, 2013 8:59 pm

Updataing Database with Values from DataGrid

Post by Gage » Thu Jun 20, 2013 7:49 pm

Hello,

I am using an SQLite database to populate a DataGrid with stored contact information. The DG is editable by the user, and I want to be able to update the database when the user leaves the card containing the DG.

I will be scripting the button that exits the card to send some sort of command to the DG to have it read the most current data in the DG's fields. What I need to figure out is the following:
- How to make the DG cycle through each index to get all of the information (does it automatically execute a custom handler for each index if you call the handler from an object outside of the DG?)


Thanks in advance!
Phil E.

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

Re: Updataing Database with Values from DataGrid

Post by Klaus » Thu Jun 20, 2013 9:19 pm

Hi Phil,

why not just script (outside of the datagrid):

Code: Select all

...
put the dgdata of grp "your data grid here..." into tDGArray
## now loop through all keys and do whatever you want with the values
repeat for each key tKey in tDGArray
  put tDGArray[i]["Name of column 1 here..."] into tWhatever
  ## write tWhatever to database
  ## etc...
end ropeat
...
I hope I understood you correctly :-)


Best

Klaus

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Re: Updataing Database with Values from DataGrid

Post by bangkok » Thu Jun 20, 2013 10:33 pm

Gage wrote:I am using an SQLite database to populate a DataGrid with stored contact information. The DG is editable by the user, and I want to be able to update the database when the user leaves the card containing the DG.
Would it be better to update the DB... right after the user updates a cell ? Why do you want to wait the closecard message ?

Gage
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 224
Joined: Tue Mar 26, 2013 8:59 pm

Re: Updataing Database with Values from DataGrid

Post by Gage » Thu Jun 20, 2013 10:51 pm

Klaus,

Yes, that would be great. Only thing I need to figure out, then is how to make the dgData current. Does it automatically update if the user changes text in a field that the dgData populated when the DG was rendered?


Bangkok,

I am playing with multiple options. I don't really know how to do what you are suggesting, either. I have multiple text fields that would be editable by the user, so how would a script be implemented to update the database? What would the trigger be?


Thank you both for the ideas!

Phil E.

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Re: Updataing Database with Values from DataGrid

Post by bangkok » Thu Jun 20, 2013 11:44 pm

Gage wrote: I am playing with multiple options. I don't really know how to do what you are suggesting, either. I have multiple text fields that would be editable by the user, so how would a script be implemented to update the database? What would the trigger be?
Put in the script of your DG :

Code: Select all

on CloseFieldEditor pFieldEditor
    put the dgColumn of the target into theColumnBeingEdited
    put unidecode(the unicodetext of pFieldEditor, "UTF8") into theNewText
    answer "You have typed : "&theNewText
    
    --here you can put a SQL query, to update the data
    --but you need to find the key, related to the record
end CloseFieldEditor

Gage
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 224
Joined: Tue Mar 26, 2013 8:59 pm

Re: Updataing Database with Values from DataGrid

Post by Gage » Thu Jun 20, 2013 11:52 pm

Bangkok,

So, in the script of the DG group, not in the behavior script, right?

What is the trigger to make that handler run?

Phil E.

Gage
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 224
Joined: Tue Mar 26, 2013 8:59 pm

Re: Updataing Database with Values from DataGrid

Post by Gage » Fri Jun 21, 2013 3:31 am

Klaus,

I tried implementing what you said, and it will work, but the part I am still getting tripped up on is getting the dgData to update when the user puts in new information into the fields. THEN I can take the dgData and do what you suggested. Do I just have to go through, row by row, and put the new contents of each field into the relevant spot in the dgData?

Phil E.

Gage
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 224
Joined: Tue Mar 26, 2013 8:59 pm

Re: Updataing Database with Values from DataGrid

Post by Gage » Fri Jun 21, 2013 3:53 am

...Going with a repeat loop containing the SetDataOfIndex command.

Guess I didn't really think that last question through.

Phil E.

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Re: Updataing Database with Values from DataGrid

Post by bangkok » Fri Jun 21, 2013 7:47 am

Gage wrote: So, in the script of the DG group, not in the behavior script, right?

What is the trigger to make that handler run?
In the DG group.

It will run when you close a cell, after having typed something inside.

Provided that your DG has the "allow text editing" property.

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

Re: Updataing Database with Values from DataGrid

Post by Klaus » Fri Jun 21, 2013 10:41 am

Hi Phil,

as soon as a user has entered new data, "the DGDATA" do update automatically,
so whenever you "put/get the dgdata..." they are always UP-TO-DATE :-)


Best

Klaus

Gage
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 224
Joined: Tue Mar 26, 2013 8:59 pm

Re: Updataing Database with Values from DataGrid

Post by Gage » Fri Jun 21, 2013 7:15 pm

Bangkok,

OK, thank you. I will see if that works for me.


Klaus,

I implemented something similar to what you suggested, but after editing a field in the row of the DG, and then pushing the button outside of the DG that calls RevExecuteSQL to update my database, the dgData contained the default value.

this is how I got the array holding the dgData:

Code: Select all

repeat with x = 1 to 7
      put the dgDataOfIndex[x] of group "Edit" of this card into theUpdateA[x]
   end repeat
   --load the code into an SQL command
   --run revExecuteSQL
I'll keep playing with it, though.

Phil

Gage
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 224
Joined: Tue Mar 26, 2013 8:59 pm

Re: Updataing Database with Values from DataGrid

Post by Gage » Fri Jun 21, 2013 7:24 pm

Bangkok,

Using the script you suggested in the DG group code, nothing is triggering it. I see that it says "onCloseFieldEditor", but apparently CloseFieldEditor is not happening. I don't know what "closes" the field after I type in it... so I can't tell how to make it work.

I have not implemented any Field Editors anywhere in the behavior of the DG; I am reading up on that now.

Phil E.

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Re: Updataing Database with Values from DataGrid

Post by bangkok » Fri Jun 21, 2013 9:27 pm

Try this stack.
Attachments
TEST.zip
(4.67 KiB) Downloaded 321 times

Post Reply