Raj Kumar Raj Kumar - 16 days ago 7
iOS Question

How to find existing folder name in sqlite iOS

I created a table with few field as UNIQUE. So, I have to show an alert to user while they insert the duplicate foldername into table. Can anyone tell me "How to check for an existing data before it can be inserted into the table in sqlite database"

Answer

you can check for record by following code

-(BOOL)CheckForRecord:(NSString *)Name
{
    BOOL isRecordPresent=false;

    const char *dbpath = [databasePath UTF8String];
    sqlite3_stmt    *statement;

    if (sqlite3_open(dbpath, &database) == SQLITE_OK)
    {
        NSString *querySQL = [NSString stringWithFormat:
                              @"SELECT ID FROM TBL_Name WHERE Column_Name=\"%@\"",Name];

        const char *query_stmt = [querySQL UTF8String];

        if (sqlite3_prepare_v2(database,
                               query_stmt, -1, &statement, NULL) == SQLITE_OK)
        {
            if(sqlite3_step(statement) == SQLITE_ROW)
            {
                isRecordPresent = 1;
            }
            else
            {
                isRecordPresent = 0;
            }
        }
    }

    sqlite3_finalize(statement);
    if(sqlite3_close(database) == SQLITE_OK)
    {

    }
    else
    {
        NSAssert1(0, @"Error: failed to close database on memwarning with message '%s'.", sqlite3_errmsg(database));
    }   
    return isRecordPresent;
}
Comments