我可以很好地创建数据库,我可以插入并查看值,但每当我在eclipse中关闭android虚拟设备并重新打开它时,数据库值就不见了! :o
请帮我!
共有3个类:(1个处理数据库,2个活动)
这对我来说意味着世界! :o
如果我讨好,我会代表
谢谢你们太多了!@ !!!
这是我的代码
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);
}
}
解决方法:
当您关闭/重新打开虚拟设备时,它是否看起来每次都进行完全启动(闪亮的Android字,滑动到解锁屏幕等)?如果是这样,它可能会在您每次运行应用程序时重新初始化所有内容.如果是这样,您的应用程序和数据库将不再在设备上,因此数据将不会持久存在.
在创建模拟器时,您是否选中了“快照”选项?我相信将设备的状态保存到文件中,这样当您关闭/重新打开模拟器时,它就可以从同一点恢复.