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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:59:14+00:00 2026-05-27T07:59:14+00:00

I use SQLite in android development. When I insert a row using insert, I

  • 0

I use SQLite in android development. When I insert a row using insert, I get the number of the row. However, I want the KEY_ID which is the primary key of the table. I used the function below, but I got:

Caused by: java.lang.IllegalStateException: get field slot from row 0 col -1 failed
    at android.database.CursorWindow.getLong_native(Native Method)
    at android.database.CursorWindow.getInt(CursorWindow.java:434)
    at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:93)
    at winterfarmer.timemama.DataBaseAdapter.insertData(DataBaseAdapter.java:89)
    at winterfarmer.timemama.ManagerActivity.addNewRecord2DB(ManagerActivity.java:106)
    at winterfarmer.timemama.ManagerActivity.addNewRecord(ManagerActivity.java:100)
    at winterfarmer.timemama.ManagerActivity.addNewRecord2DB(ManagerActivity.java:106)
    at winterfarmer.timemama.ManagerActivity.addNewRecord(ManagerActivity.java:100)
    at winterfarmer.timemama.ManagerActivity.setRecordOK(ManagerActivity.java:92)
    at winterfarmer.timemama.ManagerActivity.setRecord(ManagerActivity.java:78)
    at winterfarmer.timemama.ManagerActivity.onActivityResult(ManagerActivity.java:64)
    at android.app.Activity.dispatchActivityResult(Activity.java:3890)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:3511)

How can i fix this bug?

public int insertData(RecordInfo recordInfo) {
    Log.d(TAG, "insertData");

    ContentValues initialValues = new ContentValues();

    initialValues.put(KEY_HOUR, recordInfo.hour);
    initialValues.put(KEY_MINUTE, recordInfo.minute);
    initialValues.put(KEY_REPEAT, recordInfo.repeat);

    long rowId = db.insert(DB_ALARM_TABLE, null, initialValues);
    assert rowId >= 0 : "insertData failed";
    Log.d(TAG, "insertData, rowId = " + rowId);
    Cursor cur = db.rawQuery("SELECT last_insert_rowid() from "
            + DB_ALARM_TABLE, null);
    cur.moveToFirst();
    int colId = cur.getColumnIndex(KEY_ID);
    Log.d(TAG, "insertData, colId = " + colId);
    assert colId >= 0 : "insertData failed";
    return cur.getInt(colId);
}
  • 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-27T07:59:15+00:00Added an answer on May 27, 2026 at 7:59 am

    From the Cursor doc:

    public abstract int getColumnIndex (String columnName)

    Since: API Level 1
    Returns the zero-based index for the given column
    name, or -1 if the column doesn’t exist. If you expect the column to exist
    use getColumnIndexOrThrow(String) instead, which will make the error more clear

    So that would explain the problem better.

    Anyway, back to your actual problem:

    The problem is that KEY_ID is not a result column of your select query (the result will not be named KEY_ID, even though the value originally comes from the KEY_ID column). It will have only one column result, so it doesn’t make sense to ask for the column index. colId will be 0. You can directly do:

    Cursor cur = db.rawQuery("SELECT last_insert_rowid() from "
            + DB_ALARM_TABLE, null);
    cur.moveToFirst();
    return cur.getInt(0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pre-baked sqlite database that I want to use in my Android
I have an application which requires the use of SQLite (Android's database). I have
How do I use prepared statements in SQlite in Android?
I use SQLite for a number of application on the desktop and PDA. Most
I want to use SQLite within my MFC application. for that, i'll create an
Would be great to get some direction re trying to use sqlite for my
I am using SQLite in a project used by an android application. Currently I
It does not seem possible to use REGEXP in a SQLite query in Android.
I'm using SQLite in an Android application. In all of my tables I have
I'm currently developing an Android game which saves data into a SQLite database. This

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.