如何从SQLite列中选择所有特定日期?

时间:2019-03-26 13:08:53

标签: java android sqlite

我正在尝试使用SQLite从数据库中过滤掉特定的日子。我有一个保存名称,ID和日期的数据库。我希望getMonday()仅从字符串为星期一的COL0中选择。

我尝试了不同的方式来调用特定名称,但是从数据库中什么也没得到。其他两种方法都可以正常工作。有人可以帮我发现错误吗?

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"/>

1 个答案:

答案 0 :(得分:0)

将日期添加到数据库时,请将所有日期添加到常量中。

例如

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";
}

将数据插入数据库 add(“您的姓名”,Constants.MONDAY)

将getMonday()更改为

 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;
}

希望它会为您服务