0

How to pass the value of setOnDateChangeListener to DATABASE_NAME?

I formatted my dbhelper like this

public static String getDateTime() {
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyyMMdd", Locale.getDefault());
        Date date = new Date();
        return dateFormat.format(date);
    }

    // Database Name
    private static final String DATABASE_NAME = getDateTime();

While I formatted my calendar setOnDateChangeListener just like how I formatted my DATABASE_NAME.

Below is my repo class which gets the sum(Fats) from the date today.
What I want is to get the sum(Fats) from the date I clicked on the calendar.

public double totalFatB(){

        SQLiteDatabase db = dbHelper.getReadableDatabase();

        String query = "SELECT SUM(Fat) FROM " +breakfast.TABLE;

        Cursor c = db.rawQuery(query, null);

        c.moveToFirst();
        double i=c.getDouble(0);

        return i;

    }

Here is my onDateChangeListener

CalendarView calendarView=(CalendarView) findViewById(R.id.calendarView);
        calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {

            @Override
            public void onSelectedDayChange(CalendarView view, int year, int month,
                                            int dayOfMonth) {
                month = month+1;
                if ((month == 1) || (month == 2)|| (month == 3)|| (month == 4)|| (month == 5)|| (month == 6)|| (month == 7)|| (month == 8)|| (month == 9) )
                {
                    String m= "0"+month;
                    if ((dayOfMonth == 1) || (dayOfMonth == 2)|| (dayOfMonth == 3)|| (dayOfMonth == 4)|| (dayOfMonth == 5)|| (dayOfMonth == 6)|| (dayOfMonth == 7)|| (dayOfMonth == 8)|| (dayOfMonth == 9) ) {
                        String d = "0" + dayOfMonth;
                         date= year +""+ m +""+ d;
                        Toast.makeText(getApplicationContext(),date, Toast.LENGTH_LONG).show();
                        /*Toast.makeText(getApplicationContext(), "" + year + m + d, Toast.LENGTH_LONG).show();*/
                    }
                        else
                        {
                             date= year +""+ m +""+ dayOfMonth;
                            Toast.makeText(getApplicationContext(),date, Toast.LENGTH_LONG).show();
                            /*Toast.makeText(getApplicationContext(), "" + year + m + dayOfMonth, Toast.LENGTH_LONG).show();*/
                        }
                }
                else if ((dayOfMonth == 1) || (dayOfMonth == 2)|| (dayOfMonth == 3)|| (dayOfMonth == 4)|| (dayOfMonth == 5)|| (dayOfMonth == 6)|| (dayOfMonth == 7)|| (dayOfMonth == 8)|| (dayOfMonth == 9) ) {
                    String d = "0" + dayOfMonth;
                     date= year +""+ month +""+ d;
                    Toast.makeText(getApplicationContext(),date, Toast.LENGTH_LONG).show();
                    /*Toast.makeText(getApplicationContext(), "" + year + month + d, Toast.LENGTH_LONG).show();*/
                }
                    else
                {
                    date= year +""+ month +""+ dayOfMonth;
                    Toast.makeText(getApplicationContext(),date, Toast.LENGTH_LONG).show();
                    /*Toast.makeText(getApplicationContext(), "" + year + month + dayOfMonth, Toast.LENGTH_LONG).show();*/
                }
            }
        });
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • You only need one database total, not one per day. Name it "meals". You can query using the date later. – OneCricketeer Feb 12 '17 at 15:12
  • But if you want help, show the `setOnDateChangeListener` method. See about a [mcve] – OneCricketeer Feb 12 '17 at 15:12
  • Hello sir cricket, Okay, I will make one database for that, but how can i pass the value of my **onSelectedDayChange** to be the reference of what date will my dbhelper get the data. By the way sir, ive edited my post and included my onSelectedDayChange. – John Mark Delos Reyes Feb 12 '17 at 15:19

1 Answers1

1

How to pass the value of setOnDateChangeListener to DATABASE_NAME?

Simply put - you shouldn't do this. If you did, that means you have one database file per day, and that is causing overhead in your application data.

Besides that, a SQLiteOpenHelper class only looks at the Database name string when it is constructed, so if you had multiple names, you be recreating that object for each date and that wears out the memory/battery of your device.

In theory, it's possible...

@Override
public void onSelectedDayChange(CalendarView view, int year, int month,
                                        int dayOfMonth) {
    // Build a Calendar
    Calendar c = Calendar.getInstance();
    // c.set(); // TODO: Set year, month, dayOfMonth 
    ...
    SimpleDateFormat dateFormat = new SimpleDateFormat(
            "yyyyMMdd", Locale.getDefault());

    // Get a database by name
    String DATABASE_NAME = dateFormat.format(c.getTime());
    SQLiteDatabase db = SQLiteDatabase.openDatabase(DATABASE_NAME , null, SQLiteDatabase.OPEN_READONLY);

    // TODO: Query 'db'

But I would not recommend it.


Instead, you can use one master database and store a date column (as a TEXT or INT) within your SQLlite table

Then, you can do public double totalFatB(Date date), or pass (year, month, day) directly and build a WHERE statement to filter for a particular day from the setOnDateChangeListener method.

Also, please use SimpleDateFormat again in that method to correctly build a date string, as shown. If statements are very verbose.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Ah, you mean sir that i need to make a only one database_name? I cant do that sir, because I have so many tables and columns inside it i think inside 1 database_name has 10tables and inside that tables has 15columns. Please teach me sir how to fetch the database_name using **setOnDateChangeListener** You are my only chance to do this sir. – John Mark Delos Reyes Feb 12 '17 at 15:32
  • If your problem is column count, then you're likely duplicating data and you need a better database design, but that's too much broad to answer here – OneCricketeer Feb 12 '17 at 15:35
  • Sorry sir, my bad. Im newbie sir in terms of sqlite and android studio, But is my idea possible sir? Because I think sir it will take about 3 days for me to reformat my codes. If my idea is possible, Please teach me sir. – John Mark Delos Reyes Feb 12 '17 at 15:44
  • Im not particular with column count sir. But my problem is I need to pass the **setOnDateChangeListener** to be the reference what DATABASE_NAME will my app will fetch. – John Mark Delos Reyes Feb 12 '17 at 15:46
  • 1) No need to call me sir 2) I don't think your idea is possible (at least not by using a SQLiteOpenHelper class) because that has to be created **before** your date selection using some existing name 3) You have the opportunity to learn about correct database design, so however long that takes is worth it – OneCricketeer Feb 12 '17 at 15:52
  • Please teach me how proper database designing. I really need your help. – John Mark Delos Reyes Feb 12 '17 at 16:33
  • How to make a database effectively. – John Mark Delos Reyes Feb 12 '17 at 16:34
  • Please help me to conceptualize the best database structure for my application. – John Mark Delos Reyes Feb 12 '17 at 16:49
  • I've already given my suggestion. But 1) I'm not in charge of your application, it's your decision to follow my advice or not 2) that's too broad to answer here anyway 3) Just make each table that needs a date have one column added, and that's it. Your queries then need updated, of course. That doesn't take 3 days. – OneCricketeer Feb 12 '17 at 18:54
  • Ive already reformated my codes, my question for now sir is how can i pass the value of **setOnDateChangeListener ** ? i dont know how to explain this, But how to reformat TotalFatB like what i posted to get the **setOnDateChangeListener **? – John Mark Delos Reyes Feb 13 '17 at 05:46
  • Have you done `public double totalFatB(Date date)` like I explained? Can you get `dbHelper` inside of `setOnDateChangeListener`, if so, then that's all you need. If you want help with just that, please create a new post with what you've tried related to those steps – OneCricketeer Feb 13 '17 at 06:04
  • Im banned from posting questions again, So i make a new account. By the way, I tried to do as what you told but i have problem with the date format, The full question is on here. http://stackoverflow.com/questions/42204697/how-to-pass-onselecteddaychange-to-act-as-reference-for-dbhelper – John Mark Delos Reyes Feb 13 '17 at 13:28
  • You can read over the help center about posting quality questions, not ones with really obvious solutions if you'd take the time to think about the tools you're using. Like the one you just linked to. 1) A SQL tutorial would do you good. 2) Maybe some additional understanding of Java? – OneCricketeer Feb 13 '17 at 13:32
  • Bye the way, I edited my post on that link because I tried something that seems to be easy but it show error. – John Mark Delos Reyes Feb 13 '17 at 13:34
  • Well, yeah. You toString-d a `allfood` object. You added the whole object to a String, not a date. Related: http://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4 – OneCricketeer Feb 13 '17 at 13:55