Monday 7 January 2013

Database - Select particular row with where condition

//TableName - Name of table
//withColumn - Array of column name to select
//withCondition - column name on which you want to make condition
//ComumnValue - what pass in that column
//Here is method for same
- (NSMutableArray *)columeFromDatabase:(NSString *)TableName withColumn:(NSArray *)arrColumnName withCondition:(NSString *)ColumnNameCondition withColumnValue:(NSString *)ColumnValue
{
    NSMutableArray  *resultArray = [[NSMutableArray alloc] initWithCapacity:0];
    NSString *strComma=@",";
   
    NSMutableString *query=[[[NSMutableString alloc]init] autorelease];
    NSMutableString *strtemp=[[[NSMutableString alloc]init] autorelease];
   
    [query appendString:@"SELECT "];
    for(int i=0;i<[arrColumnName count];i++)
    {
        [query appendString:[arrColumnName objectAtIndex:i]];
        [query appendString:strComma];
       
    }
    NSString *strQueryStar=[NSString stringWithFormat:@"%@",query];
    strQueryStar = [strQueryStar substringToIndex:[strQueryStar length] - 1];
    [strtemp appendString:strQueryStar];
   
    [strtemp appendString:@" FROM "];
    [strtemp appendString:TableName];
    [strtemp appendString:@" WHERE "];
   
    [strtemp appendString:ColumnNameCondition];
    [strtemp appendString:ColumnValue];
   
    const char *sql = [strtemp UTF8String];
    sqlite3_stmt *selectStatement;
   
    //prepare the select statement
    int returnValue = sqlite3_prepare_v2(database, sql, -1, &selectStatement, NULL);
    if(returnValue == SQLITE_OK)
    {
        sqlite3_bind_text(selectStatement, 1, sql, -1, SQLITE_TRANSIENT);
        //loop all the rows returned by the query.
        NSMutableArray *arrColumns = [[NSMutableArray alloc] initWithCapacity:0];
        for (int i=0; i<sqlite3_column_count(selectStatement); i++)
        {
            const char *st = sqlite3_column_name(selectStatement, i);
            [arrColumns addObject:[NSString stringWithCString:st encoding:NSUTF8StringEncoding]];
        }
        int intRow =1;
        while(sqlite3_step(selectStatement) == SQLITE_ROW)
        {
            NSMutableDictionary *dctRow = [[NSMutableDictionary alloc] initWithCapacity:0];
            for (int i=0; i<sqlite3_column_count(selectStatement); i++)                
            {
                int intValue = 0;
                double dblValue =0;
                const char *strValue;
                switch (sqlite3_column_type(selectStatement,i))
                {
                    case SQLITE_INTEGER:
                        intValue  = (int)sqlite3_column_int(selectStatement, i);
                        [dctRow setObject:[NSNumber numberWithInt:intValue] forKey:[arrColumns objectAtIndex:i]];
                        break;
                    case SQLITE_FLOAT:
                        dblValue = (double)sqlite3_column_double(selectStatement, i);
                        [dctRow setObject:[NSNumber numberWithDouble:dblValue] forKey:[arrColumns objectAtIndex:i]];                       
                        break;
                    case SQLITE_TEXT:
                        strValue = (const char *)sqlite3_column_text(selectStatement, i);
                        [dctRow setObject:[NSString stringWithCString:strValue encoding:NSUTF8StringEncoding] forKey:[arrColumns objectAtIndex:i]];                       
                        break;
                    case SQLITE_BLOB:
                        strValue = (const char *)sqlite3_column_value(selectStatement, i);
                        [dctRow setObject:[NSString stringWithCString:strValue encoding:NSUTF8StringEncoding] forKey:[arrColumns objectAtIndex:i]];                       
                        break;
                    case SQLITE_NULL:
                        [dctRow setObject:@"" forKey:[arrColumns objectAtIndex:i]];                       
                        break;
                    default:
                        strValue = (const char *)sqlite3_column_value(selectStatement, i);
                        [dctRow setObject:[NSString stringWithCString:strValue encoding:NSUTF8StringEncoding] forKey:[arrColumns objectAtIndex:i]];
                        break;
                }
               
            }
            [resultArray addObject:dctRow];
            [dctRow release];
            intRow ++;
        }
       
        [arrColumns release];
    }
   
    sqlite3_reset(selectStatement);
    return [resultArray autorelease];
}

No comments:

Post a Comment