2010-07-29 142 views
8

爲什麼每次啓動應用程序時SQLiteOpenHelper都會調用onCreate()。這是我對的onCreate()代碼每次啓動應用程序時都會重新創建Android數據庫

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

}}

讓我知道如果我需要發佈更多的代碼或任何東西。謝謝。

+2

嗯海報。你在模擬器上測試嗎?如果是這樣,也許你應該檢查一下,確保你沒有在啓動之前擦除模擬器中的用戶數據。 – Hamy 2010-07-29 20:59:00

+0

不,我正在測試設備 – wangburger 2010-07-29 21:29:36

+0

也許你可以發佈你的整個數據庫代碼?我在這裏看不到問題:/ – Hamy 2010-07-29 22:03:39

回答

10

看看你DBWrapper構造,

你打電話

context.deleteDatabase(DATABASE_NAME);

這每次調用時將刪除數據庫文件。強制SQLHelper重新創建數據庫。

+1

哦哇... 謝謝你指出。現在我覺得啞 – wangburger 2010-07-30 12:22:09

+0

@LizB plz幫助我[鏈接](http://stackoverflow.com/questions/41755868/android-sqlite-database-not-working-when-returning-to-main-activity)在我的問題 – Tauseef 2017-01-20 05:30:34

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

} 
+0

這幾乎是我所擁有的,它不起作用。 – wangburger 2010-07-30 01:40:56

相關問題