Friday, 15 July 2011

what i should do to insert data from json into sqlite in Android -



what i should do to insert data from json into sqlite in Android -

im creat sqlit database in android store info json in database view info if app offline im add together code creat database , colum not know how insert info json db. should insert info json sqlite should add together in code ?

sqliteopenhelper

import android.content.contentvalues; import android.content.context; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqlitedatabase.cursorfactory; import android.database.sqlite.sqliteopenhelper; import android.util.log; public class bookingtabel extends sqliteopenhelper { private static final int db_version = 1; private static final string db_name = "mydb"; // books table name private static final string table_bookinds = "bookings"; // books table columns names private static final string key_id = "id"; private static final string key_booking_num = "booking_num"; private static final string key_title = "title"; private static final string key_booking_start= "booking_start"; private static final string key_booking_end = "booking_end"; private static final string key_property = "property_type"; private static final string key_customer_id = "customer_id"; private static final string key_description = "description"; private static final string key_accept_status = "accept_status"; private static final string key_booling_address = "booking_address"; private static final string key_po_box = "po_box"; private static final string key_created = "created"; private static final string key_postcode = "postcode"; private static final string key_state = "state"; private static final string key_street_address = "street_address"; private static final string key_street_number = "street_number"; private static final string key_suburb = "suburb"; private static final string key_unit_lot_number = "unit_lot_number"; private static final string key_status = "status"; private static final string key_convert_status = "convert_status"; private static final string key_qoute = "qoute"; private static final string key_sub_total = "sub_total"; private static final string key_total_discount = "total_discount"; private static final string key_booking_distance = "booking_distance"; private static final string key_gst = "gst"; private static final string key_original_booking_id = "original_booking_id"; public bookingtabel(context context) { super(context, db_name, null,db_version); // todo auto-generated constructor stub } private static final string[] columns = {key_id,key_booking_num,key_title,key_booking_start,key_booking_end,key_property, key_customer_id,key_description,key_accept_status,key_booling_address,key_po_box,key_created , key_postcode,key_state,key_street_address,key_street_number,key_suburb,key_unit_lot_number,key_status, key_convert_status,key_qoute,key_sub_total,key_total_discount,key_booking_distance,key_gst, key_original_booking_id,}; @override public void oncreate(sqlitedatabase db) { // todo auto-generated method stub // sql statement create book table string create_bookinds_table = "create table booking ( " + "id integer primary key"+ "booking_num varchar(250)"+ "title varchar(250)"+ "booking_start datetime "+"booking_end datetime "+ "property_type varchar(250) not null default 'house'"+ "customer_id integer+description text"+"accept_status varchar(250)"+"booking_address varchar(250)" + "po_box varchar(250)"+ "created integer"+ "postcode integer"+"state varchar(250)"+ "street_address varchar(250)"+"street_number integer"+"suburb varchar(250)"+"unit_lot_number integer"+ "status varchar(250)"+"convert_status varchar(250)"+"qoute float"+"sub_total float"+"total_discount float"+ "booking_distance float"+"gst float"+"original_booking_id integer )"; // create books table db.execsql(create_bookinds_table); } @override public void onupgrade(sqlitedatabase db, int arg1, int arg2) { // todo auto-generated method stub db.execsql("drop table if exists bookings"); // create fresh books table this.oncreate(db); } public void addbookings(bookings booking){ log.d("addbooking", booking.tostring()); sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(key_booking_num,booking.getbookingnum()); values.put(key_title,booking.gettitle()); values.put(key_booking_start ,booking.getbookingstart() ); values.put(key_booking_end ,booking.getbooking_end() ); values.put(key_property ,booking.getpropertytype() ); values.put(key_customer_id ,booking.getcustomer_id() ); values.put( key_description,booking.getdescription() ); values.put( key_accept_status,booking.getaccept_status() ); values.put( key_booling_address,booking.getbookingaddress() ); values.put( key_po_box,booking.getpo_box() ); values.put( key_created,booking.getcreated() ); values.put( key_postcode,booking.getpostcode() ); values.put( key_state,booking.getstate() ); values.put( key_street_address,booking.getstreet_address() ); values.put( key_street_number,booking.getstreet_number() ); values.put( key_suburb,booking.getsuburb() ); values.put( key_unit_lot_number,booking.getunit_lot_number() ); values.put( key_status,booking.getstatus() ); values.put( key_convert_status,booking.getconvert_status() ); values.put( key_qoute,booking.getqoute() ); values.put( key_sub_total,booking.getsub_total() ); values.put( key_total_discount,booking.gettotal_discount() ); values.put( key_booking_distance,booking.getbooking_distance() ); values.put( key_gst,booking.getgst() ); values.put( key_original_booking_id,booking.getoriginal_booking_id() ); db.insert(table_bookinds, null, values); db.close(); } }

bookings

public class bookings { private int id; private string booking_num; private string title; private string booking_start; private string booking_end; private string property_type; private int customer_id; private string description; private string accept_status; private string booking_address; private string po_box; private int created; private int postcode; private string state; private string street_address; private int street_number; private string suburb; private int unit_lot_number; private string status; private string convert_status; private float qoute; private float sub_total; private float total_discount; private float booking_distance; private float gst; private int original_booking_id; public bookings(){} public bookings(string booking_num ,string title ,string booking_start ,string booking_end , string property_type ,int customer_id ,string description ,string accept_status ,string booking_address , string po_box ,int created ,int postcode ,string state ,string street_address ,int street_number , string suburb ,int unit_lot_number ,string status ,string convert_status ,float qoute ,float sub_total , float total_discount ,float booking_distance ,float gst ,int original_booking_id){ this.booking_num = booking_num; this.title = title; this.booking_start = booking_start; this.booking_end =booking_end ; this.property_type =property_type; this.customer_id = customer_id; this.description = description; this.accept_status = accept_status; this.booking_address = booking_address; this.po_box = po_box; this.created = created; this.postcode = postcode; this.state = state; this.street_address = street_address; this.street_number = street_number; this.suburb = suburb; this.unit_lot_number = unit_lot_number; this.status = status; this.convert_status = convert_status; this.qoute = qoute; this.sub_total = sub_total; this.total_discount = total_discount; this.booking_distance = booking_distance; this.gst = gst; this.original_booking_id = original_booking_id; } // ---- setter public void setid(int id){ this.id = id; } public void setbookingnum(string booking_num){ this.booking_num = booking_num; } public void settitle(string title){ this.title = title; } public void setbookingstart(string booking_start){ this.booking_start = booking_start; } public void setbookingend(string booking_end ){ this.booking_end = booking_end ; } public void setpropertytype(string property_type){ this.property_type = property_type; } public void setcustomerid(int customer_id){ this.customer_id = customer_id; } public void setdescription(string description){ this.description = description; } public void setacceptstatus(string accept_status){ this.accept_status = accept_status; } public void setbookingaddress(string booking_address){ this.booking_address = booking_address; } public void setpobox(string po_box ){ this.po_box = po_box ; } public void setcreated(int created){ this.created = created ; } public void setpostcode(int postcode){ this.postcode = postcode; } public void setstate(string state){ this.state =state ; } public void setstreetaddress(string street_address){ this.street_address = street_address ; } public void setstreetnumber(int street_number){ this.street_number = street_number; } public void setsuburb(string suburb){ this.suburb = suburb; } public void setunit_lot_number(int unit_lot_number ){ this.unit_lot_number = unit_lot_number; } public void setstatus(string status){ this.status =status ; } public void setconvert_status(string convert_status){ this.convert_status =convert_status ; } public void setqoute(float qoute){ this.qoute = qoute; } public void setsubtotal(float sub_total ){ this.sub_total =sub_total ; } public void settotal_discount(float total_discount){ this.total_discount =total_discount ; } public void setbookingdistance(float booking_distance){ this.booking_distance =booking_distance ; } public void setgst(float gst){ this.gst = gst; } public void setoriginal_booking_id(int original_booking_id){ this.original_booking_id = original_booking_id ; } // --- getter --- public int getid(){ homecoming id; } public string getbookingnum(){ homecoming booking_num; } public string gettitle(){ homecoming title ; } public string getbookingstart(){ homecoming booking_start ; } public string getbooking_end(){ homecoming booking_end; } public string getpropertytype(){ homecoming property_type; } public int getcustomer_id(){ homecoming customer_id ; } public string getdescription(){ homecoming description; } public string getaccept_status(){ homecoming accept_status; } public string getbookingaddress(){ homecoming booking_address ; } public string getpo_box(){ homecoming po_box; } public int getcreated(){ homecoming created; } public int getpostcode(){ homecoming postcode ; } public string getstate(){ homecoming state ; } public string getstreet_address(){ homecoming street_address ; } public int getstreet_number(){ homecoming street_number ; } public string getsuburb(){ homecoming suburb ; } public int getunit_lot_number(){ homecoming unit_lot_number ; } public string getstatus(){ homecoming status; } public string getconvert_status(){ homecoming convert_status; } public float getqoute(){ homecoming qoute ; } public float getsub_total(){ homecoming sub_total; } public float gettotal_discount(){ homecoming total_discount; } public float getbooking_distance(){ homecoming booking_distance; } public float getgst(){ homecoming gst; } public int getoriginal_booking_id(){ homecoming original_booking_id ; } public string tostring(){ homecoming "booking >> id:"+id+" | booking_num:"+booking_num+" | title:"+title+ " | booking_start"+booking_start+" | booking_end"+booking_end+" | property_type"+property_type+ " | customer_id "+customer_id +" |description "+description+" | accept_status"+accept_status+ " | booking_address"+booking_address+" | po_box"+po_box+" | created"+created+" | postcode"+postcode+ " | state"+state+" | street_address"+street_address+" | street_number"+street_number+ " | suburb"+suburb+" | unit_lot_number"+unit_lot_number+" | status"+status+ " | convert_status"+convert_status+" | qoute"+qoute+" | sub_total"+sub_total+ " | total_discount"+total_discount+" | booking_distance"+booking_distance+" | gst"+gst+ " | original_booking_id"+original_booking_id; } }

allbookingfrag

import java.util.arraylist; import java.util.hashmap; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.client.httpclient; import org.apache.http.client.methods.httpget; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.util.entityutils; import org.json.jsonarray; import org.json.jsonobject; import android.content.intent; import android.content.sharedpreferences; import android.os.asynctask; import android.os.bundle; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.listadapter; import android.widget.listview; import android.widget.simpleadapter; public class allbookingfrag extends fragment { public static final string prefs_name = "myapp_settings"; private static final string givenusername = "email"; private static final string givenpassword = "password"; private static final string tag_result = "result"; private static final string tag_booking_num = "booking_num"; private static final string tag_title = "title"; private static final string tag_status = "status"; private static final string tag_booking_start = "booking_start"; private static final string tag_booking_end = "booking_end"; private static final string tag_qoute = "qoute"; private static final string tag_booking_address = "booking_address"; private static final string tag_customer = "customer"; private static final string tag_customer_name = "name"; string result = null; arraylist<hashmap<string, string>> resultlist; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.allbookingfrag, container, false); sharedpreferences settings = getactivity().getsharedpreferences(prefs_name, 0); string email = settings.getstring("email",givenusername); string password = settings.getstring("password",givenpassword); system.out.println("in allbooking activity names : " + email + "password : " + password ); connectwithhttpget(email, password ); resultlist = new arraylist<hashmap<string, string>>(); listview listallbooking = (listview)rootview.findviewbyid(r.id.listallbooking); homecoming rootview; } private void connectwithhttpget(string email, string password ) { class httpgetasynctask extends asynctask<string, void, string>{ @override protected string doinbackground(string... params) { // can see, doinbackground has taken array of strings argument //we need givenusername , givenpassword string email = params[0]; string password = params[1]; // string uuid = params[2]; system.out.println("paramusername: " + email + " parampassword : " + password + "paramuuid is: "); // create intermediate connect net httpclient httpclient = new defaulthttpclient(); // sending request web page want // because of sending request, have pass values through url httpget httpget = new httpget("xxxxxxxxxx?email=" + email + "&password=" + password +"&uuid=fdgsfsdfsfsf"+"&mode=booking" ); stringbuilder stringbuilder = new stringbuilder(); seek { httpresponse httpresponse = httpclient.execute(httpget); httpentity httpentity = httpresponse.getentity(); string jsonstr = null; jsonstr = entityutils.tostring(httpentity); jsonobject jsonobj = new jsonobject(jsonstr); // getting json array node jsonobject resultobj = jsonobj.getjsonobject("result"); jsonarray resultarr = resultobj.getjsonarray("result"); //log.d("response: ", "> " + jsonstr); // looping through contacts (int = 0; < resultarr.length(); i++) { jsonobject c = resultarr.getjsonobject(i); string booking_num = c.getstring(tag_booking_num); string title = c.getstring(tag_title); /* string splittitle= title; string [] mysplit_title = null; mysplit_title = splittitle.split("-"); string first_title=mysplit_title[0]; string qoute_title=mysplit_title[1]; string username_title=mysplit_title[2];*/ string status = c.getstring(tag_status); string booking_start = c.getstring(tag_booking_start); string my_starttime=booking_start; string [] my_date_time_start = null; my_date_time_start = my_starttime.split(" "); string date_str_start=my_date_time_start[0]; string time_str_start=my_date_time_start[1]; string booking_end = c.getstring(tag_booking_end); string my_endtime=booking_start; string [] my_date_time_end = null; my_date_time_end = my_endtime.split(" "); string date_str_end=my_date_time_end[0]; string time_str_end=my_date_time_end[1]; string qoute = c.getstring(tag_qoute); string booking_address = c.getstring(tag_booking_address); // system.out.println("booking_num: " + booking_num + " title : " + title + "booking_start: "+ booking_start); jsonobject client = c.getjsonobject(tag_customer); string name = customer.getstring(tag_customer_name); bookingtabel db = new bookingtabel(getactivity()); // tmp hashmap single contact hashmap<string, string> result3 = new hashmap<string, string>(); // adding each kid node hashmap key => value result3.put(tag_booking_num, booking_num); result3.put(tag_title, title); result3.put(tag_status, status); result3.put(tag_booking_start, time_str_start); result3.put(tag_booking_end, time_str_end); result3.put(tag_qoute, qoute); result3.put(tag_booking_address, booking_address); result3.put(tag_customer_name, name); // hashmap listview // arraylist<hashmap<string, string>> resultlist = new arraylist<hashmap<string, string>>(); // adding contact contact list resultlist.add(result3); system.out.println("resultlist: " + resultlist); } } grab (exception e) { // todo auto-generated grab block e.printstacktrace(); } homecoming null; } // argument comes method according homecoming type of doinbackground() , //it 3rd generic type of asynctask @override protected void onpostexecute(string result) { listview listallbooking = (listview)getactivity().findviewbyid(r.id.listallbooking); listadapter adapter = new simpleadapter(getactivity(), resultlist, r.layout.list_item_allbooking, new string[] { tag_title,tag_status, tag_booking_start, tag_booking_end,tag_qoute,tag_booking_address,tag_customer_name }, new int[] { r.id.listtitle,r.id.liststatus, r.id.listbooking_start, r.id.listbooking_end,r.id.listqoute,r.id.listbooking_address,r.id.listcustomername}); listallbooking.setadapter(adapter); // system.out.println("resultlist: " + resultlist); // setonitemclicklistener listallbooking.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> arg0, view view, int postion, long arg3) { // todo auto-generated method stub intent detailesitem = new intent(getactivity().getbasecontext(), estimates.class); startactivity(detailesitem); } }); } } // initialize asynctask class httpgetasynctask httpgetasynctask = new httpgetasynctask(); // parameter pass in execute() method relate first generic type of asynctask // passing connectwithhttpget() method arguments httpgetasynctask.execute(email, password ); } }

your json seems big. here have posted model...

=> json server => parse strings while insert database

declare next after extending activiy

private static final string table_berth = "berth"; private static final string key_bid = "berth_id"; private static final string key_class_no = "class_no"; private static final string key_class_name = "class_name"; private static final string key_seat_no = "seat_no"; private static final string key_seat_pos = "seat_pos"; private jsonarray mcomments = null; private arraylist<hashmap<string, string>> mcommentlist;

in method add together db

databasehandler db = new databasehandler(getapplicationcontext()); // in case,handler name booking tabel mcommentlist = new arraylist<hashmap<string, string>>(); string url = "your url here"; string json =null; jsonobject jsonobj = null; list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("some_param", "some_value")); servicehandler jsonparser = new servicehandler(); json = jsonparser.makeservicecall(url, servicehandler.post, params); seek { jsonobj = new jsonobject(json); if (jsonobj != null) { mcomments = jsonobj.getjsonarray("berths"); sqlitedatabase database = db.getwritabledatabase(); database.begintransaction(); try{ (int = 0; < mcomments.length(); i++) { jsonobject c = mcomments.getjsonobject(i); // gets content of each tag int class_no = c.getint(tag_class_no); string class_name = c.getstring(tag_class_name); int seat_no = c.getint(tag_seat_no); string seat_pos = c.getstring(tag_seat_pos); contentvalues values = new contentvalues(); values.put(key_class_no, class_no); values.put(key_class_name, class_name); values.put(key_seat_no, seat_no); values.put(key_seat_pos, seat_pos); database.insert(table_berth,null,values); } // transaction successful , records have been inserted database.settransactionsuccessful(); }catch(exception e){ }finally{ //end transaction database.endtransaction(); } } } grab (jsonexception e) { // todo auto-generated grab block e.printstacktrace(); }

android json sqlite insert

No comments:

Post a Comment