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

  • Home
  • SEARCH
  • 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 4273368
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:42:13+00:00 2026-05-21T07:42:13+00:00

I am trying to insert rows into a SQLite database within Android. A call

  • 0

I am trying to insert rows into a SQLite database within Android. A call to insert a record into my DB looks like this: DataHelper.insertChild(context, child). My goal is to wrap the functionality into statically called functions that handle all the nitty gritty. First let me share with you my code.

I am doing this via static methods:

private static SQLiteDatabase prepareChildDatabase(Context context) {
    Log.d(TAG, "DB: prepareChildDatabase");
    OpenHelper openHelper = new OpenHelper(context);
    return openHelper.getWritableDatabase();
}

Then I do my insert:

    public static long insertChild(Context context, Child child) {
    Log.d(TAG, "DB: insertChild");

    SQLiteDatabase db = prepareChildDatabase(context);
    SQLiteStatement insertStmt = db.compileStatement(INSERT);
    insertStmt.bindLong(1, child.getBirthday().getTime());

    // These are optional
    if(child.getName() != null) {
        insertStmt.bindString(2, child.getName());
    } else {
        insertStmt.bindNull(2);
    }

    if(child.getPhoto() != null) {
        String photoUri = child.getPhoto().toString();
        insertStmt.bindString(3, photoUri);
    } else {
        insertStmt.bindNull(3);
    }

    final long id = insertStmt.executeInsert();

    // insertStmt.clearBindings(); // Not sure this is necessary
    insertStmt.close();
    db.close();

    return id;
}

For references, my OpenHelper is here:

    private static class OpenHelper extends SQLiteOpenHelper {

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.d(TAG, "DB: OpenHelper: onCreate: " + CREATE_CHILDREN_TABLE);
        db.execSQL(CREATE_CHILDREN_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
}

And finally, the variables, statements, and table:

private static final String TABLE_NAME = "children";
private static final String COL_NAME = "name";
private static final String COL_BDAY = "birthday";
private static final String COL_PHOTO = "photo";

private static final String INSERT = "insert into " 
    + TABLE_NAME + " (" 
        + COL_BDAY + ", "
        + COL_NAME + ", " 
        + COL_PHOTO + ") values (?, ?, ?)";

private static final String UPDATE = "update " 
    + TABLE_NAME + "set "
        + COL_BDAY + " = ?, "
        + COL_NAME + " = ?, "
        + COL_PHOTO + " = ?) WHERE "
        + BaseColumns._ID + " = ?";

private static final String CREATE_CHILDREN_TABLE = 
    "CREATE TABLE " + TABLE_NAME + " ("
        + BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
        + COL_BDAY + " INTEGER NOT NULL, "
        + COL_NAME + " TEXT NULL, "
        + COL_PHOTO + " TEXT NULL)";

So what is happening is that the first time I call this everything works fine. Insert works, data is there, and I can export the DB from the device to my SQLite browser. Subsequent calls to insertChild(…) break the DB. Opening doesn’t work in that SQLiteBrowser shows no tables or rows and opening it within a text editor shows some things; my guess is that the DB is corrupted. No more rows are being inserted and I can’t read it. No errors are thrown whatsoever.

I am guessing one of two things going on here. First, my handles aren’t opening/closing correctly and some reference somewhere is getting borked by the subsequent calls. Second, my understanding of prepared statements is faulty. The thing is that I look at the code and it seems fine. Open a database, bind to a prepared statement, execute, close statement, close database.

Anyone out there willing to help me out with this? I’ve been logging, looking for exceptions, and trying things but nothing seems to work. I think I may have simply missed a little detail. That or I am going insane.

Thanks,

Mike

Update

I’ve turned my attention towards threads because the issue doesn’t reproduce on my emulator. Check out these links:

http://www.kagii.com/journal/2010/9/10/android-sqlite-locking.html

android sqlite application is being forcefully closed on device

What are the best practices for SQLite on Android?

  • 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-21T07:42:13+00:00Added an answer on May 21, 2026 at 7:42 am

    It appears that the device I am testing on has problems with permissions and writing the database files. The issue is actually that the DB is initially created, rows inserted, and then it is locked. Subsequent calls corrupt it.

    • http://code.google.com/p/android/issues/detail?id=949
    • http://groups.google.com/group/android-developers/browse_thread/thread/a0959c4059359d6f

    This is probably the result of rooting the device. Or perhaps because the packages UID was changed.

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

Sidebar

Related Questions

During onCreate() I'm trying to insert about 20 rows into the SQLite database (these
I am trying to insert over 70,000 rows into a javascript database (using Chrome
I am trying to insert rows into a MySQL database from an Access database
I'm trying to insert a comment character into a string something similar to this:
I'm trying to set up a simple SQLite database in Android, handling the schema
I am trying to insert rows into a table that has no unique field
I am trying to insert rows into an html table with javascript. I have
I'm trying to insert some data into a local MySQL database by using MySQL
I am trying to insert 2 rows into the same table. The first will
I'm trying to insert a new object into my database. I followed a step-by-step

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.