Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6382601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:32:37+00:00 2026-05-25T02:32:37+00:00

I am trying to prevent duplicate entries into my sqlite db by using the

  • 0

I am trying to prevent duplicate entries into my sqlite db by using the UNIQUE identifier when creating a table. The information, including the primary key, is inserted via some json data returned from a web service.

Whilst I understand the implications of not using an auto-generated primary key, it is necessary for the business model.

If by chance the web service returns a record which already exists then you would assume that it would not insert the data again, but throw an error. This is not the case.

Does anyone know a way to prevent this from happening?

Creation statement

CREATE TABLE "Data" ("Id" INTEGER PRIMARY KEY NOT NULL UNIQUE , "DataText" TEXT NOT NULL , "CategoryId" INTEGER NOT NULL )

Insert code

if (sqlite3_open([[self getDatabaseConnection] UTF8String], &database) == SQLITE_OK)
    {        
        const char *sql = "INSERT INTO Data (Id, CategoryId, DataText) Values(?, ?, ?)";
        if(sqlite3_prepare_v2(database, sql, -1, &addStatement, NULL) != SQLITE_OK)
        {
            NSLog(@"Error while creating add statement. '%s'", sqlite3_errmsg(database));
        }
        else
        {
            sqlite3_bind_int64(addStatement, 1, dataId);
            sqlite3_bind_int64(addStatement, 2, categoryId);
            sqlite3_bind_text(addStatement, 3, [dataText UTF8String], -1, SQLITE_TRANSIENT);

            if(SQLITE_DONE != sqlite3_step(addStatement))
            {
                NSLog(@"Error while inserting data. '%s'", sqlite3_errmsg(database), nil);
            }
        }
        //Reset the add statement.
        sqlite3_reset(addStatement);
    }
    sqlite3_close(database);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:32:38+00:00Added an answer on May 25, 2026 at 2:32 am

    Let’s say you have a table called PRODUCTS and it contains this value:

                   ProductID|ProductName
                   50|widget
    

    ProductID is the primary key.

    Unless you have a unique index on [productname] you could insert this record:

                  60|widget
    

    But even if you did not have a unique index on productname, you could not insert any additional records with a productid of 50, no matter what the product’s name happened to be, because a row with primary key value of 50 already exists.

    ADDENDUM:

    Now let’s say that you were willing to accept multiple instances of ‘widget’ but not within the same category. You could have “roofing widget” and “flooring widget”. In that case, in addition to the primary key unique constraint on your integer id column, you could place a unique constraint on this alternate composite key: (productname, categoryid).

    ADDENDUM2:

    INTEGER PRIMARY KEY should suffice. You shouldn’t need NOT NULL UNIQUE. http://www.sqlite.org/lang_createtable.html#rowid

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

To prevent duplicate table entries in a database I use a primary key. I
I am trying to prevent duplicate copies in a table. I am looking for
I'm trying to prevent information to be copied from a page (for non-technical users
Doctrine NOOB here, trying to figure out how to prevent a duplicate record in
I am trying to prevent a user from entering anything into a particular textbox
I'm using MVC3 & EF4, running into a bit of a problem when trying
I have a page with a form where I'm trying to prevent duplicate submission,
Trying to prevent access to a specific file, not any files with a certain
I am trying to prevent data races in a multihreaded server. My problem is
I'm trying to prevent A HREF from actually opening the link, but to execute

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.