我的应用程序需要一个数据库,但由于以下错误,它一直强制关闭。它适用于我的一项活动,但不适用于另一项活动
数据库处理程序:
public class DBGestion {
private static final int VERSION_BDD = 1;
private static final String NOM_BDD = "cityquest.db";
private static final String TABLE_QUEST = "table_quest";
private static final String COL_ID = "ID";
private static final int NUM_COL_ID = 0;
private static final String COL_USERNAME = "UserName";
private static final int NUM_COL_USERNAME = 1;
private static final String COL_NIVEAU ="Niveau";
private static final int NUM_COL_NIVEAU = 2;
private static final String COL_NBRIGOLETTES ="NbRigolettes";
private static final int NUM_COL_NBRIGOLETTES = 3;
private SQLiteDatabase bdd;
private MaBaseSQLite maBaseSQLite; // MaBaseSQLite extends SQLiteOpenHelper
public DBGestion(Context context) {
maBaseSQLite = new MaBaseSQLite(context, NOM_BDD, null, VERSION_BDD);
}
public void open() {
bdd = maBaseSQLite.getWritableDatabase();
}
public void close() {
bdd.close();
}
public SQLiteDatabase getBDD() {
return bdd;
}
public long initDB(String userName){
ContentValues values = new ContentValues();
values.put(COL_USERNAME, userName);
values.put(COL_NIVEAU, 0);
values.put(COL_NBRIGOLETTES, 0);
return bdd.insert(TABLE_QUEST,null, values);
}
public int getNiveau() {
Cursor c = bdd.query(TABLE_QUEST, new String[] {COL_NIVEAU}, null, null, null, null, null);
int niveau = c.getInt(NUM_COL_NIVEAU);
return niveau;
}
public long setNiveau(int niveau) {
ContentValues values = new ContentValues();
values.put(COL_NIVEAU, niveau);
return bdd.update(TABLE_QUEST,values, COL_ID + " = 0", null);
}
}
这是我使用 get 和 set 方法的代码(对不起所有法语变量名!)。基本上我首先检查问题是否已经回答(getNiveau()== 1),如果没有,我检查答案然后使一些小部件不可见,并设置Niveau(1)(不可能关闭()dbGestion 2次,因为按钮被隐藏)
在我尝试使用数据库之前我没有遇到任何问题..
public class Question extends Activity {
[variable declaration]
DBGestion dbGestion;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.question_elisa);
Intent i = getIntent();
int format = i.getIntExtra(MainActivity.FORMAT,0);
dbGestion = new DBGestion(this);
[findViewById for all my variables]
if (format == 1){
dbGestion.open();
if (dbGestion.getNiveau() == 1) {
bonneReponse.setVisibility(View.VISIBLE);
[......]
dbGestion.close();
}
bouton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String reponse = new String("");
reponse = editText.getText().toString();
if(reponse.equalsIgnoreCase("poete") ||..){
bonneReponse.setVisibility(View.VISIBLE);
[...]
}
else {...}
dbGestion.setNiveau(1);
dbGestion.close();
}});
你知道问题出在哪里吗?
这是我启动数据库的方式(在 MainActivity onCreate 方法中):
final DBGestion dbGestion = new DBGestion(this);
dbGestion.open();
dbGestion.initDB("Alex");
dbGestion.close();
这是我的 SQLiteOpenHelper 类:
public class MaBaseSQLite extends SQLiteOpenHelper {
private static final String TABLE_QUEST ="table_quest";
private static final String COL_ID ="ID";
private static final String COL_NIVEAU ="Niveau";
private static final String COL_USERNAME ="UserName";
private static final String COL_NBRIGOLETTES ="NbRigolettes";
private static final String CREATE_DB = "CREATE TABLE " + TABLE_QUEST + " (" + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COL_USERNAME + " TEXT NOT NULL, " + COL_NIVEAU + " TEXT NOT NULL, " + COL_NBRIGOLETTES + " TEXT NOT NULL); ";
public MaBaseSQLite(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_DB);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE" + TABLE_QUEST + ";");
onCreate(db);
}
}
链接到 Logcat:http ://pastebin.com/ds9bKFcS
再次感谢..