错误:java.lang.IllegalStateException:从第0行col 2获取字段槽在Android中失败

时间:2012-05-16 09:50:16

标签: android

这是我的MySqlLiteHelper:

public class MySQLiteHelper extends SQLiteOpenHelper {

    public static final String TABLE_CMBS = "cbs";
    public static final String CMB_ID = "_id";
    public static final String COLUMN_CMB = "cmb";
    public static final String COLUMN_INFO  = "cinfo";
    public static final String COLUMN_URL = "imageurl";


    private static final String DATABASE_NAME = "cmbsdr.db";
    private static final int DATABASE_VERSION = 1;

    // Database creation sql statement

    private static final String DATABASE_CREATE =
        "create table "+ TABLE_CMBS +" (_id integer primary key autoincrement, "
        + "cmb text, cbinfo text,"
        + "imageurl text);";


    public MySQLiteHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(DATABASE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(MySQLiteHelper.class.getName(),
                "Upgrading database from version " + oldVersion + " to "
                        + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CMBS);
        onCreate(db);
    }

}

    This is my Comments class:



public class Comment {
    private long id;
    private String cmb;
    private String cinfo;
    private String imageurl;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getCmb() {
        return cmb;
    }

    public void setCmb(String cmb) {
        this.cmb = cmb;
    }
    public String getCInfo(){
            return cinfo;
    }
    public void setCInfo(String cinfo){
            this.cinfo = cinfo;
    }
    public void setImageUrl(String imageurl){
            this.imageurl = imageurl;
    }
    public String getImageUrl(){
            return imageurl;
    }



}

这是我的CommentsDataSource类:

public class CommentsDataSource {

    // Database fields
    private SQLiteDatabase database;
    private MySQLiteHelper dbHelper;
    private String[] allColumns = { MySQLiteHelper.CMB_ID,
            MySQLiteHelper.COLUMN_CMB };

    public CommentsDataSource(Context context) {
        dbHelper = new MySQLiteHelper(context);
    }

    public void open() throws SQLException {
        database = dbHelper.getWritableDatabase();
    }

    public void close() {
        dbHelper.close();
    }

    public Comment createComment(String cmb, String cinfo, String imageurl) {
        ContentValues values = new ContentValues();
        values.put(MySQLiteHelper.COLUMN_CMB, cmb);
        values.put(MySQLiteHelper.COLUMN_INFO, cinfo);
        values.put(MySQLiteHelper.COLUMN_URL, imageurl);

        long insertId = database.insert(MySQLiteHelper.TABLE_CMBS, null,
                values);
        Cursor cursor = database.query(MySQLiteHelper.TABLE_CMBS,
                allColumns, MySQLiteHelper.CMB_ID + " = " + insertId, null,
                null, null, null);
        cursor.moveToFirst();
        Comment newComment = cursorToComment(cursor);
        cursor.close();
        return newComment;
    }

    public void deleteComment(Comment comment) {
        long id = comment.getId();
        System.out.println("Comment deleted with id: " + id);
        database.delete(MySQLiteHelper.TABLE_CMBS, MySQLiteHelper.CMB_ID
                + " = " + id, null);
    }

    public List<Comment> getAllComments() {
        List<Comment> comments = new ArrayList<Comment>();

        Cursor cursor = database.query(MySQLiteHelper.TABLE_CMB,
                allColumns, null, null, null, null,null);

        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            Comment comment = cursorToComment(cursor);
            comments.add(comment);

            cursor.moveToNext();
        }
        // Make sure to close the cursor
        cursor.close();
        return comments;
    }

    private Comment cursorToComment(Cursor cursor) {
        Comment comment = new Comment();
        comment.setId(cursor.getLong(0));
        comment.setCmb(cursor.getString(1));
        comment.setCInfo(cursor.getString(2));
        comment.setImageUrl(cursor.getString(3));
        return comment;
    }
}

我收到错误:

05-16 15:07:25.273: E/AndroidRuntime(26017): Caused by: java.lang.IllegalStateException: get field slot from row 0 col 2 failed

当我这样做时:

dataSource = new CommentsDataSource(this);
        dataSource.open();

        List<Comment> cmbs = dataSource.getAllComments(); 

2 个答案:

答案 0 :(得分:1)

你的身体投影字符串对你的情况是错误的。你只是在查询

private String[] allColumns = { MySQLiteHelper.CMB_ID, MySQLiteHelper.COLUMN_CMB };

在allColumns字符串数组中添加其他两个列名。 即MySQLiteHelper.COLUMN_INFO and MySQLiteHelper.COLUMN_URL

正确的方式:     private String[] allColumns = { MySQLiteHelper.CMB_ID, MySQLiteHelper.COLUMN_CMB, MySQLiteHelper.COLUMN_INFO, MySQLiteHelper.COLUMN_URL };

答案 1 :(得分:0)

将urn代码中的行更改为给定的

Cursor cursor = database.query(MySQLiteHelper.TABLE_CMB,
            allColumns, null, null, null, null,null);
方法

中的

getAllComments() bcz你的表名是MySQLiteHelper中的另一个,由 TABLE_CMBS

给出
 Cursor cursor = database.query(MySQLiteHelper.TABLE_CMBS,
            allColumns, null, null, null, null,null);