1

I to use sqlite database in my application. in my sqlite exist 10 records. I want read 4 data from this database and I want get this data until BroID in last data to be NULL (BroID is one of columns data) this is my code but I dont know how to use of loop in my code until BroID to be NULL.

    -(NSMutableArray *)readInformationFromDatabase
    {
        array = [[NSMutableArray alloc] init];

        // Setup the database object
        sqlite3 *database;
        // Open the database from the users filessytem
        if(sqlite3_open([[self dataFilePath] UTF8String], &database) == SQLITE_OK)
        {
// I want to use loop for certain work!!! (this work is get name from data base until BroID to be NULL )
            NSString *sqlStatement_userInfo =[NSString stringWithFormat:@"Select * from table1"];
                sqlite3_stmt *compiledStatement;
                if(sqlite3_prepare_v2(database, [sqlStatement_userInfo UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK)
                {
                    // Loop through the results and add them to the feeds array
                    while(sqlite3_step(compiledStatement) == SQLITE_ROW)
                    {
                        // Init the Data Dictionary
                        NSMutableDictionary *_dataDictionary=[[NSMutableDictionary alloc] init];
                        NSString *_userName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
                        [_dataDictionary setObject:[NSString stringWithFormat:@"%@",_userName] forKey:@"Name"];

                        [array addObject:_dataDictionary];
                    }
                }
                else
                {
                    NSLog(@"No Data Found");
                }

please get me idea for done this code.

6
  • I BroID a column name? Commented May 18, 2013 at 6:11
  • 1
    no my friend 'BroID' is another column and 'Name' is another column Commented May 18, 2013 at 6:12
  • Running a search to look for a similar topic should help you. Commented May 18, 2013 at 6:14
  • What does this mean "BroID in last data"? Commented May 18, 2013 at 6:26
  • what error it display? Commented May 18, 2013 at 6:33

1 Answer 1

1

OK first the SELECT statement:

  • Change the statement from SELECT * ... to SELECT colname1, colname2, ... so you are then certain of the order in which columns are returned, and you won't have to refer to the schema in order to find out what order they come back in. This actually saves time.
  • BroID must be included in the columns being selected.
  • You'll want an ORDER BY clause in order to get consistent results.
  • You can probably get the database to only include rows WHERE BroID IS NOT NULL, which might suite your needs.

If you still need to use code to stop the fetching, then simply test for a NULL BroID column and break out of the while loop using:

while (sqlite3_step(compiledStatement) == SQLITE_ROW)
{
    // Fetch other columns
    if (sqlite_column_type(compiledStatement, INDEX) == SQLITE_NULL)
        break;
}

Where INDEX is the column index of BroID.

It's not clear if you want the row where BroID IS NULL in the result set; if you don't then perform the sqlite_column_type() test before fetching the columns, else leave it as above.

Refer to the reference for details.

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.