Search This Blog

Thursday, 16 August 2012

How to check for the duplicate entry in sqlite database when inserting a value in database for xcode

Hello Friends,
                      Here it is a complete function to check if the value is already inserted to database and prevent ur app from inserting duplicate entries in databsae.

CODE:

-(BOOL)checkExistence{
    sqlite3_stmt *statemnt;
    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    const char *dbPath = [app.databasepath UTF8String];
    
    if(sqlite3_open(dbPath, &database)==SQLITE_OK){
        NSString *checkSQL = [NSString stringWithFormat:@"SELECT stationName,stationURL FROM FAVOURITESTATION WHERE stationName=\"%@\" ",app.name];//app.name is a value for which i am checking if it already exists in sqlite database or not.
        
        const char *chkSQL = [checkSQL UTF8String];
        
        if(sqlite3_prepare_v2(database,chkSQL, -1,&statemnt,NULL)==SQLITE_OK){
            if(sqlite3_step(statemnt)==SQLITE_ROW){
             sqlite3_finalize(statemnt);
                sqlite3_close(database);

                return YES;
            
            }else{
                sqlite3_finalize(statemnt);
                sqlite3_close(database);

                return NO;
            }
        }
        }
    return NO;
}


follow preivous post for complete refrence of variables used here....

No comments:

Post a Comment