简体   繁体   中英

Android Studio project not reading correctly from SQLite database?

I've been struggling with this for days now and can't work out what's wrong. The thing is, I am not getting errors at the moment. Im just not producing the result I hoped for. I am trying to read from a specific record in my sqlite database where the data is a week ago today - I am trying to get the weather on this day, which is in my database. Here is my DB code for the method I am using:

public long homepageTB(){ 

    LocalDate now = LocalDate.now();

    LocalDate lastWeek = now.minusDays(7+now.getDayOfWeek().getValue()-1);
    LocalDate currentDate = now.minusDays(now.getDayOfWeek().getValue());

    String[] lweek = new String[1];
    lweek[0] = String.valueOf(lastWeek);
    String lastwk = String.valueOf(lastWeek);

    long rv = -1; //<<<< default return to indicate no such row
    SQLiteDatabase DB=this.getReadableDatabase();
    Cursor cursor=DB.rawQuery("select weather from dailyQuiz1 where date =?",lweek);
    if (cursor.moveToFirst()) { 
        rv = cursor.getLong(cursor.getColumnIndex("weather")); 
    }

    System.out.println(lweek[0]);
    System.out.println(rv);

    return rv;
}

At the moment, this correctly returns the data week ago in format: 2022-01-31 and returns rv: -1 (which means it can't find the record). However, the record is there in my database: 在此处输入图像描述

So realistically I don't want -1 to be returned as I would want the variable in another class I am calling it from to now hold the value of the weather, which you can see in my database is ' Rainy '. Any ideas what I've done wrong or what is a better approach? I feel it will be something so obvious but I've been trying so many things for the past few days:( Thanks for the help

With the exception that you will have an issue with the returned value being 0 if the data is found

  • ie you are getting a long from a text value when using rv = cursor.getLong(cursor.getColumnIndex("weather")) so instead of crashing when trying to convert Rainy to a long 0 is given.

What you have shown works as expected. That leaves the issue to probably be that the database does not in fact contain a row where the date column does equal the String 2022-01-31.

You need to ensure that what is in the actual database being used is as expected (your image appears to be from a tool rather than the actual database and often issues are due to differences) .

So in Android Studio run the App and get to a point where it is awaiting for user interaction. The click on App Inspection do you see the same data? eg it should look like (not all columns have been included in the example):-

在此处输入图像描述

If not then your issue is that the data is not being added as expected and that needs to be corrected.

Otherwise click on the Query Icon在此处输入图像描述

Enter and run SELECT length(date), * FROM dailyquiz1 WHERE date like '2022-01%' eg :-

在此处输入图像描述

  • if the first column returned is > 10 then there is an issue with the data that is being stored, as it contains additional data.
  • if the first column returned is < 10 then you are storing data that is shorter than it should be.
  • if no data is returned then the data stored is even further away from what it should be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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