简体   繁体   English

Sqlite 查询产生最后但一个行值而不是最后插入的行值

[英]Sqlite query is yielding last but one row value instead of last inserted row value

I have a weird sqlite problem, partly Flutter partly Android... I have a database that I create in Flutter.我有一个奇怪的 sqlite 问题,部分是 Flutter,部分是 Android ... When I insert a new row and I save it, I want to retrieve it in Android and send the values to a broadcast receiver.当我插入一个新行并保存它时,我想在 Android 中检索它并将值发送到广播接收器。 I am using this query: "SELECT * FROM " + TABLE_NAME + " ORDER BY " + COLUMN_ID + " DESC LIMIT 1";我正在使用这个查询: "SELECT * FROM " + TABLE_NAME + " ORDER BY " + COLUMN_ID + " DESC LIMIT 1"; This works great when creating the first row, but after that my broadcast receiver only ever receives the value of the last but one row, not the last row.这在创建第一行时效果很好,但之后我的广播接收器只接收最后一行的值,而不是最后一行。

This is my code on the Java side, to get the details from the sqlite database and send them to the receiver:这是我在 Java 端的代码,用于从 sqlite 数据库中获取详细信息并将其发送给接收方:

    public void getScheduleFromFlutter() {
        String setting = "";
        DatabaseHandler db = new DatabaseHandler(this);
        Cursor cursor = db.getValues();
        if (cursor.moveToFirst())
            setting = cursor.getString(cursor.getColumnIndex(COLUMN_SETTING));
        Intent intent = new Intent(this, Schedules.class);
        intent.putExtra("SETTING", setting);
        sendBroadcast(intent);
        cursor.close();
        db.close();
    }

Any ideas?有任何想法吗?

You might have some issues in the query to retrieve the last row.您在检索最后一行的查询中可能存在一些问题。

However if you re inserting values in table with Sqflite .但是,如果您使用Sqflite在表中插入值。

When inserting a value:插入值时:

  // Insert the Dog into the correct table. You might also specify the
  // `conflictAlgorithm` to use in case the same dog is inserted twice.
  //
  // In this case, replace any previous data.
  await db.insert(
    'dogs',
    dog.toMap(),
    conflictAlgorithm: ConflictAlgorithm.replace,
  );

The method insert returns a future with the ID of new column.方法 insert 返回一个带有新列 ID 的未来。

  // INSERT helper
  Future<int> insert(String table, Map<String, dynamic> values,
      {String nullColumnHack, ConflictAlgorithm conflictAlgorithm});

You can then send this ID instead or query the table with this ID to get the entry you need.然后,您可以改为发送此 ID 或使用此 ID 查询表以获取您需要的条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM