I have two queries that I am running on a couple different tables and I would like to convert them to function since i have to use them multple times. I will admit that I am a novice at this so I am sure my code is rough.
My first funtion I would like to be able to hand it the FROM and WHERE staments and it return NoEntry
#Check to see if 1 has been used query = "SELECT * FROM %s WHERE CoatNumber=1 AND recipes_id= %s" % (table, recipes_id) NoEntry = system.db.runScalarQuery(query,'Foxall')
Here I would like to do similar and have it return Gap.
#Get the next available number for the Recipe Number query = "SELECT t1.CoatNumber + 1 FROM %s t1 WHERE NOT EXISTS (SELECT * FROM %s t2 WHERE t2.CoatNumber = t1.CoatNumber + 1 AND recipes_id=%s) order by CoatNumber LIMIT 1" %(table,table,recipes_id) Gap = system.db.runScalarQuery(query,'Foxall')
After the funtion return a value or not I compare them to find if my next entry needs to either insert a 1 or the first availible in a gap.
If you think that is a better way to do this, please let me know, as I came up with this solution from googling. My ultimate goal is to search for the lowest unused number in the CoatNumber field and use that one next when inserting the date.