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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:22:41+00:00 2026-06-12T13:22:41+00:00

I have an onCreate that currently opens an sqlite database and reads off 4

  • 0

I have an onCreate that currently opens an sqlite database and reads off 4 values. I then have a conditional and depending on what activity has sent it there it either displays those values or updates two values and then displays the other.

Now if I run this activity without updating the database it is lightning fast. Whereas if I run two queries to write to the database it can be sluggish. Is there anything I am able to do to optimise this.

The problem is the display stays on the previous activity until the sqlite updating has completed. This seems to be the problem.

Sorry for what is most likely a rubbish explanation. Please feel free to ask me to better describe anything.

Any help appreciated.

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    setContentView(R.layout.reason);

  //Opens DB connection

    type = b.getString("TYPE");


    get();
    if(type.equals("next")){ update();}
    db.close();
    }


    public void get(){      
Cursor b = db.rawQuery("SELECT * FROM " +
        DB_TABLE2 +" WHERE _id='1'" , null);
b.moveToFirst();
id = b.getInt(b.getColumnIndex("nextq"));
    nextvalue = b.getInt(b.getColumnIndex(type));
if(nextvalue==0){nextvalue=1;}
b.close();           
nextvalue ++;
    }

    public void update(){   
db.execSQL("UPDATE "
        + DB_TABLE2
        + " SET nextq='" + nextvalue + "'"
        + " WHERE _id='1'");
db.execSQL("UPDATE "
        + DB_TABLE
        + " SET answered_correctly='" + anscorrect +"' , open ='1' WHERE _id='" + id + "'");
    }
  • 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-12T13:22:43+00:00Added an answer on June 12, 2026 at 1:22 pm

    You can do something like this, but be warned, the Pragma Syncronize setting can be dangerous, as it turns off security features in Sqlite. Having said that, it increased my recording to roughly 0.5ms per row, or going from 350ms down to 15-20, and for another table, going from 5000-9000ms down to roughly 300.

    // this cut down Insert time from 250-600ms to 14-30 ms.
        // with prgma syncronous set to off, this drops it down to 0.5ms/row for membernames
        final InsertHelper ih = new InsertHelper(database, SQLiteHelper.TABLE_MEMBERS);
        final int nameColumn = ih.getColumnIndex(SQLiteHelper.MEMBER_TABLE_MEMBERNAME);
        final long startTime = System.currentTimeMillis();
    
        try {
            database.execSQL("PRAGMA synchronous=OFF");
            database.setLockingEnabled(false);
            database.beginTransaction();
            for (int i = 0; i < Members.size(); i++) {
                ih.prepareForInsert();
    
                ih.bind(nameColumn, Members.get(i));
    
                ih.execute();
            }
            database.setTransactionSuccessful();
        } finally {
            database.endTransaction();
            database.setLockingEnabled(true);
            database.execSQL("PRAGMA synchronous=NORMAL");
            ih.close();
            if (Globals.ENABLE_LOGGING) {
                final long endtime = System.currentTimeMillis();
                Log.i("Time to insert Members: ", String.valueOf(endtime - startTime));
            }
        }
    

    the main things you want are the InsertHelper, the “SetLockingEnabled” features, and the “execSQL Pragma…”. Keep in mind as I said that using both of those can potentially cause DB corruption if you experience a power outage on your phone, but can speed up DB inserts greatly. I learned about this from here: http://www.outofwhatbox.com/blog/2010/12/android-using-databaseutils-inserthelper-for-faster-insertions-into-sqlite-database/#comment-2685

    You can also ignore my logging stuff, I had it in there to do some sort of benchmarking to see how fast things took.

    Edit: To explain briefly what those options do, I’m basically disabling security and integrity features in SQLite in order to basically pipe data into the database. Since this occurs so fast (around 14-20ms on average now), the risk is acceptable. If this was taking seconds to occur, I wouldn’t risk it, because in the event something happens, you could get a corrupted DB. The Syncronize Option is the greatest risk of all, so judge if you want to take that risk with your data. I would recommend using timing features like I’ve included, to see how long it takes to insert data into your db each time you try something, then determine what level of risk you want. Even if you don’t use those two, the other features (InsertHelper and the BeginTransaction stuff) are going to help improve your database work greatly.

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

Sidebar

Related Questions

I have noticed that when using actionBar.setSelectedNavigationItem(x) in the onCreate() method of my Activity,
I have an Activity that should only get created once. That is, onCreate can
I currently have a gridview that returns an images position/id to a parent activity.
Hi i currently have an Activity that hosts a tab view, and each tab
I currently have code that reads a recording in from the devices mic using
I currently have multiple activity that needs to perform an asynctask for http post
I have an activity in whose onCreate method an Init function is called (the
I have Activity with ListView inside it and in the onCreate method of the
I have a LinearLayout and its child - ImageView. My activity's onCreate does nothing
I have a problem with my activity. This is my onCreate: case NORMAL: game

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.