0

我正在尝试获取具有特定日期的所有行,但它什么也没打印。得到所有的作品,但具体的des没有。似乎问题出在这一行: String selectQuery="SELECT * FROM " + TABLE_CONTACTS+ " WHERE "+KEY_DATE+"="+Date ;

我真的很乐意帮助。

private static final String KEY_ID = "id";
    private static final String KEY_PNAME = "pName";
    private static final String KEY_QUANTITY = "quantity";
    private static final String KEY_DATE = "date";

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
// Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_PNAME + " TEXT,"
                + KEY_QUANTITY + " INTEGER," + KEY_DATE + " TEXT" + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }
// Getting Specific Contacts
    public List<Contact> getSpecificContacts(String Date) {
        List<Contact> contactList = new ArrayList<Contact>();
        // Select All Query
       // String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;

        String selectQuery="SELECT  * FROM " + TABLE_CONTACTS+ " WHERE "+KEY_DATE+"="+Date ;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Contact contact = new Contact();
                contact.set_id(Integer.parseInt(cursor.getString(0)));
                contact.set_pName(cursor.getString(1));
                contact.set_quantity(Integer.parseInt(cursor.getString(2)));
                contact.set_date(cursor.getString(3));
                // Adding contact to list
                contactList.add(contact);
            } while (cursor.moveToNext());
        }

        // return contact list
        return contactList;
    }

    // Getting All Contacts
    public List<Contact> getAllContacts() {
        List<Contact> contactList = new ArrayList<Contact>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Contact contact = new Contact();
                contact.set_id(Integer.parseInt(cursor.getString(0)));
                contact.set_pName(cursor.getString(1));
                contact.set_quantity(Integer.parseInt(cursor.getString(2)));
                contact.set_date(cursor.getString(3));
                // Adding contact to list
                contactList.add(contact);
            } while (cursor.moveToNext());
        }

        // return contact list
        return contactList;
    }
4

1 回答 1

0
String selectQuery="SELECT  * FROM " + TABLE_CONTACTS+ " WHERE "+KEY_DATE+"="+Date ;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

而不是使用上面的查询使用这个

String [] columns = new String []{use columns here eg _id, Name,...};
Cursor cursor = db.query(TABLE_CONTACTS, columns, KEY_DATE+ "=?", 
new String[] { String.valueOf(Date) }, null, null, null,null);
于 2014-03-19T17:01:53.080 回答