java - inserting values in to SqlLite database. Android -
i learning android development through video tutorials.
using idea in tutorial. have created own class, database handler (dbhandler.java) file , (kovil.java) file. these files working perfectly.
my aim add values database table. once main activity (mainactivity.java) loads. want know how add values table in mainactivity.java file on create event code.
can 1 me. thank you..
here codes..
dbhandler.java file
package lk.adspace.jaffnadb; 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 dbhandler extends sqliteopenhelper { private static final int database_version = 1; // database name private static final string database_name = "jaffnatempletest"; // temple table name private static final string table_temple = "templ"; // contacts table columns names private static final string key_id = "id"; private static final string key_tmpname = "temple_name"; private static final string key_tmptype = "temple_type"; private static final string key_latitude = "latitude"; private static final string key_longitude = "longitude"; private static final string key_imgname = "image_name"; private static final string key_yearbuild = "year_build"; private static final string key_address = "address"; private static final string key_city = "city"; private static final string key_email = "email"; private static final string key_web = "website"; private static final string key_tel1 = "telephone1"; private static final string key_tel2 = "telephone2"; private static final string key_descri = "description"; private final arraylist<kovil> temple_list = new arraylist<kovil>(); public dbhandler (context context) { super(context, database_name, null, database_version); } // creating tables @override public void oncreate(sqlitedatabase db) { string create_temple_table = "create table " + table_temple + "(" + key_id + " integer primary key autoincrement not null," + key_tmpname + " text," + key_tmptype + " text," + key_latitude + " text," + key_longitude + " text," + key_imgname + " text," + key_yearbuild + " text," + key_address + " text," + key_city + " text," + key_email + " text," + key_web + " text," + key_tel1 + " text," + key_tel2 + " text," + key_descri + " text" + ")"; db.execsql(create_temple_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_temple); // create tables again oncreate(db); } // adding new temple public void add_temple(kovil kovil) { sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(key_tmpname, kovil.gettemplename()); values.put(key_tmptype, kovil.gettempletype()); values.put(key_latitude, kovil.getlatitude()); values.put(key_longitude, kovil.getlongitude()); values.put(key_imgname, kovil.getimage_name()); values.put(key_yearbuild, kovil.getyear_build()); values.put(key_address, kovil.getaddress()); values.put(key_city, kovil.getcity()); values.put(key_email, kovil.getemail()); values.put(key_web, kovil.getwebsite()); values.put(key_tel1, kovil.gettelephone1()); values.put(key_tel2, kovil.gettelephone2()); values.put(key_descri, kovil.getdescription()); // inserting row db.insert(table_temple, null, values); db.close(); // closing database connection } // getting single contact kovil get_temple(int id) { sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.query(table_temple, new string[] { key_id, key_tmpname, key_tmptype, key_latitude, key_longitude, key_imgname, key_yearbuild, key_address, key_city, key_email, key_email, key_web, key_tel1, key_tel2, key_descri }, key_id + "=?", new string[] { string.valueof(id) }, null, null, null, null); if (cursor != null) cursor.movetofirst(); kovil kovil = new kovil(integer.parseint(cursor.getstring(0)), cursor.getstring(1), cursor.getstring(2), cursor.getstring(3), cursor.getstring(4), cursor.getstring(5), cursor.getstring(6), cursor.getstring(7), cursor.getstring(8), cursor.getstring(9), cursor.getstring(10), cursor.getstring(11), cursor.getstring(12), cursor.getstring(13)); // return contact cursor.close(); db.close(); return kovil; } // getting contacts public arraylist<kovil> get_temple() { try { temple_list.clear(); // select query string selectquery = "select * " + table_temple; sqlitedatabase db = this.getwritabledatabase(); cursor cursor = db.rawquery(selectquery, null); // looping through rows , adding list if (cursor.movetofirst()) { { kovil kovil = new kovil(); kovil.setid(integer.parseint(cursor.getstring(0))); kovil.settemplename(cursor.getstring(1)); kovil.settempletype(cursor.getstring(2)); kovil.setlatitude(cursor.getstring(3)); kovil.setlongitude(cursor.getstring(4)); kovil.setimage_name(cursor.getstring(5)); kovil.setyear_build(cursor.getstring(6)); kovil.setaddress(cursor.getstring(7)); kovil.setcity(cursor.getstring(8)); kovil.setemail(cursor.getstring(9)); kovil.setwebsite(cursor.getstring(10)); kovil.settelephone1(cursor.getstring(11)); kovil.settelephone2(cursor.getstring(12)); kovil.setdescription(cursor.getstring(13)); // adding contact list temple_list.add(kovil); } while (cursor.movetonext()); } // return contact list cursor.close(); db.close(); return temple_list; } catch (exception e) { // todo: handle exception log.e("all_temples", "" + e); } return temple_list; } }
kovil.java file
package lk.adspace.jaffnadb; public class kovil { //public variables public int _id; public string _temple_name; public string _temple_type; public string _latitude; public string _longitude; public string _image_name; public string _year_build; public string _address; public string _city; public string _email; public string _website; public string _telephone1; public string _telephone2; public string _description; //empty constructor public kovil (){ } // int id, string temple_name, string temple_type, string latitude, string longitude, string image_name, string year_build, string address, string city, string email, string website, string telephone1, string telephone2, string description public kovil(int id, string temple_name, string temple_type, string latitude, string longitude, string image_name, string year_build, string address, string city, string email, string website, string telephone1, string telephone2, string description) { // todo auto-generated constructor stub this._id= id; this._temple_name=temple_name; this._temple_type=temple_type; this._latitude=latitude; this._longitude=longitude; this._image_name=image_name; this._year_build=year_build; this._address=address; this._city=city; this._email=email; this._website=website; this._telephone1=telephone1; this._telephone2=telephone2; this._description=description; } public int getid() { return this._id; } public void setid(int id) { this._id = id; } public string gettemplename() { return this._temple_name; } public void settemplename(string temple_name) { this._temple_name=temple_name; } public string gettempletype() { return this._temple_type; } public void settempletype(string temple_type) { this._temple_type=temple_type; } public string getlatitude() { return this._latitude; } public void setlatitude(string latitude) { this._latitude=latitude; } public string getlongitude() { return this._longitude; } public void setlongitude(string longitude) { this._longitude=longitude; } public string getimage_name() { return this._image_name; } public void setimage_name(string image_name) { this._image_name=image_name; } public string getyear_build() { return this._year_build; } public void setyear_build(string year_build) { this._year_build=year_build; } public string getaddress() { return this._address; } public void setaddress(string address) { this._address=address; } public string getcity() { return this._city; } public void setcity(string city) { this._city=city; } public string getemail() { return this._email; } public void setemail(string email) { this._email=email; } public string getwebsite() { return this._website; } public void setwebsite(string website) { this._website=website; } public string gettelephone1() { return this._telephone1; } public void settelephone1(string telephone1) { this._telephone1=telephone1; } public string gettelephone2() { return this._telephone2; } public void settelephone2(string telephone2) { this._telephone2=telephone2; } public string getdescription() { return this._description; } public void setdescription(string description) { this._description=description; } }
mainactivity.java file
package lk.adspace.jaffnadb; import android.os.bundle; import android.app.activity; import android.view.menu; public class mainactivity extends activity { dbhandler dbhand = new dbhandler(this); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } }
you created database, table insert method. whats problem in inserting.
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //define , initialize kovil constructor appropriate arguments. kovil insertdata = new kovil(your_id, temple_name, temple_type, latitude, longitude, image_name, year_build, address, city, email, website, telephone1, telephone2, description); dbhandler dbhand = new dbhandler(this); dbhand .add_temple(insertdata ); }
Comments
Post a Comment