If SQL Query no results do something

Hi, i’m trying to select a result if the query returns a value, , if it does not return a value then do something else. i have it working with this code. but im trying to find a better way. because it does not look good.

                  selectlog="""select palletid from rawmaterialslog where palletid=? and lotid = ?"""
                  selectlo=system.db.runPrepQuery(selectlog, [newValue, lotid])
		
		try:

################ if no results when trying to print it will fail, and it will go to Except, and then i run my code from there. but looking to avoid using the print ########
print selectlo[0][0]
system.gui.messageBox(“That Pallet was already Scanned, please choose another”)

		except:
                                 Do something.

Since you’re expecting just 1 value as a result I’d use runScalarPrepQuery instead, which will return palletid to you and None if there is no pallet.

selectlog="""select palletid from rawmaterialslog where palletid=? and lotid = ?"""
selectlo=system.db.runScalarPrepQuery(selectlog, [newValue, lotid])

if selectlo is not None:
	print selectlo
else:
	do something else
2 Likes

thank you worked perfect.