0

这是我的 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(); 
4

2 回答 2

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 };

于 2012-05-16T10:34:47.057 回答
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); 
于 2012-05-16T10:35:14.987 回答