[Solved] how to does not record in sqlite if it is already in table [closed]


- (int) GetCount :(NSString *)UserID
{
[self createEditableCopyOfDatabaseIfNeeded];
[self initializeDatabase];

int count = 0;


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *path = [documentsDirectory stringByAppendingPathComponent:@"dbfav2table.sqlite"];
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK)
{
    sqlite3_stmt *statement;

    NSString *sql_str = [NSString stringWithFormat:@"SELECT count(*) FROM tablename WHERE userID='%@'", UserID];

    const char *sqlStatement = (char *)[sql_str UTF8String];
     NSLog(@"%@", [NSString stringWithUTF8String:sqlStatement]);

    if( sqlite3_prepare_v2(database, sqlStatement, -1, &statement, NULL) == SQLITE_OK )
    {

        while( sqlite3_step(statement) == SQLITE_ROW )
        {
            count = sqlite3_column_int(statement, 0);
            NSLog(@"count for %@ is %i",UserID,count);
        }

    }
    else

 {
        NSLog( @" Error is:  %s", sqlite3_errmsg(database) );

 }

    // Finalize and close database.
    sqlite3_finalize(statement);
    sqlite3_close(database);
}

return count;
}

if it returns count = 1 it means there is already a value with same userId exist, else there is no such value. hope it helps

solved how to does not record in sqlite if it is already in table [closed]