Problem with Updating SQL Database from DG

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

Problem with Updating SQL Database from DG

Post by Gage » Mon Jul 29, 2013 7:05 pm

Hello,

I have been creating an extensively customized Datagrid that interfaces with an SQL database. There are numerous user-editable fields in the rows of the DG, and I am implementing the EditFieldText pField, pIndex, pKey function from the Datagrid API to update the internal array automatically after the field is edited by the user.

The goal of this is to keep the internal array current, so the user may perform any action after making changes with full confidence that the changed information is saved.

I am trying to debug this, and am currently handling the CloseFieldEditor message in the card script to update the SQL database. Thus, whenever the focus leaves the field being edited, the internal array is grabbed and used to update the columns in the relevant table in the SQL db.

The Problem:
However, it is apparent that the CloseFieldEditor message is being handled before the internal array of the DG reflects the MOST RECENT CHANGE. So, when I retrieve the dgData, it is current EXCEPT FOR the last change made to any field in the datagrid.

It looks like a matter of timing between the automatic update of the internal array, and the time at which I can receive and handle the CloseFieldEditor message in the card script.

How can I overcome this, so that when the internal array is retrieved (preferably from the CloseFieldEditor handler), it is 100% current, rather than one change behind?

Hope that makes sense! I can include any scripts necessary, though I feel this hinges on my lack of understanding the order in which the engine processes different functions or commands.

Regards,
Phil E.

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

Re: Problem with Updating SQL Database from DG

Post by Klaus » Tue Jul 30, 2013 11:28 am

Hi Phil,

you could:

1. ad a little delay in your CloseFieldEditor handler before getting the updated DGData:
on CloseFieldEditor
## ...
## ...
## let the EditorDo all what's neccessary and then:
send "update_all_the_stuff_from_DGData" to this cd in 100 millisecs
end CloseFieldEditor

THis way the datagrid has the time to update its internal stuff.

2. Use the data in the EditorField to update your DB directly in the CloseEditorField handler!
This is the way I do things over here.

Get the picture?


Best

Klaus

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

Re: Problem with Updating SQL Database from DG

Post by Gage » Wed Jul 31, 2013 12:31 am

Hi, Klaus,

Yes, I think I get it.

So: call a handler on the card script after a delay that updates the database (from the CloseFieldEditor handler)

or

send the statement needed to update the DB directly from the CloseFieldEditor handler (which is what I am already doing)

Since I am already implementing the second suggestion, but am still having this problem, is there a possibility that I can just have the program wait to execute the SQL command creation and delivery from that handler?

Thanks,
Phil E.

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

Re: Problem with Updating SQL Database from DG

Post by Klaus » Wed Jul 31, 2013 11:42 am

Hi Phil,
Gage wrote:...send the statement needed to update the DB directly from the CloseFieldEditor handler (which is what I am already doing)

Since I am already implementing the second suggestion, but am still having this problem, is there a possibility that I can just have the program wait to execute the SQL command creation and delivery from that handler?
??? If you use "SEND XXX IN..." you already have the delay you need and you can even define the delay?
Sorry, don't get it.


Best

Klaus

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

Re: Problem with Updating SQL Database from DG

Post by Gage » Wed Jul 31, 2013 7:55 pm

Hi, Klaus,

Ahh, I didn't mean "SEND"ing the statement. I meant that the CloseFieldEditor message is automatically sent by the engine, and I am handling that to update the database, but still having the problem.

To rephrase, I am not manually adding any "WAIT" commands of any sort anywhere. I thought your #2 suggestion was to use the CloseFieldEditor handler to create the statement to update the database to reflect the current data. I am doing that, and am experiencing the issue where the internal data has not been updated BEFORE the CloseFieldEditor handler executes, leaving my most recent change unreflected when I go to update the database in CloseFieldEditor.

So, my current method:
1. click on a field in the DG to edit its contents
2. EditFieldText with pField, pIndex, pKey parameters passed
3. edit the field as desired
4. move focus off field being edited
5. CloseFieldEditor is sent by engine and handled in DG group script
6. CFE handler in DG group script responsible for building statement to update the data using the dgData

The problem happens around number 5 and 6. Because I pass all three parameters with EditFieldText, the dgData should automatically update on CloseFieldEditor. However, I go to handle this message in order to update the database, but it appears that the dgData does not yet reflect the MOST RECENT CHANGE only (all previous changes are reflected, though). Since the dgData is not current, my update also does not reflect the most recent edit to a field in the data grid.

I hope that is more clear. Sorry about the ambiguity.

Thanks!
Phil E.

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

Re: Problem with Updating SQL Database from DG

Post by Klaus » Thu Aug 01, 2013 11:41 am

Hi Phil,

I thought of scripting the "CloseEditorField" handler by yourself and use the
data that the user had entered to update the db BEFORE this is written back into
the datagrid and it gets updated!

Know what I mean?


Best

Klaus

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

Re: Problem with Updating SQL Database from DG

Post by Gage » Thu Aug 01, 2013 6:47 pm

Klaus,

OK, yes!

Kind of like reinventing the wheel, but just because the built-in Field Editor functionality is a little flawed for how I am using it.

How can the Field Editor be accessed? I know CloseFieldEditor is sent with the parameter pFieldEditor, which I imagine contains the long ID of the Field Editor. So would I use script like this:

on CloseFieldEditor pFieldEditor
put the text of fld pFieldEditor into tText
--update DB using tText
end CloseFieldEditor

Let me know if that is about right.

Thanks for your help!
Phil E.

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

Re: Problem with Updating SQL Database from DG

Post by Klaus » Fri Aug 02, 2013 10:59 am

Hi Phil,

yes, something like this.

I added this script to the datagrid GROUP directly (not the behavior) to update my DB
independently from the datagrid:

Code: Select all

on CloseFieldEditor pFieldEditor

  ## tCurrentIndex is a local variable that I set "on selectionChanged pHilitedIndex, pPrevHilitedIndex" in the same script!
  put pHilitedIndex into tCurrentIndex

  ## Get complete data of currently selected ROW
  put the dgDataOfIndex[tCurrentIndex] of me into tData
  
  ## Get the text/value, that the user entered
  put the text of pFieldEditor into tValue
 
  ## I "call" my db update routine with a little delay:
  send "db_update XXX,YYY,tValue,1234" to me in 10
  
  ## MOST IMPORTANT:
  ## PASS CloseFieldEditor to let the dg behavior do its work :-)
  pass CloseFieldEditor
end CloseFieldEditor
Best

Klaus

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

Re: Problem with Updating SQL Database from DG

Post by Gage » Fri Aug 02, 2013 5:53 pm

Klaus,

You help solve so many of my problems. I am sincerely grateful!

Have an excellent day!

Phil E.

Post Reply