只有一行插入SQLite数据库

时间:2015-06-24 16:06:17

标签: java android sqlite

我想在SQLDatabase表中存储String(markerID),Integer(ListView中的位置)和另一个Integer(如:0或1)。 第一个条目是perefctly存储的,在下一个调用中,我得到调试日志" Rows"使用正确的字符串和整数。 之后就会出现问题。即使我单击其他列表项并获取特定的调试日志,也没有插入新行。此外,如果我再次单击第一个列表项,则不会更新行。 这就是我调用光标的方式:

Context context = this;
   getALlEntrysListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            likeButton = (ImageView) view.findViewById(R.id.heartImage);

            DbHelper dbh = new DbHelper(context);
            Cursor cursor = dbh.getLike(dbh);
            cursor.moveToFirst();
            if (cursor.moveToFirst()) {
                do {
                    Log.d("Debug", "LongItemClick on " + position);
                    Log.d("Database", "Rows: " + cursor.getString(0) + cursor.getString(1) + cursor.getString(2));
                    if (cursor.getString(0).equals(markerID) && Integer.parseInt(cursor.getString(1)) == position && Integer.parseInt(cursor.getString(2)) == 1) {
                        dbh.updateLike(dbh, markerID, position, Integer.parseInt(cursor.getString(2)), 0);
                        Log.d("Debug", "Dislike");
                        cursor.close();
                        return true;
                    }
                    else if (cursor.getString(0).equals(markerID) && Integer.parseInt(cursor.getString(1)) == position && Integer.parseInt(cursor.getString(2)) == 0) {
                        Log.d("Debug", "Change Dislike to Like");
                        dbh.updateLike(dbh, markerID, position, Integer.parseInt(cursor.getString(2)), 1);
                        cursor.close();
                        return true;
                    }
                    else {
                        Log.d("Debug", "New Like");
                        dbh.addLike(dbh, markerID, position, 1);
                        cursor.close();
                        return true;
                    }
                } while (cursor.moveToNext());
            } else {
                Log.d("Debug", "First Entry");
                dbh.addLike(dbh, markerID, position, 1);
                cursor.close();
                return true;
            }

        }
    });

这些是addLike课程中updateLikegetLikeDbHelper的方法:

public void addLike (DbHelper dbh, String markerID, Integer position, Integer like) {
    dbase = dbh.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(LIKES_MARKERID, markerID);
    cv.put(LIKES_POSITION, position);
    cv.put(LIKES_LIKE, like);
    dbase.insert(TABLE_LIKES, null, cv);
}

public void updateLike (DbHelper dbh, String markerID, Integer position, Integer like, Integer newLike) {
    dbase = dbh.getWritableDatabase();
    String selection = LIKES_POSITION+ " LIKE ? AND "+ LIKES_MARKERID + " LIKE ? ";
    String args[] = {like.toString(), markerID};
    ContentValues values = new ContentValues();
    values.put (LIKES_LIKE, newLike);
    dbase.update(TABLE_LIKES, values, selection, args);
}

public Cursor getLike(DbHelper dbh) {
    dbase = dbh.getReadableDatabase();
    String columns[] = {LIKES_MARKERID, LIKES_POSITION, LIKES_LIKE};
    Cursor cursor = dbase.query(TABLE_LIKES, columns, null, null, null, null, null);
    return cursor;
}

创建表格:

String likes = "CREATE TABLE IF NOT EXISTS " + TABLE_LIKES + " ( "
            + LIKES_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + LIKES_MARKERID + " TEXT, "
            + LIKES_POSITION + " INTEGER, "
            + LIKES_LIKE + " INTEGER) ";
    db.execSQL(likes);

1 个答案:

答案 0 :(得分:0)

我知道这个答案很晚,但是您可以在whileif中使用if (cursor.moveToFirst())代替else if (cursor.getString(0).equals(markerID) && Integer.parseInt(cursor.getString(1)) == position && Integer.parseInt(cursor.getString(2)) == 0) DROP USER “stack_advice”;