0

当我运行以下代码时,我会在 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();     
    }

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

4

1 回答 1

0

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

phone所以从这个结果集中读取字段是没有意义的......

尝试更换

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

经过

Cursor c1 = sb.rawQuery( "select * from blac", null);
于 2013-01-17T10:00:06.853 回答