3

我可以很好地创建数据库,我可以插入和查看值,但是每当我在 eclipse 中关闭 android 虚拟设备并重新打开它时,数据库值就消失了!:O 请帮帮我!

有 3 个类:(1 个处理数据库和 2 个活动)

这对我来说意味着整个世界!!!:O 如果可以的话,我会 +rep 非常感谢你们!@!!!

这是我的代码

数据库管理器:`

package com.jeux;

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


public class Database2 {

private static final String DATABASE_NAME = "JeuxMotActivity";

private static final int DATABASE_VERSION = 1;

public static final String TABLE_CATEGORIE = "Categorie";
public static final String KEY_CATEGORIE_ID_CATEGORIE = "IDCategorie";
public static final String KEY_CATEGORIE_NOM_CATEGORIE = "NomCategorie";

public static final String TABLE_MOT = "Mot";
public static final String KEY_MOT_ID_MOT = "IDMot";
public static final String KEY_MOT_MOT = "Mot";
public static final String KEY_MOT_EMAIL = "Email";
public static final String KEY_MOT_ID_CATEGORIE = "IDCategorie";

public static final String TABLE_ADMINISTRATEUR = "Administrateur";
public static final String KEY_ADMINISTRATEUR_EMAIL = "Email";
public static final String KEY_ADMINISTRATEUR_PASSWORD = "Password";


public static final String TABLE_SCORE = "Score";
public static final String KEY_SCORE_ID_JEU = "IDJeu";
public static final String KEY_SCORE_USERNAME = "Username";
public static final String KEY_SCORE_SCORE = "Score";
public static final String KEY_SCORE_INITIALES = "Initiales";

public static final String TABLE_UTILISATEUR = "Utilisateur";
public static final String KEY_UTILISATEUR_USERNAME = "Username";
public static final String KEY_UTILISATEUR_PASSWORD = "Password";

public static final String TABLE_JEU = "jeu";
public static final String KEY_JEU_ID_JEU = "IDJeu";
public static final String KEY_JEU_NOM_JEU = "NomJeu";
public static final String KEY_JEU_COMMENTAIRES = "Commentaires";


private DBHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;


private static class DBHelper extends SQLiteOpenHelper {

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

    @Override
    public void onCreate(android.database.sqlite.SQLiteDatabase nb) {
        //TODO check teh constraints and the relations
        nb.execSQL("CREATE TABLE " + TABLE_CATEGORIE + " (" +
                KEY_CATEGORIE_ID_CATEGORIE + " INTEGER PRIMARY KEY    AUTOINCREMENT, " +
                KEY_CATEGORIE_NOM_CATEGORIE + " TEXT NOT NULL" +     ");"
                );

        nb.execSQL("CREATE TABLE " + TABLE_MOT + " (" +
                KEY_MOT_ID_MOT + " INTEGER PRIMARY KEY     AUTOINCREMENT, " +
                KEY_MOT_MOT + " TEXT NOT NULL," + 
                KEY_MOT_EMAIL + " TEXT NOT NULL," + 
                KEY_MOT_ID_CATEGORIE + " TEXT NOT NULL," + 
                "FOREIGN KEY(" + KEY_MOT_ID_CATEGORIE + ") REFERENCES " + TABLE_CATEGORIE + "(" + KEY_CATEGORIE_ID_CATEGORIE  + ")," +
                "FOREIGN KEY(" + KEY_MOT_EMAIL + ") REFERENCES " + TABLE_ADMINISTRATEUR + "(" + KEY_ADMINISTRATEUR_EMAIL  + ")" +
                ");"
                );

        nb.execSQL("CREATE TABLE " + TABLE_ADMINISTRATEUR + " (" +
                KEY_ADMINISTRATEUR_EMAIL + " TEXT PRIMARY KEY , " +
                KEY_ADMINISTRATEUR_PASSWORD + " TEXT NOT NULL" +     ");"
                );

        nb.execSQL("CREATE TABLE " + TABLE_SCORE + " (" +
                KEY_SCORE_ID_JEU + " TEXT NOT NULL, " +
                KEY_SCORE_USERNAME + " TEXT NOT NULL," + 
                KEY_SCORE_SCORE + " INTEGER NOT NULL," + 
                KEY_SCORE_INITIALES + " TEXT NOT NULL," + 
                "FOREIGN KEY(" + KEY_SCORE_ID_JEU + ") REFERENCES "      + TABLE_JEU + "(" + KEY_JEU_ID_JEU  + ")," +
                "FOREIGN KEY(" + KEY_SCORE_USERNAME + ") REFERENCES      " + TABLE_UTILISATEUR + "(" + KEY_UTILISATEUR_USERNAME  + ")" +
                ");"
                );

        nb.execSQL("CREATE TABLE " + TABLE_UTILISATEUR + " (" +
                KEY_UTILISATEUR_USERNAME + " TEXT PRIMARY KEY , " +
                KEY_UTILISATEUR_PASSWORD + " TEXT NOT NULL" + ");"
                );
        nb.execSQL("CREATE TABLE " + TABLE_JEU + " (" +
                KEY_JEU_ID_JEU + " INTEGER PRIMARY KEY     AUTOINCREMENT, " +
                KEY_JEU_NOM_JEU + " TEXT NOT NULL UNIQUE," + 
                KEY_JEU_COMMENTAIRES + " TEXT" + 
                ");"
                ); 
    }

    @Override
    public void onUpgrade(android.database.sqlite.SQLiteDatabase db,
            int oldversion, int newversion) {
    //  db.execSQL("DROP TABLE IF EXISTS " + TABLE_CATEGORIE);
    //  onCreate(db);
    }

}

    public Database2(Context c){
        ourContext = c;
    }

    public Database2 open() throws SQLException {
        ourHelper = new DBHelper(ourContext);
        ourDatabase = ourHelper.getWritableDatabase();
        return this;
    }

    public void close() throws SQLException{
        ourHelper.close();
    }       

    public long createCategorie(String name) {
        ContentValues cv = new ContentValues();
        cv.put(KEY_CATEGORIE_NOM_CATEGORIE, name);
        return ourDatabase.insert(TABLE_CATEGORIE, null, cv);   
    }

    public String getCategorieData(){
        String result = "";
        String[] columns = new String[] {KEY_CATEGORIE_ID_CATEGORIE,     KEY_CATEGORIE_NOM_CATEGORIE};
        Cursor c = ourDatabase.query(TABLE_CATEGORIE, columns,null, null,     null, null, null);

        int id = c.getColumnIndex(KEY_CATEGORIE_ID_CATEGORIE);
        int name = c.getColumnIndex(KEY_CATEGORIE_NOM_CATEGORIE);

        for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            result += c.getString(id) + " " + c.getString(name) + "\n";
        }

        return result;
    }

    }

`

Event Manager :



package com.jeux;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Jeuxdemots2Activity extends Activity implements OnClickListener {


  Button sqlUpdate, sqlView;
  EditText nameText;

  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  sqlUpdate = (Button) findViewById(R.id.updatebutton);
  sqlView = (Button) findViewById(R.id.afficherbutton);
  nameText = (EditText) findViewById(R.id.nametext);

  sqlUpdate.setOnClickListener(this);
  sqlView.setOnClickListener(this);    
  }

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub

    switch (arg0.getId()){
    case R.id.updatebutton:
        try{
    String name = nameText.getText().toString();
    Database2 entry = new Database2(Jeuxdemots2Activity.this);
    entry.open();
    entry.createCategorie(name);
    entry.close();
        }catch (Exception e){
            String error = e.toString();

            Dialog d = new Dialog(this);
            d.setTitle("Dang it!");
            TextView tv = new TextView(this);
            tv.setText(error);
            d.setContentView(tv);
            d.show();
        }finally{

        }


    break;

    case R.id.afficherbutton:
        Intent i = new Intent("android.intent.action.SQLVIEW");
        startActivity(i);
    break;

    }


}
}

`

视图#3:

package com.jeux;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class SQLview extends Activity {

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.afficherlayout);
    TextView tv = (TextView) findViewById(R.id.affichertextview);
    Database2 info = new Database2(this);
    info.open();
    String data = info.getCategorieData();
    info.close();
    tv.setText(data);
}
}

一张照片来说明我的情况

4

1 回答 1

5

当您关闭/重新打开您的虚拟设备时,它看起来是否每次都在进行完全启动(闪亮的 Android 字词、滑动解锁屏幕等)?如果是这样,它可能会在您每次运行应用程序时重新初始化所有内容。如果是这样,您的应用程序和数据库不再在设备上,因此数据不会持久存在。

当你创建你的模拟器时,你是否检查了“快照”选项?我相信这会将您的设备状态保存到一个文件中,以便当您关闭/重新打开模拟器时,它能够从同一点恢复。

于 2012-04-25T13:41:47.730 回答