database - How to query android sqlite for specific category in table? -
i have database created sqlite in android , want able list contacts in specific group. database helper code this;
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); (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); (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(); } { 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(); } { 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; }
}
and table class this;
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 " + ")"; }
}
the array of category type added table category in sqlite are;
- friends
- family
- office
- other
i need on how select contacts created under specific category. far query select contacts , populate listview adapter without categorizing;
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 * " + 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 + " , " + " ct." + table.contact.category_type + " = ca." + table.category.cat_id + " order " + 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(); } { 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); }
please me modify query in above snippet atleast listview populate contacts specific group.
thank in advance.
try below query contact name have specific category & country
select name contact_details category_type = 'friends' , country = 'xyz'
Comments
Post a Comment