Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8199523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:11:07+00:00 2026-06-07T06:11:07+00:00

I want to load tableView values from a database, called for test purposes database.sqlite.

  • 0

I want to load tableView values from a database, called for test purposes database.sqlite.
It is populated with the following:

1, January

2, February

… down to …

12, December

As you will see I’m using FMDB. The following line compiles but crashes:

NSString *myString = [[rows objectAtIndex:myNumber] objectForKey:@"MonthName"];

I’m having problems coming to terms with the inconsistent way you deal with Ints and String in Cocoa which is part of my problem. Also, although there are NSLog debugging lines in there this approach looks convoluted. What advice can you give me?

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *path = [docsPath stringByAppendingPathComponent:@"database.sqlite"];
FMDatabase *database = [FMDatabase databaseWithPath:path];
[database open];

NSLog(@"Open Database");
FMResultSet *results = [database executeQuery:@"SELECT * FROM monthly"];

while([results next]) {
    NSString *name = [results stringForColumn:@"name"];
    NSInteger age  = [results intForColumn:@"age"];

    // Stuff Data into Array
    NSMutableArray *rows = [[NSMutableArray alloc] init];
    NSMutableDictionary *firstRow = [NSMutableDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:age], @"MonthID", [NSString stringWithFormat:@"%@", name], @"MonthName", nil];
    [rows addObject:firstRow];
    NSLog(@"Month: %@ - %d",name, age);
    int myNumber = [[firstRow objectForKey:@"MonthID"] intValue];

    // *** THE LINE BELOW IS GIVING ME PROBLEMS ***
    NSString *myString = [[rows objectAtIndex:myNumber] objectForKey:@"MonthName"];

    // NSLog(@"Month No: %@ - %d",myString, myNumber);
    NSLog(@"***** Month No: %d", myNumber);
    // [self.monthMonths myString]; 
}
[database close];
NSLog(@"Close Database");
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T06:11:10+00:00Added an answer on June 7, 2026 at 6:11 am

    This line gives error because your index or month id is starting from 1 but when you add it in array:

    [rows addObject:firstRow];
    

    it gets added at 0 index(default starting index).
    So when you call this line:

    NSString *myString = [[row s objectAtIndex:myNumber] objectForKey:@"MonthName"];
    

    it tries to extract value at index 1 because:

    int myNumber = [[firstRow objectForKey:@"MonthID"] intValue];
    

    It actually tries to extract value at index 1 which is not present because your array contain only 1 value at index 0 so it crashes.

    Edit:Try using this:

    NSMutableArray *rows = [[NSMutableArray alloc] init];
    while([results next]) {
        NSString *name = [results stringForColumn:@"name"];
        NSInteger age  = [results intForColumn:@"age"];
    
        NSMutableDictionary *firstRow = [NSMutableDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:age], @"MonthID", [NSString stringWithFormat:@"%@", name], @"MonthName", nil];
    
    // Stuff Data into Array
        [rows addObject:firstRow];
        NSLog(@"Month: %@ - %d",name, age);
        int myNumber = [[firstRow objectForKey:@"MonthID"] intValue]-1; //to get value at proper index...
    
        // *** THE LINE BELOW IS GIVING ME PROBLEMS ***
        NSString *myString = [[rows objectAtIndex:myNumber] objectForKey:@"MonthName"];
    
        // NSLog(@"Month No: %@ - %d",myString, myNumber);
        NSLog(@"***** Month No: %d", myNumber);
        // [self.monthMonths myString]; 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to load my data on scroll down for my tableView. I have
I want to load data from MySql database to a HTTP form When I
I have a UINavigationController consisting of a tableview I want to load some data
I want to load PNG-file from resources. There is a roughly MFC-way (with CResourceStream
In CustomCell.m I define init method where I want to load cell from the
I have an NSTableView that I want to load it's value from an NSArrayController.
Basically I want to recreate the filebrowser (drilldown Tableview) from the DropBox iPhone app
i have a list of image categories in a tableview. i want to load
I use a grouped tableview and custom cells which I load from nib. When
I have an NSMutableArray and I load my tableview from it. Now I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.