Android:无法解析方法“query(java.lang.string)”

时间:2015-05-04 16:51:40

标签: java android sql database sqlite

嘿我试图查询我的数据库中与当前日期匹配的记录数,并且还匹配同一记录中的当前时间,但是,我得到一个“无法解析方法”查询(java.lang.string )“

我需要查询的是检查记录日期和时间是否与当前日期和时间匹配。

这是我的DatabaseHandler类文件

public class DatabaseHandler extends SQLiteOpenHelper{

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "FoodTekTrakr",
            DATABASE_TABLE = "AlarmManager",
            KEY_ID = "id",
            KEY_TITLE = "title",
            KEY_DATE = "date",
            KEY_TIME = "time",
            KEY_SONG = "song";


    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db){
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_TITLE + " TEXT," + KEY_DATE + " TEXT," + KEY_TIME + " TEXT," + KEY_SONG + " TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db,int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);
    }

    public void createAlarm(UserInputAlarmData AlarmData){
        SQLiteDatabase db = getWritableDatabase();

        ContentValues values = new ContentValues();

        values.put(KEY_TITLE, AlarmData.getTitle());
        values.put(KEY_DATE, AlarmData.getDate());
        values.put(KEY_TIME, AlarmData.getTime());
        values.put(KEY_SONG, AlarmData.getSong());


        db.insert(DATABASE_TABLE, null, values);
        db.close();
    }

    public UserInputAlarmData getAlarmData(int id){
        SQLiteDatabase db = getReadableDatabase();

        Cursor cursor = db.query(DATABASE_TABLE, new String[] {KEY_ID, KEY_TITLE, KEY_DATE, KEY_TIME, KEY_SONG}, KEY_ID
                + "=?", new String[] {String.valueOf(id) }, null, null, null );

        if (cursor != null)
            cursor.moveToFirst();

        UserInputAlarmData AlarmData = new UserInputAlarmData(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4));
        db.close();
        cursor.close();
        return AlarmData;
    }

    public void deleteAlarm(UserInputAlarmData AlarmData){
        SQLiteDatabase db = getWritableDatabase();
        db.delete(DATABASE_TABLE, KEY_ID + "=?", new String[] { String.valueOf(AlarmData.getId()) });
        db.close();
    }

    public int getAlarmDataCount() {
        SQLiteDatabase db =getReadableDatabase();
        Cursor cursor = db.rawQuery("SELECT * FROM " + DATABASE_TABLE, null);

        int count = cursor.getCount();
        db.close();
        cursor.close();

        return count;
    }

    public int updateAlarmData(UserInputAlarmData AlarmData){
        SQLiteDatabase db = getWritableDatabase();

        ContentValues values = new ContentValues();

        values.put(KEY_TITLE, AlarmData.getTitle());
        values.put(KEY_DATE, AlarmData.getDate());
        values.put(KEY_TIME, AlarmData.getTime());
        values.put(KEY_SONG, AlarmData.getSong());


        return db.update(DATABASE_TABLE, values, KEY_ID + "=?", new String[] { String.valueOf(AlarmData.getId())});
    }

    public List<UserInputAlarmData> getAllAlarmData() {
        List<UserInputAlarmData> AlarmDatas = new ArrayList<UserInputAlarmData>();
        SQLiteDatabase db = getWritableDatabase();
        Cursor cursor = db.rawQuery("SELECT * FROM " + DATABASE_TABLE, null);

        if (cursor.moveToFirst()) {
            do {
                UserInputAlarmData AlarmData = new UserInputAlarmData(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4));
                AlarmDatas.add(AlarmData);
            }
            while (cursor.moveToNext());
        }
        return  AlarmDatas;
    }

public boolean DateAndTimeChecker() {

        Date ThisDate = Calendar.getInstance().getTime();
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        String DateChecker = formatter.format(ThisDate);

        Date ThisTime = Calendar.getInstance().getTime();
        SimpleDateFormat timeformatter = new SimpleDateFormat("hh:mm");
        String TimeChecker = formatter.format(ThisTime);
        String  DatabaseTime;
        SQLiteDatabase db =getReadableDatabase();

        Cursor c = db.query("SELECT " + KEY_DATE + "WHERE"+ KEY_TIME + "=" + DateChecker  + "FROM" + DATABASE_TABLE);
        if(c != null) {
            return true;
        }
        else{
            return false;
        }
    }

}

我收到错误的方法

public boolean DateAndTimeChecker() {

        Date ThisDate = Calendar.getInstance().getTime();
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        String DateChecker = formatter.format(ThisDate);

        Date ThisTime = Calendar.getInstance().getTime();
        SimpleDateFormat timeformatter = new SimpleDateFormat("hh:mm");
        String TimeChecker = formatter.format(ThisTime);
        String  DatabaseTime;
        SQLiteDatabase db =getReadableDatabase();

        Cursor c = db.query("SELECT " + KEY_DATE + "WHERE"+ KEY_TIME + "=" + DateChecker  + "FROM" + DATABASE_TABLE);
        if(c != null) {
            return true;
        }
        else{
            return false;
        }

    }

非常感谢你提前帮助

2 个答案:

答案 0 :(得分:1)

您需要在rawQuery上添加第二个参数

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

答案 1 :(得分:0)

晚会,但我认为这里的问题是你的select语句不正确。 select语句的正确格式为:SELECT column_name FROM table_name WHERE [condition]。