I can't insert data into a SQLite databse on Android -
i trying insert room numbers , coordinates database, access them give directions rooms, seems failing insert data database.
here database handler:
public class mrldatabasehandler extends sqliteopenhelper { //private context context; //private string locationsfile; private static final int database_version = 1; private static final string database_name = "mrl_locationsdb3"; static final string table_locations = "locations"; //table name private static final string key_id = "_gpspoint_id"; private static final string key_rnumber = "_rnumber"; private static final string key_lat = "_latitude"; private static final string key_longi = "_longitude"; private static final string key_alti = "_altitude"; public mrldatabasehandler(context context) { super(context, database_name, null, database_version); //this.context = context; //locationsfile = context.getstring(r.string.mrl_locations); } @override public void oncreate(sqlitedatabase db) { string create_locations_table = "create table " + table_locations + "(" + key_id + " integer primary key autoincrement," + key_rnumber + " text," + key_lat + " double," + key_longi + " double," + key_alti + " double"+ ")"; db.execsql(create_locations_table); //populating locations table /**mrl_locationsloader locationloader = new mrl_locationsloader(context, db, locationsfile); try { locationloader.loaddata(); }catch (ioexception e){ e.printstacktrace(); } ***/ } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exists " + table_locations); // create tables again oncreate(db); } //////crud implemetation // adding new coordinate void addcoordinate( mrl_locationsmodelclass point) { sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(key_id , point.get_gpspoint_id()); values.put(key_rnumber, point.get_rnumber()); values.put(key_lat, point.get_latitude()); values.put(key_longi, point.get_longitude()); values.put(key_alti, point.get_altitude()); // inserting row db.insert(table_locations, null, values); db.close(); // closing database connection } // getting single coordinate sqlite database mrl_locationsmodelclass getpoint(int id) { sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.query(table_locations, new string[] { key_id, key_rnumber, key_lat, key_longi, key_alti }, key_id + "=?", new string[] { string.valueof(id) }, null, null, null, null); if (cursor != null) cursor.movetofirst(); mrl_locationsmodelclass locationsmodelclass = new mrl_locationsmodelclass( integer.parseint(cursor.getstring(0)), cursor.getstring(1), double.parsedouble(cursor.getstring(2)), double.parsedouble(cursor.getstring(3)), double.parsedouble(cursor.getstring(4))); // return contact return locationsmodelclass; }
here class using it:
public class drawpath extends activity { mrldatabasehandler db = new mrldatabasehandler(this); int dest = 9, src = 6; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // inserting coordinates log.d("insert: ", "inserting .."); db.addcoordinate(new mrl_locationsmodelclass("l101", -12.804646,28.240218, (double) 1223)); db.addcoordinate(new mrl_locationsmodelclass("c102", -12.8048,28.240736, (double) 1227)); db.addcoordinate(new mrl_locationsmodelclass("z103", -12.805274,28.240529, (double) 1225)); db.addcoordinate(new mrl_locationsmodelclass("a104", -12.805347,28.238821, (double) 1232)); db.addcoordinate(new mrl_locationsmodelclass("nh201",-12.804497,28.24068, (double) 1222)); mrl_locationsmodelclass srcpoint = db.getpoint(src); mrl_locationsmodelclass destpoint = db.getpoint(dest); shareddata data = shareddata.getinstance(); data.setapikey("0fm62i15vxd_pvpf8wxdtk0ooud2wm8rxst3kvg"); data.setsrc_lat(srcpoint.get_latitude()); data.setsrc_lng(srcpoint.get_longitude()); data.setdest_lat(destpoint.get_latitude()); data.setdest_lng(destpoint.get_longitude()); startactivity(new intent(drawpath.this,routepath.class)); } }
and here logcat:
03-13 05:19:26.337: d/insert:(618): inserting .. 03-13 05:19:30.940: e/database(618): error inserting _longitude=28.240736 _rnumber=c102 _altitude=1227.0 _latitude=-12.8048 _gpspoint_id=0 03-13 05:19:30.940: e/database(618): android.database.sqlite.sqliteconstraintexception: error code 19: constraint failed 03-13 05:19:30.940: e/database(618): @ android.database.sqlite.sqlitestatement.native_execute(native method) 03-13 05:19:30.940: e/database(618): @ android.database.sqlite.sqlitestatement.execute(sqlitestatement.java:55) 03-13 05:19:30.940: e/database(618): @ android.database.sqlite.sqlitedatabase.insertwithonconflict(sqlitedatabase.java:1549) 03-13 05:19:30.940: e/database(618): @ android.database.sqlite.sqlitedatabase.insert(sqlitedatabase.java:1410) 03-13 05:19:30.940: e/database(618): @ com.agarwal.route.mrldatabasehandler.addcoordinate(mrldatabasehandler.java:84) 03-13 05:19:30.940: e/database(618): @ com.agarwal.route.drawpath.oncreate(drawpath.java:20) 03-13 05:19:30.940: e/database(618): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1047) 03-13 05:19:30.940: e/database(618): @ android.app.activitythread.performlaunchactivity(activitythread.java:2627) 03-13 05:19:30.940: e/database(618): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2679) 03-13 05:19:30.940: e/database(618): @ android.app.activitythread.access$2300(activitythread.java:125) 03-13 05:19:30.940: e/database(618): @ android.app.activitythread$h.handlemessage(activitythread.java:2033) 03-13 05:19:30.940: e/database(618): @ android.os.handler.dispatchmessage(handler.java:99) 03-13 05:19:30.940: e/database(618): @ android.os.looper.loop(looper.java:123) 03-13 05:19:30.940: e/database(618): @ android.app.activitythread.main(activitythread.java:4627) 03-13 05:19:30.940: e/database(618): @ java.lang.reflect.method.invokenative(native method) 03-13 05:19:30.940: e/database(618): @ java.lang.reflect.method.invoke(method.java:521) 03-13 05:19:30.940: e/database(618): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:868) 03-13 05:19:30.940: e/database(618): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:626) 03-13 05:19:30.940: e/database(618): @ dalvik.system.nativestart.main(native method)
you're trying add value 0 integer primary key
column on , on again. causes constraint failure.
to fix it, remove line addcoordinate()
:
values.put(key_id , point.get_gpspoint_id());
to capture autogenerated id value, take return value insert()
:
long id = db.insert(table_locations, null, values); point.set_gpspoint_id(id);
(old question got bumped edit.)
Comments
Post a Comment