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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:09:05+00:00 2026-06-18T18:09:05+00:00

I’m using SQLite for the first time, and am trying to learn its exception

  • 0

I’m using SQLite for the first time, and am trying to learn its exception handling, so I’m forcing an insert error in my test app. The exception occurs and I see it written to the Eclipse LogCat output window. However it doesn’t get caught in the code. I’ve seen other questions here about being sure to use the correct exception type, and think I’ve got that right. Any idea what I’m missing?

In the following statement, which is in my main activity, myTable is a class which extends my own AbstractDbAdapter (which has a class DatabaseHelper that extends SQLiteOpenHelper).

try {
    myTable.create("dupkey");
}
catch (android.database.sqlite.SQLiteConstraintException e) {
    Log.e(TAG, "SQLiteConstraintException:" + e.getMessage());
}
catch (android.database.sqlite.SQLiteException e) {
    Log.e(TAG, "SQLiteException:" + e.getMessage());
} 
catch (Exception e) {
    Log.e(TAG, "Exception:" + e.getMessage());
}

Sample stack trace:

Error inserting id="dupkey" last_seen_ts=1360624732 first_seen_ts=1360624732 android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
  at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
  at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:61)
  at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1582)
  at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1426)
  at com.myCompany.testApp.myTable_DbAdapter.create(myTable_DbAdapter.java:51)

The myTable and AbstractDbAdapter classes:

public class myTable_DbAdapter extends AbstractDbAdapter {

    private static final String DATABASE_TABLE = "myTable";

    // column names -- keys for ContentValues()
    public static final String KEY_ID = "id";
    public static final String KEY_FIRST_SEEN = "first_seen_ts";
    public static final String KEY_LAST_SEEN = "last_seen_ts";

    public myTable_DbAdapter(Context ctx) {
        super(ctx);
    }

    public long create(String id) {
        long firstSeen = System.currentTimeMillis() / 1000;  // SQLite timestamps are in seconds

        ContentValues args = new ContentValues();
        args.put(KEY_ID, id);
        args.put(KEY_FIRST_SEEN, firstSeen);
        args.put(KEY_LAST_SEEN, firstSeen);  // defaults to firstSeen for a new entry

        return mDb.insert(DATABASE_TABLE, null, args);
    }
}

public abstract class AbstractDbAdapter {

    protected static final String TAG = "AbstractDbAdapter";

    protected DatabaseHelper mDbHelper = null;
    protected SQLiteDatabase mDb = null;

    protected static final String TABLE_CREATE_MYTABLE =
        "create table myTable (" +
        "  id text primary key not null" +
        ", first_seen_ts integer not null" +
        ", last_seen_ts integer not null" +
        ");";

    protected static final String DATABASE_NAME = "myDB";
    protected static final int DATABASE_VERSION = 1;

    protected final Context mCtx;

    protected static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // Note: SQLite requires one execSQL per table
            db.execSQL(TABLE_CREATE_MYTABLE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which destroys existing data.");
            db.execSQL("DROP TABLE IF EXISTS myTable");
            onCreate(db);
        }
    }

    public AbstractDbAdapter(Context ctx) {
        this.mCtx = ctx;
    }

    public AbstractDbAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        if (mDb != null) {
            mDb.close();
            mDb = null;
        }
        if (mDbHelper != null) {
            mDbHelper.close();
            mDbHelper = null;
        }
    }

}
  • 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-18T18:09:06+00:00Added an answer on June 18, 2026 at 6:09 pm

    I found the answer here: SQLiteConstraintException not caught

    The SQLiteDatabase.insert() method doesn’t throw an exception. d’oh!

    For other SQLite newbies like me, if you want to catch exceptions when inserting into the database, use the SQLite.insertOrThrow() method. It will throw an exception which you can then catch and handle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am using Paperclip to handle profile photo uploads in my app. They upload
I'm making a simple page using Google Maps API 3. My first. One marker
I am using JSon response to parse title,date content and thumbnail images and place
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.