【问题标题】:Only one row inserted to SQLite Database只有一行插入到 SQLite 数据库
【发布时间】:2020-05-11 05:36:23
【问题描述】:

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

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

        }
    });

这些是 DbHelper 类中的 addLikeupdateLikegetLike 方法:

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

【问题讨论】:

  • 是否有任何字段具有唯一索引?
  • 是的,该表还有一个用于 id 的自动增量字段。我将表格的创建添加到我的问题中
  • 怎么知道没有插入数据?会发生什么?
  • 我想是因为两个原因: 1.“日志”“行”:只给出第一行,即使我点击其他项目并得到日志“新喜欢”。 2. 如果我再次单击该项目,它应该将类似整数更新为 0,但它会再次进入“新点赞”,因此没有添加新行。
  • 如果我再次点击第一个项目(存储的..)它会转到“不喜欢”但不会更新条目:(

标签: java android sqlite


【解决方案1】:

我知道这个答案已经很晚了,但是您可以在 if (cursor.moveToFirst())else if (cursor.getString(0).equals(markerID) &amp;&amp; Integer.parseInt(cursor.getString(1)) == position &amp;&amp; Integer.parseInt(cursor.getString(2)) == 0)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-13
    相关资源
    最近更新 更多