4

我的应用程序需要一个数据库,但由于以下错误,它一直强制关闭。它适用于我的一项活动,但不适用于另一项活动

数据库处理程序:

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

再次感谢..

4

1 回答 1

6

从您得到的错误代码中,您可以将其追溯到这段代码:

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 int getNiveau() {
    Cursor c = bdd.query(TABLE_QUEST,new String[] { COL_NIVEAU},null,null,null,null,null);
    if(c.getCount() == 1) {
        c.moveToFirst();
        int niveau = c.getInt(c.getColumnIndexOrThrow(MaBaseSQLite.COL_NIVEAU));
        c.close();
        return niveau;
    }
    c.close();
    return -1;
}

所以基本上我正在做的是检查我是否只有一个结果列,如果我移动到第一列,然后在该位置获取整数。您必须记住的另一件事是从列名中获取列索引。如果您决定更改数据库模式,您将获得更好的可移植性。您的所有问题都可以通过位于此处的游标文档来回答:http: //developer.android.com/reference/android/database/Cursor.html

希望能回答你所有的问题。

于 2013-04-15T18:22:02.747 回答