• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Sqlite code should be executed only once

Hi,

Am new to android so please forgive me if i go wrong somewhere.

I am developing a app using android C2DM. sqlite3 is my back-end. Everything is just working fine but am struck with an performance issue regarding sqlite3. so my question is 'can i place the database code somewhere where it will be executed only once i.e The dvm(dalvik vertiual machine) should execute the code pertaining to the db only once; during the successive run the dvm should not go thru the (db)code because the db had been created.
More specifically, my app send msg to all the phones which has my app. so when an app at the client end receives a msg, the dvm should not execute this code : SQLiteDatabase db;


//use tat ref to open or create a table
db = openOrCreateDatabase( "/data/data/de.vogella.android.c2dm.simpleclient/app_database/file__0/0000000000000001.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);

try
{

//initialsiging a query with all the table fields
final String CREATE_TABLE_CONTAIN = "CREATE TABLE IF NOT EXISTS tbl_Message4("
+ "id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "msg TEXT not null,"
+ "msg_time INTEGER not null,"
+ "msg_status INTEGER not null);";

//execute the above query
db.execSQL(CREATE_TABLE_CONTAIN);

Because everytime a msg comes this code ll be executed. So i want to avoid this, i guess i hv conveyed my message. Any help ll be much appreciated.


Thanks,
TheIlliterate
 
Welcome ThIlliterate! This is a very specific question (one I have no idea for the answer). If you don't mind, I'll move this over to the Android Application Development forum. There's a few tutorials over there as well that may be able to get you the answer.
Enjoy your time here!
 
Here's the skeleton:

Code:
public class DatabaseHelper extends SQLiteOpenHelper {
    public static String DB_DIR = "/data/data/<package>/database/";
    public static final String OLD_DB_PATH = DB_DIR + "old_" + DB_NAME;

       private DatabaseHelper(Context context, String name, CursorFactory factory,
                        int version) {
                super(context,  name, factory, version);

                DB_PATH = context.getDatabasePath(DB_NAME).getAbsolutePath();
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

                /* WILL BE CALLED ONLY IF DB DOES NOT EXIST */
                createDatabase = true;

                LogCat.debug(this, "Creating new database @ " + db.getPath());
                createTable(db);

        }

        private void createTable(SQLiteDatabase db) {
           /* YOUR LOGIC TO CREATE TABLE GOES IN HERE */
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
           /* THIS IS CALLED WHEN YOU CHANGE YOUR DB VERSION i.e. UPGRADE YOUR DB (add new cols etc.) */
        }


}

More on SQLiteOpenHelper

SQLiteOpenHelper | Android Developers

Hopefully this helps.
 
Back
Top Bottom