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 4537034
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:37:04+00:00 2026-05-21T14:37:04+00:00

I am developing an app based on sqlite, When i am inserting data in

  • 0

I am developing an app based on sqlite,
When i am inserting data in to database the following error occurs.

Table Structure:

CREATE TABLE “Products” (“ProductBarcode” VARCHAR PRIMARY KEY UNIQUE NOT NULL , “ProductName” VARCHAR NOT NULL , “ProductImage” VARCHAR NOT NULL , “ProductIngredients” VARCHAR NOT NULL , “ProductStatus” VARCHAR NOT NULL )

2011-04-15 10:09:48.408 halalgauge[4517:207]  Not Matched 
2011-04-15 10:09:48.410 halalgauge[4517:207] *** Assertion failure in -[sqlClass addRecord:], /Users/admin/Desktop/Halal/Classes/sqlClass.m:149
2011-04-15 10:09:48.410 halalgauge[4517:207] Exception occured at add statement, the error is Error while inserting data. 'constraint failed' 

The code is:

#import "sqlClass.h"

sqlite3 *database = nil;
sqlite3_stmt *deleteStmt = nil;
sqlite3_stmt *addStmt = nil;
sqlite3_stmt *detailStmt = nil;
sqlite3_stmt *updateStmt = nil;


@implementation sqlClass
@synthesize membersInfoArray,membersInfoDict,rowID;






- (void) copyDatabaseIfNeeded 
{
    membersInfoArray = [[NSMutableArray alloc]init];
    membersInfoDict = [[NSMutableDictionary alloc]init];

    //Using NSFileManager we can perform many file system operations.
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSString *dbPath = [self getDBPath];
    BOOL success = [fileManager fileExistsAtPath:dbPath]; 

    if(!success) {

        NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"HalalGauge.sqlite"];
        success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

        if (!success) 
            NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }   
    }

- (NSString *) getDBPath {


@try {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    return [documentsDir stringByAppendingPathComponent:@"HalalGauge.sqlite"];


}
@catch (NSException * e) {
    NSLog(@"Exception");
}
@finally {
    //[sqlClass finalizeStatements];
    NSLog(@"At Finally block");
}

}



+ (void) finalizeStatements {

    if (addStmt) sqlite3_finalize(addStmt);
    if (database) sqlite3_close(database);
    if (deleteStmt) sqlite3_finalize(deleteStmt);
    if (detailStmt) sqlite3_finalize(detailStmt);
    if (updateStmt) sqlite3_finalize(updateStmt);
}

- (void) gettingData:(NSString *)dbPath {
    NSLog(@"Data base path is %@",dbPath);

    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
    {
        const char *sql = "select * from Products";
        sqlite3_stmt *selectstmt;
        if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) 
        {
            while(sqlite3_step(selectstmt) == SQLITE_ROW)
            {

                [membersInfoDict setValue:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)] forKey:@"ProductBarcode"];
                [membersInfoDict setValue:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)] forKey:@"ProductName"];
                [membersInfoDict setValue:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)] forKey:@"ProductImage"];
                [membersInfoDict setValue:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 3)] forKey:@"ProductIngredients"];
                [membersInfoDict setValue:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 4)] forKey:@"ProductStatus"];

                if(membersInfoDict)
                {
                   [membersInfoArray addObject:membersInfoDict];
                   membersInfoDict = nil;
                //  NSLog(@"Entered and return");
                //  return;
                }
            }
        }

    }

    else
        sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
}


- (void) addRecord:(NSMutableDictionary *)recordDict
{
    @try {

        if(addStmt == nil) {
            const char *sql = "insert into Products (ProductBarcode,ProductName,ProductImage,ProductIngredients,ProductStatus) Values(?,?,?,?,?)";
            if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
                NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
        }

        sqlite3_bind_text(addStmt, 1, [[recordDict objectForKey:@"ProductBarcode"] UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(addStmt, 2, [[recordDict objectForKey:@"ProductName"] UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(addStmt, 3, [[recordDict objectForKey:@"ProductImage"] UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(addStmt, 4, [[recordDict objectForKey:@"ProductIngredients"] UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(addStmt, 5, [[recordDict objectForKey:@"ProductStatus"] UTF8String], -1, SQLITE_TRANSIENT);

        //NSLog(@"the values are %@",addStmt);

        if(SQLITE_DONE != sqlite3_step(addStmt))
            NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
        else
            rowID = sqlite3_last_insert_rowid(database);
        NSLog(@"last inserted rowId = %d",rowID);

        sqlite3_close(database);



    }
    @catch (NSException * e) {
        NSLog(@"Exception occured at add statement, the error is %@ ",e);
    }
    @finally {
        [sqlClass finalizeStatements];
    }
        }





@end
  • 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-21T14:37:04+00:00Added an answer on May 21, 2026 at 2:37 pm

    I solved my problem with the following code:

    - (void) addRecord:(NSMutableDictionary *)recordDict
    {
        @try {
    
            [self checkAndCreateDatabase];
            if (sqlite3_open([databasePath UTF8String], &database)== SQLITE_OK) {
    
                NSString *statement;
                sqlite3_stmt *compiledstatement;
                NSString *ProductName,*ProductBarcode,*ProductImage,*ProductIngredients,*ProductStatus;
                ProductName = [recordDict objectForKey:@"ProductName"];
                ProductBarcode = [recordDict objectForKey:@"ProductBarcode"];
                ProductImage = [recordDict objectForKey:@"ProductImage"];
                ProductIngredients = [recordDict objectForKey:@"ProductIngredients"];
                ProductStatus = [recordDict objectForKey:@"ProductStatus"];
    
    
    
                statement = [[NSString alloc]initWithFormat:@"insert into Products values('%@','%@','%@','%@','%@')",ProductBarcode,ProductName,ProductImage,ProductIngredients,ProductStatus];
                const char *sqlstatement = [statement UTF8String];
                if (sqlite3_prepare_v2(database, sqlstatement, -1, &compiledstatement, NULL)== SQLITE_OK) {
                    if (SQLITE_DONE!=sqlite3_step(compiledstatement) ) {
                        NSAssert1(0,@"Error when inserting  %s",sqlite3_errmsg(database));
    
                    }
                    else {
                        NSLog(@"Data inserted Successfully");
                    }
    
                sqlite3_finalize(compiledstatement);
    
                }
                sqlite3_close(database);
            }
    
        }
        @catch (NSException * e) {
            NSLog(@"The Record already inserted ");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing an iPhone app that uses the built-in SQLite database. I'm trying to
I'm developing a Pylons app which is based on exisitng database, so I'm using
I'm developing an app with a Flex-based front end and a Spring/Hibernate back-end. To
I've been developing my first web based app for about two months now (I'm
I have a winforms (VB 2008) based app that I'm developing and I want
I am novice iPhone developer . I am developing a Navigation based app for
I'm developing an app that handle sets of financial series data (input as csv
I'm developing a web app based on videos that my client would like to
I am developing an GPS based app. So I was wondering how could I
I am developing an iPhone app which is a Navigation based app. In this

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.