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