Исключение IllegalStateException: получить слот поля из строки 0 col 3

#android

#Android

Вопрос:

Мое приложение всегда вылетает в начале, ошибка в журнале

Мой код:

     private void onCreateDBAndDBTabled() {
        myDB = this.openOrCreateDatabase(MY_DB_NAME, MODE_PRIVATE, null);
        //myDB.execSQL("DROP TABLE "   MY_DB_TABLE);
        myDB.execSQL("CREATE TABLE IF NOT EXISTS "   MY_DB_TABLE
                  " (_id integer primary key autoincrement, name varchar(100), rate integer(1), eattime varchar(100),image BLOB)"
                 ";");
    ArrayList<Pizza> list = new ArrayList<Pizza>();
Cursor cursor = this.myDB.query(MY_DB_TABLE, new String[] { "name", "rate","eattime" },null,null,null,null,null,null);
      if (cursor.moveToFirst()) {
         do {
             Log.e("XXX", "Courser Enter: "   cursor.getString(0));
             Pizza pizza = new Pizza();
             pizza.title= cursor.getString(0);
             pizza.rate = cursor.getInt(1);
             pizza.date = cursor.getString(2);

             ByteArrayInputStream inputStream = new ByteArrayInputStream(cursor.getBlob(3));
             pizza.picture = BitmapFactory.decodeStream(inputStream);
            list.add(pizza); 
            } 
         while (cursor.moveToNext());
      }
      if (cursor != null amp;amp; !cursor.isClosed()) {
         cursor.close();
      }
      Log.e("XXX", "Count:"   list.size());
      //Log.e("XXX", "Item[0] -->"    list.toArray()[0].toString());
      CustomAdapter aa = new CustomAdapter(this, R.layout.customlistitem,list);
     Log.e("XXX", String.valueOf(aa.getCount()));
      lv.setAdapter(aa);
      aa.notifyDataSetChanged();
    }
  

AddPizza:

 private void addData() {
        myDB = this.openOrCreateDatabase(MY_DB_NAME, MODE_PRIVATE, null);
        //myDB.execSQL("DROP TABLE "   MY_DB_TABLE);
        myDB.execSQL("CREATE TABLE IF NOT EXISTS "   MY_DB_TABLE
                      " (_id integer primary key autoincrement, name varchar(100), rate integer(1), eattime varchar(100),image BLOB)"
                     ";");
        if(!name.getText().equals("") amp;amp; rating.getRating()!=0.0)
        {
            Log.e("XXX", "Enter_Insert");
            Calendar cal = Calendar.getInstance(); 
            DateFormat formatter = new SimpleDateFormat(); 
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            Bitmap bt = ((BitmapDrawable)iv.getDrawable()).getBitmap();
            bt.compress(Bitmap.CompressFormat.PNG, 100, out);
            ContentValues cv = new ContentValues();
            cv.put("image", out.toByteArray());            
            cv.put("name", name.getText().toString());
            cv.put("eattime", formatter.format(cal.getTime()));
            cv.put("rate", rating.getRating());
            myDB.insert(MY_DB_TABLE, null, cv);

            //myDB.execSQL("INSERT INTO "  MY_DB_TABLE   "(name,rate,eattime,image) VALUES  "      ", "    " , datetime('now', 'localtime'), " );
        }

    }
  

ЖУРНАЛ:

 09-30 12:16:26.755: ERROR/AndroidRuntime(1149): Caused by: java.lang.IllegalStateException: get field slot from row 0 col 3 failed
09-30 12:16:26.755: ERROR/AndroidRuntime(1149):     at android.database.CursorWindow.getBlob_native(Native Method)
09-30 12:16:26.755: ERROR/AndroidRuntime(1149):     at android.database.CursorWindow.getBlob(CursorWindow.java:242)
09-30 12:16:26.755: ERROR/AndroidRuntime(1149):     at android.database.AbstractWindowedCursor.getBlob(AbstractWindowedCursor.java:35)
09-30 12:16:26.755: ERROR/AndroidRuntime(1149):     at com.korn.pizzacounter.Main.onCreateDBAndDBTabled(Main.java:70)
09-30 12:16:26.755: ERROR/AndroidRuntime(1149):     at com.korn.pizzacounter.Main.onCreate(Main.java:40)
09-30 12:16:26.755: ERROR/AndroidRuntime(1149):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-30 12:16:26.755: ERROR/AndroidRuntime(1149):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
  

Ответ №1:

В инструкции запроса БД отсутствует четвертый столбец. Вы объявляете только «имя», «скорость», «время приема пищи», но не «изображение».