我有一个由android中的sqlite创建的数据库,我希望能够只列出特定组中的联系人。数据库助手代码是这样的;
public class DataBaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "contact.db";
public static final int DATABASE_VERSION = 1;
Context context;
public DataBaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(Table.Contact.CREATE_CONTACT_DETAILS_TABLES);
db.execSQL(Table.Country.CREATE_COUNTRY_TABLES);
db.execSQL(Table.State.CREATE_STATE_TABLES);
db.execSQL(Table.City.CREATE_CITY_TABLES);
db.execSQL(Table.Category.CREATE_CATEGORY_TABLES);
createDefaults(db);
createCatgeoryType(db);
}
private void createDefaults(SQLiteDatabase db) {
String[] country = context.getResources().getStringArray(R.array.country_arrays);
for (String aCountry : country) {
ContentValues contentValues = new ContentValues();
contentValues.put(Table.Country.COUNTRY_NAME, aCountry);
db.insert(Table.Country.TABLE_NAME, null, contentValues);
}
}
private void createCatgeoryType(SQLiteDatabase db) {
String[] category = context.getResources().getStringArray(R.array.category_arrays);
for (String aCategory : category) {
ContentValues contentValues = new ContentValues();
contentValues.put(Table.Category.CATEGORY_TYPE, aCategory);
db.insert(Table.Category.TABLE_NAME, null, contentValues);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public long insert(Context context, String tableName, ContentValues contentValues) {
DataBaseHelper databaseHelper = null;
SQLiteDatabase sqLiteDatabase = null;
long row_id = -1;
try {
databaseHelper = new DataBaseHelper(context);
sqLiteDatabase = databaseHelper.getWritableDatabase();
if (sqLiteDatabase != null) {
row_id = sqLiteDatabase.insertWithOnConflict(tableName, null, contentValues, SQLiteDatabase.CONFLICT_ABORT);
}
} catch (Exception e) {
e.printStackTrace();
if (databaseHelper != null)
databaseHelper.close();
if (sqLiteDatabase != null)
sqLiteDatabase.close();
} finally {
if (databaseHelper != null)
databaseHelper.close();
if (sqLiteDatabase != null) {
sqLiteDatabase.close();
}
}
return row_id;
}
public long update(Context context, String tablename, ContentValues contentValues, String _id) {
DataBaseHelper databaseHelper = null;
SQLiteDatabase sqLiteDatabase = null;
long row_id = 0;
try {
databaseHelper = new DataBaseHelper(context);
sqLiteDatabase = databaseHelper.getWritableDatabase();
row_id = sqLiteDatabase.update(tablename, contentValues, " _id =" + _id, null);
} catch (Exception e) {
e.printStackTrace();
assert databaseHelper != null;
databaseHelper.close();
assert sqLiteDatabase != null;
sqLiteDatabase.close();
} finally {
assert databaseHelper != null;
databaseHelper.close();
assert sqLiteDatabase != null;
sqLiteDatabase.close();
}
if (row_id > 0) {
Log.d("No of updated row is", "1");
}
return row_id;
}
}
和Table类是这样的;
public class Table {
public static class Contact {
public static final String TABLE_NAME = "contact_details";
public static final String
_ID = "_id",
NAME = "name",
EMAIL = "email",
PHOTO_URl = "photo_url",
MOBILE = "mobile",
LANDLINE = "landline",
ADDRESS = "address",
STATE = "state",
COUNTRY = "country",
CITY = "city",
COMPANY_NAME = "company_name",
WEBSITE = "website",
CATEGORY_TYPE = "category_type";
public static final String CREATE_CONTACT_DETAILS_TABLES = " CREATE TABLE " +
TABLE_NAME
+ "(" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ NAME + " TEXT, "
+ EMAIL + " TEXT, "
+ PHOTO_URl + " TEXT, "
+ MOBILE + " TEXT, "
+ LANDLINE + " TEXT, "
+ ADDRESS + " TEXT, "
+ STATE + " TEXT, "
+ COUNTRY + " TEXT, "
+ CITY + " TEXT, "
+ COMPANY_NAME + " TEXT, "
+ CATEGORY_TYPE + " TEXT, "
+ WEBSITE + " TEXT " + ")";
}
public static class Country {
public static final String TABLE_NAME = "country";
public static final String
COUNTRY_ID = "country_id",
COUNTRY_NAME = "country_name";
public static final String CREATE_COUNTRY_TABLES = " CREATE TABLE " +
TABLE_NAME
+ "(" + COUNTRY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COUNTRY_NAME + " TEXT " + ")";
}
public static class State {
public static final String TABLE_NAME = "state";
public static final String
STATE_ID = "state_id",
COUNTRY_ID = "country_id",
STATE_NAME = "state_name";
public static final String CREATE_STATE_TABLES = " CREATE TABLE " +
TABLE_NAME
+ "(" + STATE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COUNTRY_ID + " TEXT, "
+ STATE_NAME + " TEXT " + ")";
}
public static class City {
public static final String TABLE_NAME = "city";
public static final String
COUNTRY_ID = "country_id",
STATE_ID = "state_id",
CITY_ID = "city_id",
CITY_NAME = "city_name";
public static final String CREATE_CITY_TABLES = " CREATE TABLE " +
TABLE_NAME
+ "(" + CITY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COUNTRY_ID + " TEXT, "
+ STATE_ID + " TEXT, "
+ CITY_NAME + " TEXT " + ")";
}
public static class Category {
public static final String TABLE_NAME = "category_type";
public static final String
CAT_ID = "cat_id",
CATEGORY_TYPE = "category_type";
public static final String CREATE_CATEGORY_TABLES = " CREATE TABLE " +
TABLE_NAME
+ "(" + CAT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ CATEGORY_TYPE + " TEXT " + ")";
}
}
在sqlite中添加到Table Category的类别类型的数组是;
我需要有关如何选择在特定类别下创建的所有联系人的帮助。到目前为止,此查询选择所有联系人并填充listview适配器而不进行分类;
public void getDetailList() {
mapArrayList = new ArrayList<>();
DataBaseHelper dbHelper = null;
SQLiteDatabase sqLiteDatabase;
try {
dbHelper = new DataBaseHelper(getActivity());
sqLiteDatabase = dbHelper.getReadableDatabase();
HashMap<String, Object> hashMap;
cursor = sqLiteDatabase.rawQuery("select * from " +
Table.Contact.TABLE_NAME + " ct , "
+ Table.Country.TABLE_NAME + " co ,"
+ Table.Category.TABLE_NAME + " ca where" +
" ct." + Table.Contact.COUNTRY +
" = co." + Table.Country.COUNTRY_ID
+ " and " +
" ct." + Table.Contact.CATEGORY_TYPE +
" = ca." + Table.Category.CAT_ID + " order by " + Table.Contact.NAME + " asc ", null);
while (cursor.moveToNext()) {
hashMap = new HashMap<>();
hashMap.put("_id", cursor.getString(cursor.getColumnIndex(Table.Contact._ID)));
hashMap.put(fields[1], cursor.getString(cursor.getColumnIndex(Table.Contact.NAME)));
hashMap.put(fields[2], cursor.getString(cursor.getColumnIndex(Table.Contact.MOBILE)));
hashMap.put(fields[3], cursor.getString(cursor.getColumnIndex(Table.Contact.LANDLINE)));
hashMap.put(fields[4], cursor.getString(cursor.getColumnIndex(Table.Contact.EMAIL)));
hashMap.put(fields[5], cursor.getString(cursor.getColumnIndex(Table.Contact.ADDRESS)));
hashMap.put(fields[6], cursor.getString(cursor.getColumnIndex(Table.Country.COUNTRY_NAME)));
hashMap.put(fields[7], cursor.getString(cursor.getColumnIndex(Table.Contact.STATE)));
hashMap.put(fields[8], cursor.getString(cursor.getColumnIndex(Table.Contact.CITY)));
hashMap.put(fields[9], cursor.getString(cursor.getColumnIndex(Table.Contact.COMPANY_NAME)));
hashMap.put(fields[10], cursor.getString(cursor.getColumnIndex(Table.Contact.WEBSITE)));
hashMap.put(fields[11], cursor.getString(cursor.getColumnIndex(Table.Category.CATEGORY_TYPE)));
hashMap.put("image", cursor.getString(cursor.getColumnIndex(Table.Contact.PHOTO_URl)));
mapArrayList.add(hashMap);
}
cursor.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
assert dbHelper != null;
dbHelper.close();
}
adapter = new ContactListAdapter(getActivity(), R.layout.contact_list,
R.id.imgContact, R.id.tvContact, R.id.tvNumber, R.id.tvEmail, mapArrayList);
listView.setAdapter(adapter);
}
请帮我修改上述代码段中的查询至至少get listview以填充特定群组的联系人。
提前谢谢。
答案 0 :(得分:0)
尝试以下查询以获取具有特定类别的所有联系人姓名&amp;国家
select name from contact_details where category_type = 'Friends' and country = 'xyz'
答案 1 :(得分:0)
cursor = sqLiteDatabase.rawQuery("select * from " +
Table.Contact.TABLE_NAME + " ct , "
+ Table.Country.TABLE_NAME + " co ,"
+ Table.Category.TABLE_NAME + " ca where" +
" ct." + Table.Contact.COUNTRY +
" = co." + Table.Country.COUNTRY_ID
+ " and " +
" ct." + Table.Contact.CATEGORY_TYPE +
" = ca." + Table.Category.CAT_ID + " and " + " ca." + Table.Category.CAT_ID + " = '" + 2 + "'"+
" order by " + Table.Contact.NAME + " asc ", null);