每次启动应用程序时都会重新创建Android数据库

时间:2010-07-29 20:47:34

标签: android sqlite

为什么每次我的应用程序启动时SQLiteOpenHelper都会调用onCreate()。这是我的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");
  }

我的扩展SQLiteOpenHelper类周围有一个外部类,当我查询时,我这样做:

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

并因为if语句

而跳过此块
if (cursor.moveToFirst()) {...}

这是我的整个数据库包装类:

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 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 && !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 个答案:

答案 0 :(得分:12)

查看你的DBWrapper构造函数,

你正在打电话

context.deleteDatabase(DATABASE_NAME);

每次调用它时都会删除数据库文件。强制SQLHelper重新创建数据库。

答案 1 :(得分:1)

以下链接有望帮助。

stackoverflow.com -> Query if Adroid DB exists!

希望有所帮助

上面的海报还提到你正在删除数据库

content.deleteDabase(DATABASE_NAME)
你在DBWrapper()构造函数

中的

答案 2 :(得分:0)

试试这个:

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);
    }

}

答案 3 :(得分:0)

如果您想在第一次启动应用时创建一次数据库,则需要修改该查询。

您应该检查数据库是否已存在,如果不是,则创建数据库,否则不创建数据库。

CREATE TABLE如果不存在TABLE_NAME(key data_type);

在onCreate方法中调用此查询。