我有SQL数据库
包含列:
ID - 名称 - 价格 - 类别 - 计数 - 颜色 - First_Litter
示例:
1 - Test1 - 10 - Test1 - 0 - Yellow - T
2 - Test2 - 12 - Test2 - 2 - Red - T
3 - Test3 - 15 - Test3 - 7 - Green - T
4 - Cast1 - 15 - Cast1 - 5 - Blue - C
我希望使用First_Litter获取此联系人 我有这个代码,通过ID获取项目项目并且工作正常
public Items getContact(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
KEY_NAME, KEY_PRICE, KEY_CATEGORY,KEY_COUNT,KEY_BGC,KEY_FL }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Items contact = new Items(Integer.parseInt(cursor.getString(0)),cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6));
// return contact
return contact;
}
我将这段代码编辑了一下,以获得First Litter的getContact
public Items getContactFL(String fl) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
KEY_NAME, KEY_PRICE, KEY_CATEGORY,KEY_COUNT,KEY_BGC,KEY_FL }, KEY_FL + "=?",
new String[] { fl }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Items contact = new Items(Integer.parseInt(cursor.getString(0)),cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6));
// return contact
return contact;
}
但它不起作用,它只给我1个项目
我需要当我做getContactFL(" A")时,告诉我所有项目在FirstLitter列中有A
有人可以帮助我,抱歉我的英语不好
答案 0 :(得分:-1)
我不认为你是first_letter的专栏......你可以这样做......
String Query="SELECT * FROM "+TABLE_CONTACTS+" WHERE "+KEY_NAME+" LIKE '%"+f1+"%'";
Cursor Cursor=db.rawQuery(Query, null);
确定更改代码
if (cursor != null)
cursor.moveToFirst();
Items contact = new Items(Integer.parseInt(cursor.getString(0)),cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6));
// return contact
return contact;
到
Items[] contact=new Items[cursor.getCount()];
int i=0;cursor.moveToFirst();
while (!cursor.isAfterLast())
{
contact[i++] = new Items(Integer.parseInt(cursor.getString(0)),cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6));
cursor.moveToNext();
}
return contact;
答案 1 :(得分:-1)
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
KEY_NAME, KEY_PRICE, KEY_CATEGORY,KEY_COUNT,KEY_BGC,KEY_FL }, KEY_FL + "=?",
new String[] { fl }, null, null, null, null);
if (cursor.moveToFirst()) {
//put your item code here
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
// return contact
return contact;
}