NamedQuery will not run. Keeps going to except. Runs and deletes as expected when tx = txID is removed.
def onMessageReceived(self, payload):
# Get the selected row or rows.
selRows = self.getSibling('Table').props.selection.data
# Begin a transaction - do not delete unless all can be deleted
txID = system.db.beginNamedQueryTransaction("SampleTestV2",database = "SampleTestV2")
# If the user gave confirmation, we need to loop through all of them.
for row in selRows:
# For each selected row, we need to get the value of the id column in that row.
productid = row["ProductID"]
try:
# Use the id of the row to delete it from the database table.
system.db.runNamedQuery("Configurations/DeleteProduct", {"ID":productid}, tx = txID)
except:
self.getSibling("Status").props.text = "Could not delete. Selection(s) potentially referenced somewhere."
system.db.rollbackTransaction(txID)
system.db.closeTransaction(txID)
self.getSibling("Status").props.text = "Successfully deleted"
system.db.commitTransaction(txID)
system.db.closeTransaction(txID)
you could try printing the exception to the console like this:
import exceptions
import java.lang.Exception
try:
# stuff you want to try
except (java.lang.Exception, Exception) as e:
system.perspective.print(e)
# whatever else you want to do in Except block
I know that the recommendation is "Use Named Queries", and a lot of the times that is a good answer, but in some cases (for instance this one), a prep query is really the better tool for the job.
Imagine a user selects 10 rows for deletion, do you really want to run 10 quries against the database when you can just run 1?
What about if 5 different users select 2 rows? or 5 rows.
You should really loop through the selected rows gathering all of the ids, and then run a single query.
Let's say I do this: How can I assure they all get deleted, or none get deleted?
q = """
delete from your_table
where id in ({})
""".format(','.join('?' * len(selRows)))
system.db.runPrepUpdate(q, [product['ProductID'] for product in selRows])
You would use system.db.beginTransaction to get a tx id then use that with your prepUpdates. If something fails you can rollback or if all is well then commit and close the transaction.
It's all or nothing with the transaction, it's not individual transactions, it's individual queries inside one transaction, if anything fails everything inside the transaction will rollback unless you break them up into multiple transactions.