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 7161509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:34:43+00:00 2026-05-28T13:34:43+00:00

I am trying to get the next highest PK in an SQLite database that

  • 0

I am trying to get the next highest PK in an SQLite database that was not originally set up with auto increment and already has hundreds of records. So I made the below method which seems like it should work, but I am not able to get any results from this. I have several other methods that get data from the database that work and can’t figure out what I’m doing wrong and I’m going cross-eyed so thought I’d ask for some help.

- (NSInteger)getNextSubjectId {

    DrillDownAppAppDelegate *appDelegate = (DrillDownAppAppDelegate *)[[UIApplication sharedApplication] delegate];

    FMDatabase *database = [FMDatabase databaseWithPath:appDelegate.getDBPath];    
    [database open];

    FMResultSet *rst = [database executeQuery:@"select max(subject_id) + 1 AS SUBJECT_ID from SUBJECT"];

    NSLog(@"PRINT FIRST SUBJECT_ID = %@", [rst stringForColumnIndex:0]);

    NSString *nextSubId = [rst stringForColumnIndex:0];
    [database close];

    NSLog(@"NEW SUBJECT_ID = %@", nextSubId);


    NSInteger myInt = [nextSubId intValue];

    return myInt;

    //    [maxSubjectId release];

}

The log shows:

2012-01-25 22:56:29.398 Quotes[6992:f803] PRINT FIRST SUBJECT_ID = (null)
(gdb)

The header looks like this:

//  Subject.h
//  DrillDownApp

#import <UIKit/UIKit.h>

@interface Subject : NSObject {
    NSInteger subject_id;
    NSString *category_title;
    NSString *title;
    BOOL isDirty;

}

- (id) initWithPrimaryKey:(NSInteger)pk;
- (void) addSubject;
- (NSInteger)getNextSubjectId;

@property (nonatomic, readwrite) BOOL isDirty;
@property (nonatomic, readonly) NSInteger subject_id;
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSString * category_title;

@end
  • 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-05-28T13:34:44+00:00Added an answer on May 28, 2026 at 1:34 pm

    First call [rst next]

    Also, are you sure your subject_id is a string and not an int?

    And finally, have you seen if you can use lastInsertRowID?

    I use like this:

    int lastRow = [[self getDatabase]lastInsertRowId];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am just trying to get the auto incremented value of a table that
I'm trying to get a regexp to solve the next problem: I have some
I am trying to get an array of a date plus the next 13
Trying to get comfortable with jQuery and I have encountered some sample code that
I'm trying to get the next item in a list of strings (postal codes).
I'm trying to get a next field after matching field using awk. Is there
I am trying to get the next page of the topic but it gives
I am trying get content from the link https://....com:8455 where Server's certificate is not
i am trying to get the value in the next column ie Link1 labelwhen
I'm trying to get two icons in separate spans next to an input. Example

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.