1

i have an problem when i select usernames the return is only one object and the table contain all of usernames how to solve this i try to select by database manager and i find that query sql statment is right

 - (NSMutableArray*) FindAccounts
    {
    ///database
    NSString *docsDir;
    NSArray *dirPaths; 
    // Get the documents directory
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    docsDir = [dirPaths objectAtIndex:0];  
    // Build the path to the database file
    databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"TRIAL.sqlite"]];
    NSFileManager *filemgr = [NSFileManager defaultManager];
    sqlite3_stmt *statement;
    const char *dbpath = [databasePath UTF8String];
    if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
    {
    NSString *querySQL = [NSString stringWithFormat:@"SELECT USERNAME,TYPE FROM CONTNEW"];
    const char *query_stmt = [querySQL UTF8String];
    NSMutableArray *resultArray = [[NSMutableArray alloc]init];
    if (sqlite3_prepare_v2(contactDB,query_stmt, -1, &statement, NULL) == SQLITE_OK)
    {
    if (sqlite3_step(statement) == SQLITE_ROW)
    {
    NSString *USERS = [[NSString alloc] initWithUTF8String:(const char *)     sqlite3_column_text(statement, 0)];
    [resultArray addObject:USERS];
    return resultArray;
    }
    else{
    NSLog(@"Not found");
    return nil;
    }
    sqlite3_reset(statement);
    }
    }
    return nil;
    }
3
  • Why? because you aren't looping. Commented Oct 28, 2014 at 10:02
  • how can i make looping for this ? thanks Commented Oct 28, 2014 at 10:03
  • Using a while statement instead of an if statement. Remove those early return statements as well as you have resource leaks (hint: close the database). Commented Oct 28, 2014 at 10:04

1 Answer 1

2

OK, try this (note that there is not much error reporting):

- (NSMutableArray*) FindAccounts
{
    NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *databasePath = [docsDir stringByAppendingPathComponent:@"TRIAL.sqlite"];
    sqlite3_stmt *statement;
    // I assume contactDB is an instance variable?
    if (!contactDB &&
        sqlite3_open([databasePath UTF8String], &contactDB) != SQLITE_OK) {
        NSLog(@"Failed to open database");
        return nil;
    }

    NSString *querySQL = @"SELECT USERNAME,TYPE FROM CONTNEW ORDER BY USERNAME";
    NSMutableArray *resultArray = [NSMutableArray new];
    if (sqlite3_prepare_v2(contactDB, [querySQL UTF8String], -1, &statement, NULL) == SQLITE_OK)
    {
        while (sqlite3_step(statement) == SQLITE_ROW)
        {
            NSString *username = @(sqlite3_column_text(statement, 0));
            [resultArray addObject:username];
        }
        sqlite3_reset(statement);
    }
    return resultArray;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.