Code for Updating database upon Closing Card
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Code for Updating database upon Closing Card
Hello:
Searched the forum and couldn't find this problem.
Users in my application will be entering text into empty fields or modifying text in other fields in one card. Before entry or modification, the text of fields containing the text will have been downloaded from a sqlite database when the card was opened. I need a script that identifies the fields that the user changed so that the database field or fields can be updated upon closing the card. I know that the textchanged handler can identify a change in one field, but I do not know how the name of every changed field can be put into memory for inclusion in the update.
For all I know the script may be very complex; if so, I would appreciate just a description of a method or links to explanations. Thanks for your replies.
Monty
Searched the forum and couldn't find this problem.
Users in my application will be entering text into empty fields or modifying text in other fields in one card. Before entry or modification, the text of fields containing the text will have been downloaded from a sqlite database when the card was opened. I need a script that identifies the fields that the user changed so that the database field or fields can be updated upon closing the card. I know that the textchanged handler can identify a change in one field, but I do not know how the name of every changed field can be put into memory for inclusion in the update.
For all I know the script may be very complex; if so, I would appreciate just a description of a method or links to explanations. Thanks for your replies.
Monty
-
- VIP Livecode Opensource Backer
- Posts: 2718
- Joined: Sat Dec 22, 2007 5:35 pm
- Location: Genève
- Contact:
Re: Code for Updating database upon Closing Card
Hi Monty
Why not using a customprop uMyText for each fld on opencard
and check if the uMyText of each fld = the text of each fld in a loop on closecard ?
Best regards
Jean-Marc
Many ways to do that.I do not know how the name of every changed field can be put into memory for inclusion in the update.
Why not using a customprop uMyText for each fld on opencard
and check if the uMyText of each fld = the text of each fld in a loop on closecard ?
Best regards
Jean-Marc
https://alternatic.ch
Re: Code for Updating database upon Closing Card
why not just update every field in the table with the values in the fields? Name the fields the same as the fields in the table, which also makes populating them easier, iterate the number of fields on the card, get the name of the field, build the SQL, use parameters to make sure you don't suffer an SQL injection, execute it.
Re: Code for Updating database upon Closing Card
Thanks Jean-Marc and Mikey.
Going with Jean-Marc's suggestion, I wrote the following script:
But it doesn't update the database. I am a beginner at SQL in LC. I'm thinking it's some syntactical mistake in lines 8 and/or 9.
Monty
Going with Jean-Marc's suggestion, I wrote the following script:
Code: Select all
global gDatabaseID
on closecard
put fld "pcfnum" into tPCFnum --the identifier of the correct row
repeat with i=1 to 13 --the number of fields the user may have modified
put the short name of fld i into tName
put the text of fld i into tTextNew
put the cText of fld i into tTextOld
if tTextNew <> tTextOld then
put "UPDATE OpsDetails SET tName='"&tTextNew&"' WHERE pcfnum='"&tPCFnum&"'" into tSQL
revExecuteSQL gDatabaseID, tSQL, "tName"
end if
end repeat
end closecard
Monty
Re: Code for Updating database upon Closing Card
Hi.
Assume it's the 3rd param in revExecuteSQL:
You don't have no varList nor array here, as far as I see. And is the field name in the database really "tName"?
Btw., I do such in a slightly other way:
To keep this simple, I usually have both separate "GetSQL" (for select queries) & "PutSQL" (for action queries) handlers, where all the error handling is concentrated ;-)
Finally, I don't trust long open connections anymore. Opening and closing a connection is this fast these days, I do it for each call to the DB now.
Hope I cold help!
Code: Select all
put "UPDATE OpsDetails SET tName='"&tTextNew&"' WHERE pcfnum='"&tPCFnum&"'" into tSQL
revExecuteSQL gDatabaseID, tSQL, "tName"
Code: Select all
revExecuteSQL databaseID,SQLStatement [,{variablesList | arrayName}]
Btw., I do such in a slightly other way:
- I grab my "record to be edited" via SELECT query & "revdb_querylist(FldDel,RecDel,MyDBID,StrSQL)".
- This is then a.) stored in a custom property and b.) displayed in the fields.
- The fields then can be edited, and have a suitable validity check each.
- At "saving time" I just combine the fields to a matching record,
- compare this to the stored one and update if required.
To keep this simple, I usually have both separate "GetSQL" (for select queries) & "PutSQL" (for action queries) handlers, where all the error handling is concentrated ;-)
Finally, I don't trust long open connections anymore. Opening and closing a connection is this fast these days, I do it for each call to the DB now.
Hope I cold help!
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!
Re: Code for Updating database upon Closing Card
Instead of trying to see which fields changed and which not, send an UPDATE with all fields to the database.
Databases are really super fast, even a 100 fields sqlite update is faster than a 100 fields livecode check.
Databases are really super fast, even a 100 fields sqlite update is faster than a 100 fields livecode check.
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w
Re: Code for Updating database upon Closing Card
well, not in the database, but in the table, and exactly what I was proposing, earlier. Plus the code is simpler/easier/etc.