Teja Nandamuri
Teja Nandamuri

Reputation: 11201

Unable to store sqlite3_last_insert_rowid into an array

I am using the following code to store "sqlite3_last_insert_rowid" into NSMutableArray,I am getting the rowid but nothing is stored in the array. It is giving me null.

  NSUInteger rowIDNum=sqlite3_last_insert_rowid(myDatabase);
        NSNumber *xWrapped = [NSNumber numberWithInt:rowIDNum];
        [_rowID insertObject:xWrapped atIndex:0];
        NSLog(@"row ID array %@",[_rowID objectAtIndex:0]);

Could you please tell me the correct way to store rowid into an array?

Upvotes: 0

Views: 53

Answers (1)

trojanfoe
trojanfoe

Reputation: 122391

I suspect the array is not allocated, else you would get an NSRangeException if the array was allocated but empty (i.e. the first time you called that method).

From the NSMutableArray reference:

index

The index in the array at which to insert anObject. This value must not be greater than the count of elements in the array.

Important: Raises an NSRangeException if index is greater than the number of elements in the array.

You would normally allocate the array in the class init method or viewDidLoad method, depending on what the class is. Once you've allocated the array, use:

NSUInteger rowIDNum=sqlite3_last_insert_rowid(myDatabase);
[_rowID insertObject:@(rowIDNum)];

Upvotes: 1

Related Questions