11
votes

Why is SQLiteOpenHelper calling onCreate() every time my application starts up. Here's my code for onCreate()

@Override
public void onCreate(SQLiteDatabase db) {
    Log.i("onCreate()", "Enter");

    //create cards table
    db.execSQL(         
        "create table circles" + 
        "("+
        "id integer primary key,"+
        "x integer not null," +
        "y integer not null"+
        ")"
    );     

    Log.i("onCreate()", "Exit");
}

I have an outside class around my extended SQLiteOpenHelper class, and when I query, I do this:

Cursor cursor = openHelper.getWritableDatabase().rawQuery("select * from circles", null); 

and skips this block because of this if statement

if (cursor.moveToFirst()) {...}

Here's my entire Database wrapper class:

package db.main;

import java.util.ArrayList;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import testing.main.Circle;

public class DBWrapper {

   private static final String DATABASE_NAME = "circles.db";
   private static final int DATABASE_VERSION = 1;
   private static final String[] TABLES = new String[] { "circles"};

   private Context context;
   private OpenHelper openHelper;

   public DBWrapper(Context context) {
       context.deleteDatabase(DATABASE_NAME);
      this.context = context;
      this.openHelper = new OpenHelper(this.context);
   }

   public void insertCircle(Circle c) {
       String sql = "insert into circles (x, y) values (" + c.getX() + ", " + c.getY() + ")";
       Log.i("DBWrapper::insertCircle()", "Executing sql: " + sql);
       openHelper.getWritableDatabase().execSQL(sql);
   }
   
   public void clearCircles() {
       String sql = "delete * from circles";
       Log.i("DBWrapper::clearCircles()", "Executing sql: " + sql);
       openHelper.getWritableDatabase().execSQL(sql);
   }
   
   public ArrayList<Circle> getCircles() {
       ArrayList<Circle> circles = new ArrayList<Circle>();
       Cursor cursor = openHelper.getWritableDatabase().query(TABLES[0], null, null, null, null, null, null);
       //Cursor cursor = openHelper.getWritableDatabase().rawQuery("select * from circles", null);
       Log.i("DBWrapper::getCircles()", "move to first1");
       if (cursor.moveToFirst()) {
           Log.i("DBWrapper::getCircles()", "move to first");
           do {
               Log.i("DBWrapper::getCircles()", "Creating circle: " + cursor.getString(1) + ", " + cursor.getString(2));
               circles.add(new Circle(Integer.parseInt(cursor.getString(1)),
                                    Integer.parseInt(cursor.getString(2)))); 
           } while (cursor.moveToNext());
       }
       if (cursor != null && !cursor.isClosed()) {
           cursor.close();
       }
       return circles;
   }

   private static class OpenHelper extends SQLiteOpenHelper {

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

      @Override
      public void onCreate(SQLiteDatabase db) {
          Log.i("OpenHelper::onCreate()", "Enter");
          //create cards table
         db.execSQL(         
         "create table circles" + 
         "("+
         "id integer primary key,"+
         "x integer not null," +
         "y integer not null"+
         ")"
         );

          Log.i("OpenHelper::onCreate()", "Exit");
      }

      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
         Log.w("Example", "Upgrading database, this will drop tables and recreate.");
         for(String s: TABLES) {
             db.execSQL("DROP TABLE IF EXISTS " + s);
         }
         onCreate(db);
      }
   }
}
4
Hmmm. Are you testing on an emulator? If so, perhaps you should check to be sure you are not wiping user data from the emulator before starting it.Hamy
Perhaps you could post your entire body of database code? I don't see the problem in what you have here :/Hamy
PS - it might be good to add "IF NOT EXISTS" to your create SQL. That will prevent either an error or overwriting the old dbHamy

4 Answers

12
votes

Look at your DBWrapper constructor,

you're calling

context.deleteDatabase(DATABASE_NAME);

This will delete the database file every time you call it. Forcing the SQLHelper to recreate the database.

1
votes

The link below hopefully helps.

stackoverflow.com -> Query if Adroid DB exists!

Hope that helps

The poster above also mentioned you are deleting the database

content.deleteDabase(DATABASE_NAME)

in you DBWrapper() constructor

1
votes

Try this :

public class DataBaseHelper extends SQLiteOpenHelper {
    private static final String DATENBANK_NAME = "yourdatabase.db";
    private static final int DATENBANK_VERSION = 1;

    public DataBaseHelper(Context context) {
        super(context, DATENBANK_NAME, null, DATENBANK_VERSION);
    }

    public void onCreate(SQLiteDatabase db) {
        db.execSQL(PartialTripTbl.SQL_CREATE);
    }

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

}
0
votes

If you want to create once database when app launch first time , then you need to modified that query.

You should need to check database already exists or not, if not than create database else not create database.

CREATE TABLE if not exists TABLE_NAME (key data_type);

Call this query inside onCreate method.