使用SQLite数据库时出现IllegalStateExcepton

时间:2013-01-17 09:55:42

标签: android android-sqlite

当我运行以下代码时,我在Toast消息中显示此异常:Java.lang.IllegalStateExcepton: Bad request for field slot 0,-1. numRows = 1, numColumns = 1

SQLiteDatabase sb;
    String g="o";
    try {
        sb=SQLiteDatabase.openDatabase("data/data/com.example.datatestx/ma", null, SQLiteDatabase.CREATE_IF_NECESSARY);

        sb.execSQL("create table if not exists blac  (" + " phone text  , " + " ftime  text, " + " ttime text );  "    );

        sb.execSQL( "insert into blac(phone, ftime,ttime) values ('01711233434', '333333','33333' );"  );
        sb.execSQL( "insert into blac(phone, ftime,ttime) values ('01761233433', '777' ,'77777');"  );
        sb.execSQL( "insert into blac(phone, ftime,ttime) values ('01712333323', '999','33433' );"  );
        Cursor c1 = sb.rawQuery( "select count(*) as Total from blac", null);
    if(c1.getCount()>0){

    c1.moveToFirst();
    do {
        g=g+c1.getString(c1.getColumnIndex("phone"));

    }while(c1.moveToNext());
    }  c1.close();
        sb.close();

         Toast.makeText(getApplicationContext(), "Success!"+g, Toast.LENGTH_LONG).show();   
    }
    catch (Exception e)
    {

    Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();     
    }

我不知道为什么这不起作用。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

SELECT查询中,您要求COUNT(*) as Total,这意味着您的结果集将包含一个名为' Total&#39}的字段。其中包含总行数。

因此从这个结果集中读取phone字段没有意义......

尝试替换

Cursor c1 = sb.rawQuery( "select count(*) as Total from blac", null);

通过

Cursor c1 = sb.rawQuery( "select * from blac", null);