SQLite Android索引越界问题

时间:2014-05-22 22:33:47

标签: java android sql sqlite adk

我试图在SQLite数据库中添加帖子并在之后检索它。 我似乎正在正确地附加帖子,但是在检索时我得到了这个错误。

05-23 00:26:03.088: E/AndroidRuntime(30715): FATAL EXCEPTION: main
05-23 00:26:03.088: E/AndroidRuntime(30715): Process: com.deparesmatteo.www, PID: 30715
05-23 00:26:03.088: E/AndroidRuntime(30715): android.database.CursorIndexOutOfBoundsException:            Index -1 requested, with a size of 1
05-23 00:26:03.088: E/AndroidRuntime(30715):    at     android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
05-23 00:26:03.088: E/AndroidRuntime(30715):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)

这些是我使用的方法:

public static PostEntity createPost(final PostEntity entity)
{
    ContentValues values = new ContentValues();
    values.put(MySQLiteOpenHelper._TITLE, entity.getPostTitle());
    values.put(MySQLiteOpenHelper._DESCRIPTION, entity.getPostDesc());
    values.put(MySQLiteOpenHelper._CATEGORY, entity.getPostCategory());
    values.put(MySQLiteOpenHelper._IMAGEURL, entity.getPostImageURL());
    long postId=database.insert(MySQLiteOpenHelper.TABLE_NAME, null, values);
    Cursor cursor =database.query(MySQLiteOpenHelper.TABLE_NAME, ALL_COLUMNS, MySQLiteOpenHelper._ID+" = '"+(postId)+"'", null, null, null, null);
    return getEntityFromCursor(cursor);
}
private static PostEntity getEntityFromCursor(final Cursor cursor)
{
    PostEntity entity = new PostEntity(cursor.getInt(0), cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4));
    return entity;

提前感谢您的帮助!

修改


private final static String[] ALL_COLUMNS = {MySQLiteOpenHelper._ID.toString(),   MySQLiteOpenHelper._TITLE, MySQLiteOpenHelper._DESCRIPTION, MySQLiteOpenHelper._CATEGORY,  MySQLiteOpenHelper._IMAGEURL}; 

这些是所有列声明

MySQLiteOpenHelper声明

public static final String TABLE_NAME = "post";
private static final String DATABASE_NAME="posts.db";
private static final int DATABASE_VERSION = 1;

public static final String _ID = "_id";
public static final String _TITLE = "title";
public static final String _DESCRIPTION = "description";
public static final String _CATEGORY = "category";
public static final String _IMAGEURL = "image";


private static final String DB_CREATE = "CREATE TABLE " + TABLE_NAME + 
                                        "(" + _ID + " integer primary key, " +
                                        _TITLE + " text not null, "
                                        + _DESCRIPTION + " text not null, "
                                        + _CATEGORY + " text not null, "
                                        + _IMAGEURL + " text not null "+");";

1 个答案:

答案 0 :(得分:1)

您忘记了正确访问光标。将您的getEntityFromCursor()更改为:

private static PostEntity getEntityFromCursor(final Cursor cursor) {
    PostEntity entity = null;
    if( cursor != null && cursor.moveToFirst() ) {
        entity = new PostEntity( cursor.getInt(0),
                                 cursor.getString(1),
                                 cursor.getString(2),
                                 cursor.getString(3),
                                 cursor.getString(4) );
    }
    return entity;
}