在Android上,主键的getColumnIndex在sqlite上返回值-1

时间:2015-05-11 11:41:39

标签: android mysql sqlite

我在android上获取整数主键的列索引时遇到问题。它返回值-1。我需要这个id来对表进行操作。这是我的代码:

第一个是我的SQLiteHelper类的代码,我用来创建表和存储列名。

public class SQLiteHelper extends SQLiteOpenHelper {

    //Nombre del archivo que contiene la base de datos
    public static final String DATABASE_NAME = "apunto.db";

    public static final String TABLE_BOOKS = "books";

    public static final String ITEM_ID = "item_id";
    public static final String CATEGORY = "category";
    public static final String LIKE = "user_like";
    public static final String RECOMMENDATION = "recommendation";
    public static final String TITLE = "title";
    public static final String DESCRIPTION = "description";
    public static final String PHOTO = "photo";
    public static final String GENRE = "genre";

    private static final int DATABASE_VERSION = 1;

    private String DATABASE_CREATE_BOOKS = "CREATE TABLE "
            + TABLE_BOOKS + "( " + ITEM_ID + " INTEGER PRIMARY KEY , " + TITLE + " STRING , "
             + GENRE + " STRING , " + DESCRIPTION + " STRING , " + RECOMMENDATION + " STRING , "
            + PHOTO + " STRING , "+ LIKE + " INTEGER "
            + ")";

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(DATABASE_CREATE_BOOKS);

    }
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_BOOKS);
        onCreate(db);
    }

    @Override
    public synchronized void close() {
        super.close();
    }
}

这是我的DatabaseOperations类的代码,从操作到数据库。在这个类中,我插入了一些使用database.insert(tables[0] , null , values);从输入中获得的值,并返回插入行的id。但是当我尝试读取插入的项目时(这是我执行book.setId(cursor.getInt(cursor.getColumnIndex(SQLiteHelper.ITEM_ID)))时出现错误的地方,。我得到一个游标错误"E/CursorWindow﹕ Failed to read row 1, column -1 from a CursorWindow which has 2 rows, 6 columns"。它说te游标只有6列,它应该有7个(包括SQLiteHelper.ITEM_ID)

public class DatabaseOperations {
    private SQLiteHelper dbHelper;
    private SQLiteDatabase database;
    private Boolean idFound;

public DatabaseOperations(Context context) {

        dbHelper = new SQLiteHelper(context);

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

    //---------------DB MESSAGE OPERATIONS--------------------

    public int saveBook(Book bookToSave){
        ContentValues values = new ContentValues();
        values.put(bookFields[0], bookToSave.getTitle());
        values.put(bookFields[1], bookToSave.getGenre());
        values.put(bookFields[2], bookToSave.getDescription());
        values.put(bookFields[3], bookToSave.getRecommendation());
        values.put(bookFields[4], bookToSave.getPhoto());
        values.put(bookFields[5], bookToSave.getLike());

        int itemId = (int) database.insert(tables[0] , null , values);

        ContentValues newValues = new ContentValues();
        newValues.put(SQLiteHelper.USER_ID,0);
        newValues.put(SQLiteHelper.CATEGORY, 0);
        newValues.put(SQLiteHelper.ITEM_ID, itemId);

        int insertionId = (int) database.insert(SQLiteHelper.TABLE_RECOMMENDATIONS, null, newValues);
        Log.i("myApp","itemId " + itemId);

        return insertionId;
    }

public ArrayList<Object> getAllItemsFromCategory(int category){

        ArrayList<Object> itemsArrayRead = new ArrayList<>();
        try{

            Cursor cursor = database.query(tables[category],arrayFields.get(category),null,null,null,null,null);
            cursor.moveToLast();

            while (!cursor.isBeforeFirst()){
                if(category==0){
                    Book bookAux = cursorToBook(cursor);
                    itemsArrayRead.add(bookAux);
                }
                cursor.moveToPrevious();
            }
            cursor.close();
        }
        catch (Exception e){

        }

        return itemsArrayRead;
    }

private Book cursorToBook(Cursor cursor){

        Book book = new Book();
        book.setId(cursor.getInt(cursor.getColumnIndex(SQLiteHelper.ITEM_ID)));
        Log.i("myApp", "id " + cursor.getColumnIndex(SQLiteHelper.ITEM_ID));
        book.setTitle(cursor.getString(cursor.getColumnIndex(SQLiteHelper.TITLE)));
        Log.i("myApp", "title " + cursor.getColumnIndex(SQLiteHelper.TITLE));
        book.setGenre(cursor.getString(cursor.getColumnIndex(SQLiteHelper.GENRE)));
        Log.i("myApp", "genre " + cursor.getColumnIndex(SQLiteHelper.GENRE));
        book.setDescription(cursor.getString(cursor.getColumnIndex(SQLiteHelper.DESCRIPTION)));
        Log.i("myApp", "description " + cursor.getColumnIndex(SQLiteHelper.DESCRIPTION));
        book.setRecommendation(cursor.getString(cursor.getColumnIndex(SQLiteHelper.RECOMMENDATION)));
        Log.i("myApp", "recommendation " + cursor.getColumnIndex(SQLiteHelper.RECOMMENDATION));
        book.setPhoto(cursor.getString(cursor.getColumnIndex(SQLiteHelper.PHOTO)));
        Log.i("myApp", "photo " + cursor.getColumnIndex(SQLiteHelper.PHOTO));
        book.setLike(cursor.getInt(cursor.getColumnIndex(SQLiteHelper.LIKE)));
        Log.i("myApp", "like " + cursor.getColumnIndex(SQLiteHelper.LIKE));

        Log.i("myApp", "6to" + cursor.getInt(6));
        Log.i("myAPp" , "6to" + cursor.getString(6));

        return book;
    }

    public long getCategoryCount(int category) {
        String sql = "SELECT COUNT(*) FROM " + tables[category];
        SQLiteStatement statement = database.compileStatement(sql);
        long count = statement.simpleQueryForLong();
        return count;
    }

1 个答案:

答案 0 :(得分:0)

是igot,item作为数组字段的一部分,因为当我借口时,数据库inselt,手机空值,返回插入元素的id。因为我的列项是集成主键然后我把书对象id,在bt内,当我想要开始我需要sqlete,那就是我堆叠Manelo的地方