Create a separate Database class android -
i new android.in application need created 4-5 tables. wanted know if can create separate database class , separate class tables? because of code looks clean instead of using
public dbadapter extends sqliteopenhelper{ //all crud operations 4-5 tables in same class }
can suggest how make separate initiate db , use crud operations different tables classes?
import java.util.arraylist; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.util.log; public class databasehandler extends sqliteopenhelper { // static variables // database version private static final int database_version = 1; // database name private static final string database_name = "contactsmanager"; // contacts table name private static final string table_contacts = "contacts"; // contacts table columns names private static final string key_id = "id"; private static final string key_name = "name"; private static final string key_ph_no = "phone_number"; private static final string key_email = "email"; private final arraylist<contact> contact_list = new arraylist<contact>(); 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_name + " text," + key_ph_no + " text," + key_email + " text" + ")"; db.execsql(create_contacts_table); } // upgrading database @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // drop older table if existed db.execsql("drop table if exists " + table_contacts); // create tables again oncreate(db); } /** * crud(create, read, update, delete) operations */ // adding new contact public void add_contact(contact contact) { sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(key_name, contact.getname()); // contact name values.put(key_ph_no, contact.getphonenumber()); // contact phone values.put(key_email, contact.getemail()); // contact email // inserting row db.insert(table_contacts, null, values); db.close(); // closing database connection } // getting single contact contact get_contact(int id) { sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.query(table_contacts, new string[] { key_id, key_name, key_ph_no, key_email }, key_id + "=?", new string[] { string.valueof(id) }, null, null, null, null); if (cursor != null) cursor.movetofirst(); contact contact = new contact(integer.parseint(cursor.getstring(0)), cursor.getstring(1), cursor.getstring(2), cursor.getstring(3)); // return contact cursor.close(); db.close(); return contact; } // getting contacts public arraylist<contact> get_contacts() { try { contact_list.clear(); // select query string selectquery = "select * " + table_contacts; sqlitedatabase db = this.getwritabledatabase(); cursor cursor = db.rawquery(selectquery, null); // looping through rows , adding list if (cursor.movetofirst()) { { contact contact = new contact(); contact.setid(integer.parseint(cursor.getstring(0))); contact.setname(cursor.getstring(1)); contact.setphonenumber(cursor.getstring(2)); contact.setemail(cursor.getstring(3)); // adding contact list contact_list.add(contact); } while (cursor.movetonext()); } // return contact list cursor.close(); db.close(); return contact_list; } catch (exception e) { // todo: handle exception log.e("all_contact", "" + e); } return contact_list; } // updating single contact public int update_contact(contact contact) { sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(key_name, contact.getname()); values.put(key_ph_no, contact.getphonenumber()); values.put(key_email, contact.getemail()); // updating row return db.update(table_contacts, values, key_id + " = ?", new string[] { string.valueof(contact.getid()) }); } // deleting single contact public void delete_contact(int id) { sqlitedatabase db = this.getwritabledatabase(); db.delete(table_contacts, key_id + " = ?", new string[] { string.valueof(id) }); db.close(); } // getting contacts count public int get_total_contacts() { string countquery = "select * " + table_contacts; sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.rawquery(countquery, null); cursor.close(); // return count return cursor.getcount(); } }
Comments
Post a Comment