简体   繁体   中英

How do I select all specific days from a SQLite column?

I'm trying to use SQLite to filter out specific days from a database. I have a database that saves name, ID and day. I want getMonday() to only select from COL0 where the String is Monday.

I've tried different ways to call the specific name, but I get nothing from the database. The other two methods work fine. Can someone please help me spot the error?

private static final String TAG = "DatabaseHelper";
    private static final String TABLE_NAME = "exercises";
    private static final String COL0 = "day";
    private static final String COL1 = "ID";
    private static final String COL2 = "name";


    public DatabaseHelper(Context context) {
        super(context, TABLE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String createTable = "CREATE TABLE " + TABLE_NAME + "("
                + COL1 + " INTEGER PRIMARY KEY AUTOINCREMENT," + COL0 + " TEXT," + COL2 + " TEXT" + ");";
        /*String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " + COL2 + " TEXT, " + COL0 + "TEXT)";*/
        db.execSQL(createTable);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        db.execSQL("DROP IF TABLE EXISTS " + TABLE_NAME);
        onCreate(db);
    }

    public boolean addData(String item, String day) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL2, item);
        contentValues.put(COL0, day);

        Log.d(TAG, "addData: Adding " + item + "and " + day + " to " + TABLE_NAME);

        long result = db.insert(TABLE_NAME, null, contentValues);

        //if date as inserted incorrectly it will return -1
        if (result == -1) {
            return false;
        } else {
            return true;
        }
    }

    //Get monday from database
    public Cursor getMonday(){
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "SELECT " + COL2 + " FROM " + TABLE_NAME +
                " WHERE " + COL0 + " = '" + "Monday" + "'";
        Cursor data = db.rawQuery(query, null);
        return data;
    }


    //Get all the data from database
    public Cursor getData(){
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "SELECT * FROM " + TABLE_NAME;
        Cursor data = db.rawQuery(query, null);
        return data;
    }


    //Returns only the ID that matches the name passed in
    public Cursor getItemID(String name){
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "SELECT " + COL1 + " FROM " + TABLE_NAME +
                " WHERE " + COL2 + " = '" + name + "'";
        Cursor data = db.rawQuery(query, null);
        return data;
    }

}


btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final PopupMenu popupMenu = new PopupMenu(getActivity(), btnAdd);
                popupMenu.getMenuInflater().inflate(R.menu.menu_week, popupMenu.getMenu());

                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem weekDay) {

                        /*exerciseName = bundle.getString("chestExerciseNames");*/
                        weekDays = weekDay.getTitle().toString();
                        newEntry = bundle.getString("chestExerciseNames");

                        if(newEntry != null) {
                            AddData(newEntry, weekDays);
                        }

                return true;
                    }
                });

                popupMenu.show();
            }
        });

        return view;
    }

        public void AddData(String newEntry, String weekDays){
            boolean insertData = mDatabaseHelper.addData(newEntry, weekDays);

            if(insertData == true) {
                Toast.makeText(getActivity(), newEntry + " Added to " + weekDays, Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(getActivity(), "Something went wrong", Toast.LENGTH_SHORT).show();
            }
        }

 <item
        android:id="@+id/buttonMonday"
        android:title="Monday"/>
    <item
        android:id="@+id/buttonTuesday"
        android:title="Tuesday"/>
    <item
        android:id="@+id/buttonWednesday"
        android:title="Wednesday"/>
    <item
        android:id="@+id/buttonThursday"
        android:title="Thursday"/>
    <item
        android:id="@+id/buttonFriday"
        android:title="Friday"/>
    <item
        android:id="@+id/buttonSaturday"
        android:title="Saturday"/>
    <item
        android:id="@+id/buttonSunday"
        android:title="Sunday"/>

While adding the days to the database, please add all the days to constants.

Eg

public interface Constants {

 public String MONDAY = "monday";
 public String TUESDAY = "tuesday";
 public String WEDNESDAY = "wednesday";
 public String THURSDAY = "thursday";
 public String FRIDAY = "friday";
 public String SATURDAY = "satuday";
 public String SUNDAY = "sunday";
}

and

While inserting data to database add("Your name",Constants.MONDAY)

Change the getMonday() to

 public Cursor getMonday() {

    SQLiteDatabase db = this.getWritableDatabase();
    String query = "SELECT " + COL2 + " FROM " + TABLE_NAME +
            " WHERE " + COL0 + " = '" + Constants.MONDAY + "'";
    Cursor data = db.rawQuery(query, null);
    return data;
}

Hope it will work for you

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