In the past with sqlite, when updating a record I would use a long concatenated string to build the update statement. For example below. But I ran into the edit row where I can just select the appropriate row and then update a column and then save the row. Question is do I need to Open a transaction before this, and using edit row do I need to commit the transaction before I close the database? Currently during certain situation I open a transaction and then i commit the transaction. The documentation does not say whether the transaction needs commited after the save row. Heres old code:
a transaction / commit or rollback is used if you edit multiple records, as example you will write a invoice and you need it completely and not half if a error happened.
IMO the new way is to use prepared statements which are now built into methods such as SelectSQL or ExecuteSQL. Thus:
sSQL = "UPDATE UserRegistrations SET dbUserLoginID = ?1, dbUserPassword = ?2 WHERE dbID = ?3"
db.ExecuteSQL (sSQL, ScrambleString(Session.Username), ScrambleString(Session.UserPassword), Session.UserRegistrationDBID)
.
with no need for single quotes, or converting to string. And no chance of an SQL injection attack.
In SQLite, a single SQL statement doesn’t need transaction treatment, since SQLite itself will automatically wrap each such in a transaction, unless a transaction is already open.
The only place I use transactions is when I know I’ll be looping doing lots of updates. This only happens in a few places in my code, in particular where I have extracted a number of spam tokens from a text and wish to add them all individually to a database. Then I’d do:
db.ExecuteSQL ("BEGIN TRANSACTION")
// here is my loop doing updates
dbExecuteSQL ("COMMIT")
.
I’ve never tried this editrow/saverow stuff as you anyway have to do a select beforehand to have a row to edit. Also it’s Xojo wrapping stuff around an ordinary SQL UPDATE statement and you don’t know what they are actually doing.
I’ve ran into many problems with editrow.(depends of the database engine)
you need a primary key and it’s not always the case.
I’m now using sql queries “update … set …” with no problem .