{"id":6515,"date":"2014-04-20T01:15:40","date_gmt":"2014-04-20T01:15:40","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/20\/sqliteconstraintexception-error-code-19-constraint-failed-android-collection-of-common-programming-errors\/"},"modified":"2014-04-20T01:15:40","modified_gmt":"2014-04-20T01:15:40","slug":"sqliteconstraintexception-error-code-19-constraint-failed-android-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/20\/sqliteconstraintexception-error-code-19-constraint-failed-android-collection-of-common-programming-errors\/","title":{"rendered":"SQLiteConstraintException: error code 19: constraint failed Android-Collection of common programming errors"},"content":{"rendered":"<p>I am trying to insert lat and long coordinates into a SQLite table and I am getting <code>SQLiteConstraintException: error code 19: constraint failed<\/code>.<\/p>\n<p>I have the code surrounded by try\/catch and no exception is thrown. I get the constraint error in my logcat.<\/p>\n<p>I have attached the DB class and the call from my main activity. Any help or guidance would be very much appreciated!<\/p>\n<p>Here are the top few lines from my logcat:<\/p>\n<pre><code>08-25 14:39:06.934: E\/SQLiteDatabase(681): Error inserting lat=59310767 longi=-8613281\n08-25 14:39:06.934: E\/SQLiteDatabase(681): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed\n08-25 14:39:06.934: E\/SQLiteDatabase(681):  at android.database.sqlite.SQLiteStatement.native_executeInsert(Native Method)\n08-25 14:39:06.934: E\/SQLiteDatabase(681):  at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:113)\n08-25 14:39:06.934: E\/SQLiteDatabase(681):  at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1745)\n08-25 14:39:06.934: E\/SQLiteDatabase(681):  at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1618)\n<\/code><\/pre>\n<p>Here is the code from my DbHelper:<\/p>\n<pre><code>public void onCreate(SQLiteDatabase db) \n{\n    \/\/ TODO Auto-generated method stub\n    db.execSQL(\"CREATE TABLE \" + DATABASE_TABLE + \" (\" + \n            KEY_ID + \" INTEGER PRIMARY KEY AUTOINCREMENT, \" + \n            KEY_LAT + \" REAL NOT NULL, \" + \n            KEY_LONGI + \" REAL NOT NULL)\" \n\n            \/\/ KEY_NAME + \" TEXT NULL, \" +\n            \/\/ KEY_INFO + \" TEXT NULL);\"\n    );\n}\n\n@Override\npublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {\n    \/\/ TODO Auto-generated method stub\n    db.execSQL(\"DROP TABLE IF EXISTS \" + DATABASE_TABLE);\n    onCreate(db);\n}\n<\/code><\/pre>\n<p>Here is my DatabaseManager<\/p>\n<pre><code>public DatabaseManager(Context c) {\n    ourContext = c;\n}\n\npublic DatabaseManager open() throws SQLException {\n    ourHelper = new DbHelper(ourContext);\n    ourDB = ourHelper.getWritableDatabase(); \/\/example show writeable\n    return this;\n}\n\npublic void close() {\n    ourHelper.close();\n} \n\npublic long createPin(long latitude, long longitude) {\n    ContentValues cv = new ContentValues();\n    cv.put(KEY_LAT, latitude);\n    cv.put(KEY_LONGI, longitude);\n\n    return ourDB.insert(DATABASE_TABLE, null, cv);\n}\n<\/code><\/pre>\n<p>This is the call from my MainActivity:<\/p>\n<pre><code>try{\n    long longitude= touchedPoint.getLongitudeE6();\n    long latitude= touchedPoint.getLatitudeE6();\n\n    \/\/Add Pin to the Database\n    DatabaseManager db = new DatabaseManager(MainActivity.this);\n    db.open();\n    db.createPin(latitude,longitude);\n    db.close();\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I am trying to insert lat and long coordinates into a SQLite table and I am getting SQLiteConstraintException: error code 19: constraint failed. I have the code surrounded by try\/catch and no exception is thrown. I get the constraint error in my logcat. I have attached the DB class and the call from my main [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6515","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6515","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6515"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6515\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}