安卓 SQLite : Index -1 requested with a size of 1

标签 android database eclipse

我的应用程序需要一个数据库,但由于以下错误,它一直强制关闭。 它在我的一个 Activity 中有效,但在另一个 Activity 中无效

数据库处理程序:

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),如果没有,我检查答案然后使一些 Widget INVISIBLE 和 setNiveau(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

再次感谢..

最佳答案

从你得到的错误代码可以追溯到这段代码:

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;
}

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

希望这能回答您所有的问题。

关于安卓 SQLite : Index -1 requested with a size of 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16021910/

相关文章:

android - 如何使用 Appcelerator Titanium SDK 使用适用于 Android 的应用内结算模块检查用户进行了哪些应用内购买?

c# - 在 Unity 应用启动时阅读 Android intent 额外数据

php - 建立数据库连接 PDO 时出错

database - 最佳优化主键查询的大型数据库

eclipse - 从已部署的 Eclipse Web 应用程序设置 Tomcat <Context> 参数

android - Eclipse 3.7.2 不会创建 android 项目

java - Eclipse 代码风格未反射(reflect)在项目中

android - 微调器的文本大小

javascript - 不显示来自 Phonegap 的联系人图像

c# - 如何在未安装 SQL Server 的计算机上使用 SQL 数据库运行应用程序