关闭模拟器后数据从数据库丢失

时间:2013-08-17 12:47:07

标签: android database

我的程序包含一个数据库类。我从数据库检索数据到listview和代码工作。但是当我关闭模拟器并重新打开它时,所有数据都将丢失。给出了数据库类。请帮助我对数据持久性代码进行必要的更改。

classdbOpenHelper.java

package example.events1;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class classdbOpenHelper extends SQLiteOpenHelper {

    public static final String KEY_ROWID = "_id";
    public static final String KEY_DESC = "countdesc";
    public static final String KEY_DATE = "countdate";
    public static final String KEY_EVENT = "countevent";
    public static final String DATABASE_NAME= "countdb";
    public static final String DATABASE_TABLE = "countable";
    public static final int DATABASE_VERSION = 1;


    public classdbOpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + DATABASE_TABLE + "("
                + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_DESC + " TEXT, " + KEY_DATE + " TEXT, " + KEY_EVENT + " TEXT  )";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXITS " + DATABASE_TABLE);
        onCreate(db);

    }


    public Cursor fetchAllEvents() {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor mCursor = db.query(DATABASE_TABLE, new String[] {KEY_ROWID,
                KEY_DESC, KEY_DATE, KEY_EVENT },
        null, null, null, null, null );
        if (mCursor != null) {
        mCursor.moveToFirst();
        }
        return mCursor;
   }

    public Cursor fetchEventByName(String inputText,String datevalue ) throws SQLException {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor mCursor = null;
        if (inputText == null || inputText.length () == 0) {
        mCursor = db.query(DATABASE_TABLE, new String[] {KEY_ROWID,
                KEY_DESC, KEY_DATE, KEY_EVENT },
        null, null, null, null, null);
        }
        else {
            mCursor  =  db.rawQuery("SELECT * FROM countable WHERE countdesc = ? AND countdate = ?", new String[] {inputText,datevalue});           
        }

        if (mCursor != null) {
        mCursor.moveToFirst();
        }
        return mCursor;
   }

    public void delete(long id) {
        SQLiteDatabase db = this.getReadableDatabase();

        db.delete(DATABASE_TABLE, KEY_ROWID + " = ?",
                new String[] { String.valueOf(id )});
        db.close();
    }
}

1 个答案:

答案 0 :(得分:1)

更改

 String CREATE_CONTACTS_TABLE = "CREATE TABLE " + DATABASE_TABLE + "(" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_DESC + " TEXT, " + KEY_DATE + " TEXT, " + KEY_EVENT + " TEXT  )";

  String CREATE_CONTACTS_TABLE = "CREATE TABLE IF NOT EXISTS" + DATABASE_TABLE + "(" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_DESC + " TEXT, " + KEY_DATE + " TEXT, " + KEY_EVENT + " TEXT  )";

app/activity被完全销毁时(i-e;电话/模拟器重新启动时),您的数据库为空的原因是因为you haven't specified the conditions何时创建数据库,何时没有。通过进行上述更改,您的问题应该早已不复存在