SQLite - Checking for a duplicate entry before adding it

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
buchacho
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 50
Joined: Fri Jun 14, 2013 10:22 pm

SQLite - Checking for a duplicate entry before adding it

Post by buchacho » Thu Oct 17, 2013 11:27 pm

I have a function that will add a new entry to a table in my SQLite database. I would like to add to the function some kind of SQL query that I use to check to see if I am adding a duplicate entry before doing so. Is there a query I can perform to search the table based on a few columns to return the number of matches?

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Re: SQLite - Checking for a duplicate entry before adding it

Post by bangkok » Fri Oct 18, 2013 8:05 am

Are you saying that you are able to do an INSERT in SQL... but you don't know how to do a... SELECT ? !
:roll:

Anyway. Make a simple SELECT on your column, before to do the INSERT, to check.

Code: Select all

put "SELECT * from mytable where name='"&newEntry&"'"
Some people would advise you to put your insert function in a TRY CATCH structure.

http://forums.runrev.com/phpBB2/viewtop ... f=7&t=9418

You can even add some "fuzyness" (because a "duplicate" for human, is not a always a "duplicate" for computer... spelling mistakes, typos etc.) with "LIKE" (and wildcard %), in order to make a warning for the user before the insert.

Code: Select all

put "SELECT * from mytable where name like '%"&newentry&"%'" into tSQL

buchacho
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 50
Joined: Fri Jun 14, 2013 10:22 pm

Re: SQLite - Checking for a duplicate entry before adding it

Post by buchacho » Mon Oct 21, 2013 6:47 pm

bangkok wrote:Are you saying that you are able to do an INSERT in SQL... but you don't know how to do a... SELECT ? !
:roll:
Not exactly :D I like to ask here since I am not too familiar with SQL and am looking for efficient ways to input user data. It is very time consuming to design the user input aspect with LiveCode to ensure input meets strict rules for multiple fields and drop-down menus...

I found this function to be useful, where tSQL is the SELECT argument:

Code: Select all

put revQueryDatabase(tDatabaseID, tSQL) into theCursor
put revNumberOfRecords(theCursor) into numRecords

Post Reply