1

I am facing an issue while retrieving the top record for same date from the table. Using the latest version of SQLCipher 4.4.2 & room database version is 2.2.6.

id | date_value | product_name | price

  • 1 | 2021/01/20 | ABC | 50
  • 2 | 2021/01/20 | XYZ | 60
  • 3 | 2021/01/20 | XYZ | 65
  • 4 | 2021/01/20 | ABC | 60
  • 5 | 2021/01/21 | ABC | 70

Query:

select t.*, (select t2.id from table_product t2 where t.date_value = t2.date_value order by t2.id desc limit 1) as id1 from table_product t group by t.date_value order by t.date_value

Result:

1 | 2021/01/20 | ABC | 50

returning 1st record, not the latest record of the same date.

Am I doing wrong or is there any other way to get the latest record from the multiple records of the same date?

Please help!!

Thanks

  • Test class

    import net.sqlcipher.database.SQLiteDatabase;
    
    public class AwesomeTest extends SQLCipherTest {
    
    @Override
    public boolean execute(SQLiteDatabase database) {
    
        try {
    
            // Add your scenario here
            database.execSQL("CREATE TABLE table_product (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, date_value STRING NOT NULL, date_time_stamp LONG NOT NULL, product_name STRING NOT NULL, price INTEGER NOT NULL)");
    
            database.execSQL("INSERT INTO table_product (id, date_value, date_time_stamp, product_name, price) VALUES ('1', '2021/02/27', '1614364210000', 'ABC', '10')");
    
            database.execSQL("INSERT INTO table_product (id, date_value, date_time_stamp, product_name, price) VALUES ('2', '2021/02/27', '1614364211000', 'XYZ', '11')");
    
            database.execSQL("INSERT INTO table_product (id, date_value, date_time_stamp, product_name, price) VALUES ('3', '2021/02/27', '1614364212000', 'ABC', '13')");
    
            database.execSQL("INSERT INTO table_product (id, date_value, date_time_stamp, product_name, price) VALUES ('4', '2021/02/28', '1614450600000', 'ABC', '14')");
    
            String query = "SELECT t.*, (SELECT t2.id from table_product t2 where t.date_value = t2.date_value order by t2.id desc " +
                    "limit 1) as id1 from table_product t group by t.date_value order by t.date_value";
    
            Cursor cursor = database.rawQuery(query, new String[]{});
    
            if(cursor != null) {
               Log.i("TEST", "Record count is " + cursor.getCount());
               while(cursor.moveToNext()) {
                    int index1 = cursor.getColumnIndex("price");
                    int price = cursor.getInt(index1);
                    Log.i("TEST", "product price is " + price);
               }
               cursor.close();
            }
    
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    
      @Override
      public String getName() {
          return "Awesome Test";
      }
    }
    
7
  • "Am I doing wrong" -- your date is lacking a time component. "is there any other way to get the latest record from the multiple records of the same date?" -- track the time. I do not know where your id value comes from, but I would be surprised if it were guaranteed to always be increasing in value. Commented Mar 2, 2021 at 12:21
  • Thanks, @CommonsWare for your comments. The id is auto-increment value. For query testing purposes, when I am creating or opening a database instance without SupportFactory passphrase build. The query returns the right result. I have tested on Database Inspector of Android Studio. While using the SupprtFactory passphrase for creating or opening instance of a database, the above query doesn't work. It returns 1st record, not the latest one. Commented Mar 2, 2021 at 12:52
  • "The id is auto-increment value" -- that can get reset. Do not assume that your auto-generated id column is guaranteed to be always increasing in value. Track the time as well as the date. "the above query doesn't work" -- then provide a sample project that demonstrates the issue. Or, provide a test class for the SQLCipher for Android test suite that demonstrates the issue. I assume that this post is related, but neither that post nor this SO question has any code beyond a bare SQL statement. Commented Mar 2, 2021 at 13:15
  • Ok, @CommonsWare - I have added a test class code. Yes, your assumption is right. The test result on create table & insert records queries is "OK", but on the last query, it gets "FAILED". Commented Mar 2, 2021 at 15:40
  • Given the structure of your test case, that implies an exception is being thrown. In this case, that is because AFAIK you cannot use execSQL() for a SELECT statement. You need to use rawQuery(), then look at the resulting Cursor to see if you are getting what you expect. Commented Mar 2, 2021 at 21:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.