# Check if record exists in a table

**URL:** <https://forum.inductiveautomation.com/t/check-if-record-exists-in-a-table/9286>\
**Category:** Ignition\
**Created:** [November 4, 2014, 6:32pm UTC](https://forum.inductiveautomation.com/t/check-if-record-exists-in-a-table/9286 "2014-11-04T18:32:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nikzitronik](https://avatars.discourse-cdn.com/v4/letter/n/9e8a1a/32.png) [@Nikzitronik](https://forum.inductiveautomation.com/u/Nikzitronik)\
**Post date:** [November 4, 2014, 6:32pm UTC](https://forum.inductiveautomation.com/t/check-if-record-exists-in-a-table/9286/1 "2014-11-04T18:32:28Z")

</div>

I have a recipe table where the user will be able to add and delete records.  
The user will enter the record number he wants to delete in a numeric field and then hit a delete button.  
I need to check if the record exists before I delete it from the table.  
I want to do the same for the addition of a record in the table. I want to check if the record exists in the table before I add it again.

What sort of mySQL query do I need to add to the button to return a boolean result that indicates if the record exists or not??

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [November 4, 2014, 7:21pm UTC](https://forum.inductiveautomation.com/t/check-if-record-exists-in-a-table/9286/2 "2014-11-04T19:21:03Z")

</div>

system.db.runScalarQuery() will return the value from the first row and column of a query. Ideally, you’d make your query to return just one row and column. 🙂

[code]query=“SELECT COUNT(\*) from table where id=’%s’”  
recordExists=system.db.runScalarQuery(query % (str(recordId)))  
if recordExists: # or, if recordExists \> 0:  
do something  
else:  
do something else

[/code]
