如何使用SQlitedatabase解决Java.lang.StringOutOfBound异常

时间:2013-01-17 12:39:45

标签: android android-sqlite

我运行以下代码并获得 Java.lang.StringOutOfBound Exception

   SQLiteDatabase sb;
     String g="o";
    String []gt={"0"};
     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 ('01741297133', '333333','33333' );"  );
        sb.execSQL( "insert into blac(phone, ftime,ttime) values ('01761233433', '777' ,'77777');"  );
        sb.execSQL( "insert into blac(phone, ftime,ttime) values ('01712333333', '999','33433' );"  );
        //Cursor c1 = sb.rawQuery( "select * from blac where phone=?", gt);
        String gb="33"; 
        String gh = "SELECT * FROM blac where( phone like '%"+gb+" ')"+"ORDER BY phone";
        Cursor c1 = sb.rawQuery( gh,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();     
    }

但是在运行代码之后......值 g 没有改变 请有人在这里帮助我。

1 个答案:

答案 0 :(得分:0)

你想从“手机”栏中获取一个字符串,但据我所知,你没有创建这样一个列:

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

如果您的列有String常量,那么您以错误的方式编写了您的execSQL:

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

你必须这样写:

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