Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid database recreates every time application is launched
    primarykey
    data
    text
    <p>Why is SQLiteOpenHelper calling onCreate() every time my application starts up. Here's my code for onCreate()</p> <pre><code>@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"); } </code></pre> <p>I have an outside class around my extended SQLiteOpenHelper class, and when I query, I do this:</p> <pre><code>Cursor cursor = openHelper.getWritableDatabase().rawQuery("select * from circles", null); </code></pre> <p>and skips this block because of this if statement</p> <pre><code>if (cursor.moveToFirst()) {...} </code></pre> <p>Here's my entire Database wrapper class:</p> <p><pre> package db.main;</p> <p>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;</p> <p>public class DBWrapper {</p> <p>private static final String DATABASE_NAME = "circles.db"; private static final int DATABASE_VERSION = 1; private static final String[] TABLES = new String[] { "circles"};</p> <p>private Context context; private OpenHelper openHelper;</p> <p>public DBWrapper(Context context) { context.deleteDatabase(DATABASE_NAME); this.context = context; this.openHelper = new OpenHelper(this.context); }</p> <p>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); }</p> <p>public void clearCircles() { String sql = "delete * from circles"; Log.i("DBWrapper::clearCircles()", "Executing sql: " + sql); openHelper.getWritableDatabase().execSQL(sql); }</p> <p>public ArrayList getCircles() { ArrayList circles = new ArrayList(); 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 &amp;&amp; !cursor.isClosed()) { cursor.close(); } return circles; } private static class OpenHelper extends SQLiteOpenHelper {</p> <code> 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); } </code></pre> <p>} }</p> <p></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload