IllegalStateException:从第0行col 3获取字段槽

时间:2011-09-30 12:19:30

标签: 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 && !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("") && 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'), " );
        }

    }

LOG:

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 个答案:

答案 0 :(得分:1)

数据库查询语句中缺少第四列。您只声明“名称”,“费率”,“吃饭时间”,而不是“图像”。