{"id":5873,"date":"2014-04-07T23:40:24","date_gmt":"2014-04-07T23:40:24","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/07\/error-no-such-table-exists-sqlite-collection-of-common-programming-errors\/"},"modified":"2014-04-07T23:40:24","modified_gmt":"2014-04-07T23:40:24","slug":"error-no-such-table-exists-sqlite-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/07\/error-no-such-table-exists-sqlite-collection-of-common-programming-errors\/","title":{"rendered":"Error: No such table exists SQLite-Collection of common programming errors"},"content":{"rendered":"<p>I have a SQLite database with two tables, one for holding Login information, such as Email adress and password, and the other one to hold GPS coordinates for the user. I create both tables in the OnCreate method in my DBadapter class &#8220;DatabaseHandler.class&#8221;. The login table works perfectly but the Coordinates table doesn&#8217;t. The program throws &#8220;no such table exists&#8221; when I try to access and run commands on it.<\/p>\n<p><em>Logcat<\/em><\/p>\n<pre><code>01-22 12:42:06.769: E\/AndroidRuntime(854): FATAL EXCEPTION: main\n01-22 12:42:06.769: E\/AndroidRuntime(854): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidbasic12\/com.example.androidbasic12.map}: android.database.sqlite.SQLiteException: no such table: coord (code 1): , while compiling: SELECT * FROM coord WHERE name = name\n01-22 12:42:06.769: E\/AndroidRuntime(854):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)\n01-22 12:42:06.769: E\/AndroidRuntime(854):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)\n01-22 12:42:06.769: E\/AndroidRuntime(854):  at android.app.ActivityThread.access$600(ActivityThread.java:141)\n01-22 12:42:06.769: E\/AndroidRuntime(854):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)\n01-22 12:42:06.769: E\/AndroidRuntime(854):  at android.os.Handler.dispatchMessage(Handler.java:99)\n01-22 12:42:06.769: E\/AndroidRuntime(854):  at android.os.Looper.loop(Looper.java:137)\n01-22 12:42:06.769: E\/AndroidRuntime(854):  at android.app.ActivityThread.main(ActivityThread.java:5039)\n01-22 12:42:06.769: E\/AndroidRuntime(854):  at java.lang.reflect.Method.invokeNative(Native Method)\n01-22 12:42:06.769: E\/AndroidRuntime(854):  at java.lang.reflect.Method.invoke(Method.java:511)\n01-22 12:42:06.769: E\/AndroidRuntime(854):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)\n01-22 12:42:06.769: E\/AndroidRuntime(854):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)\n01-22 12:42:06.769: E\/AndroidRuntime(854):  at dalvik.system.NativeStart.main(Native Method)\n01-22 12:42:06.769: E\/AndroidRuntime(854): Caused by: android.database.sqlite.SQLiteException: no such table: coord (code 1): , while compiling: SELECT * FROM coord WHERE name = name\n01-22 12:42:06.769: E\/AndroidRuntime(854):  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)\n01-22 12:42:06.769: E\/AndroidRuntime(854):  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)\n01-22 12:42:06.769: E\/AndroidRuntime(854):  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)\n01-22 12:42:06.769: E\/AndroidRuntime(854):  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)\n01-22 12:42:06.769: E\/AndroidRuntime(854):  at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:58)\n01-22 12:42:06.769: E\/AndroidRuntime(854):  at android.database.sqlite.SQLiteQuery.(SQLiteQuery.java:37)\n01-22 12:42:06.769: E\/AndroidRuntime(854):  at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)\n01-22 12:42:06.769: E\/AndroidRuntime(854):  at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)\n01-22 12:42:06.769: E\/AndroidRuntime(854):  at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)\n01-22 12:42:06.769: E\/AndroidRuntime(854):  at com.example.androidbasic12.library.DatabaseHandler.getLatLng(DatabaseHandler.java:155)\n01-22 12:42:06.769: E\/AndroidRuntime(854):  at com.example.androidbasic12.library.UserFunctions.getCoords(UserFunctions.java:124)\n01-22 12:42:06.769: E\/AndroidRuntime(854):  at com.example.androidbasic12.map.onCreate(map.java:56)\n01-22 12:42:06.769: E\/AndroidRuntime(854):  at android.app.Activity.performCreate(Activity.java:5104)\n01-22 12:42:06.769: E\/AndroidRuntime(854):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)\n01-22 12:42:06.769: E\/AndroidRuntime(854):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)\n01-22 12:42:06.769: E\/AndroidRuntime(854):  ... 11 more\n<\/code><\/pre>\n<p><em>DatabaseHandler.class<\/em><\/p>\n<pre><code>public class DatabaseHandler extends SQLiteOpenHelper {\n\n\/\/ All Static variables\n\/\/ Database Version\nprivate static final int DATABASE_VERSION = 1;\n\n\/\/ Database Name\nprivate static final String DATABASE_NAME = \"android_api\";\n\n\/\/Carcoord table name\nprivate static final String TABLE_CARCOORD = \"coord\";\n\/\/ carcoord table columns names\nprivate static final String CAR_KEY_ID = \"id\";\nprivate static final String CAR_KEY_NAME = \"name\";\nprivate static final String CAR_KEY_LAT = \"lat\";\nprivate static final String CAR_KEY_LNG = \"lng\";\n\n\n\/\/ Login table name\nprivate static final String TABLE_LOGIN = \"login\";\n\/\/ Login Table Columns names\nprivate static final String KEY_ID = \"id\";\nprivate static final String KEY_NAME = \"name\";\nprivate static final String KEY_EMAIL = \"email\";\nprivate static final String KEY_UID = \"uid\";\nprivate static final String KEY_CREATED_AT = \"created_at\";\n\n\/\/Strings that handles create table querys\nprivate static final String CREATE_LOGIN_TABLE = \"CREATE TABLE \" + TABLE_LOGIN + \"(\"\n        + KEY_ID + \" INTEGER PRIMARY KEY,\" \n        + KEY_NAME + \" TEXT,\"\n        + KEY_EMAIL + \" TEXT UNIQUE,\"\n        + KEY_UID + \" TEXT,\"\n        + KEY_CREATED_AT + \" TEXT\" + \")\";\nprivate static final String CREATE_CARCOORD_TABLE = \"CREATE TABLE \" + TABLE_CARCOORD + \"(\"\n        + CAR_KEY_ID + \" INTEGER PRIMARY KEY,\"\n        + CAR_KEY_NAME + \" TEXT,\"\n        + CAR_KEY_LAT + \" TEXT,\"\n        + CAR_KEY_LNG + \" TEXT\" + \")\";\n\n\/\/Constructor\npublic DatabaseHandler(Context context) {\n    super(context, DATABASE_NAME, null, DATABASE_VERSION);\n}\n\n\/\/ Creating Tables\n@Override\npublic void onCreate(SQLiteDatabase db) {\n    db.execSQL(CREATE_LOGIN_TABLE);\n    db.execSQL(CREATE_CARCOORD_TABLE);\n\n}\n\n\/\/ Upgrading database\n@Override\npublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {\n    \/\/ Drop older table if existed\n    db.execSQL(\"DROP TABLE IF EXISTS \" + TABLE_LOGIN);\n    db.execSQL(\"DROP TABLE IF EXISTS \" + TABLE_CARCOORD);\n\n    \/\/ Create tables again\n    onCreate(db);\n}\n\n\/**\n * Store coordinates in TABLEE_COORDS\n *\/\npublic void addCoord(String name, String lat, String lng){\n    SQLiteDatabase db = this.getWritableDatabase();\n    ContentValues values = new ContentValues();\n    values.put(KEY_NAME, name); \/\/ Name\n    values.put(CAR_KEY_LAT, lat); \/\/ latitude\n    values.put(CAR_KEY_LNG, lng); \/\/Longitude\n\n    \/\/ Inserting row\n    db.insert(TABLE_CARCOORD, null, values);\n    db.close(); \/\/ Closing writable database connection\n}\n\n\/**\n * Storing user details in database\n * *\/\npublic void addUser(String name, String email, String uid, String created_at) {\n    SQLiteDatabase db = this.getWritableDatabase();\n\n    ContentValues values = new ContentValues();\n    values.put(KEY_NAME, name); \/\/ Name\n    values.put(KEY_EMAIL, email); \/\/ Email\n    values.put(KEY_UID, uid); \/\/ Email\n    values.put(KEY_CREATED_AT, created_at); \/\/ Created At\n\n    \/\/ Inserting Row\n    db.insert(TABLE_LOGIN, null, values);\n    db.close(); \/\/ Closing database connection\n}\n\n\/**\n * Getting user data from database\n * *\/\npublic HashMap getUserDetails(){\n    HashMap user = new HashMap();\n    String selectQuery = \"SELECT  * FROM \" + TABLE_LOGIN;\n\n    SQLiteDatabase db = this.getReadableDatabase();\n    Cursor cursor = db.rawQuery(selectQuery, null);\n    \/\/ Move to first row\n    cursor.moveToFirst();\n    if(cursor.getCount() &gt; 0){\n        user.put(\"name\", cursor.getString(1));\n        user.put(\"email\", cursor.getString(2));\n        user.put(\"uid\", cursor.getString(3));\n        user.put(\"created_at\", cursor.getString(4));\n    }\n    cursor.close();\n    db.close();\n    \/\/ return user\n    return user;\n}\n\n\n\/**\n * Getting user login status\n * return true if rows are there in table\n * *\/\npublic int getRowCount() {\n    String countQuery = \"SELECT  * FROM \" + TABLE_LOGIN;\n    SQLiteDatabase db = this.getReadableDatabase();\n    Cursor cursor = db.rawQuery(countQuery, null);\n    int rowCount = cursor.getCount();\n    db.close();\n    cursor.close();\n    \/\/ return row count\n    return rowCount;\n    }\npublic Cursor getUsername(){\n    SQLiteDatabase db = this.getReadableDatabase();\n    String query = (\"SELECT * FROM \" + TABLE_LOGIN + \" WHERE name = \" + KEY_NAME);\n    Cursor cursor = db.rawQuery(query, null);\n    return cursor;\n}\n\npublic Cursor getLatLng(){\n    SQLiteDatabase db = this.getReadableDatabase();\n    String query = (\"SELECT * FROM \" + TABLE_CARCOORD + \" WHERE name = \" + CAR_KEY_NAME);\n    Cursor cursor = db.rawQuery(query, null);\n    return cursor;\n\n}\n\n\n\/**\n * reset table TABLE_CARCOORD\n *\/\npublic void resetCarcoord(){\n    SQLiteDatabase db = this.getWritableDatabase();\n    db.delete(TABLE_CARCOORD, null, null);\n    db.close();\n}\n\/**\n * Re create database\n * Delete all tables and create them again\n * *\/\npublic void resetTables(){\n    SQLiteDatabase db = this.getWritableDatabase();\n    \/\/ Delete All Rows\n    db.delete(TABLE_LOGIN, null, null);\n    db.close();\n}\n<\/code><\/pre>\n<p>}<\/p>\n<p><em>Code snippet from UserFunctions.class where I use the table &#8220;coord&#8221;.<\/em><\/p>\n<pre><code>    \/**\n * Get lat and lng from TABLE_CARCOORDS\n *\/\npublic ArrayList getCoords(Context context, String lat, String lng) {\n    DatabaseHandler db = new DatabaseHandler(context);\n    Cursor cursor = db.getLatLng();\n    ArrayList cordarr = new ArrayList();\n    if (cursor.moveToFirst()){\n        lat = cursor.getString(cursor.getColumnIndex(\"lat\"));\n        lng = cursor.getString(cursor.getColumnIndex(\"lng\"));\n        cordarr.add(lat);\n        cordarr.add(lng);\n    }\ncursor.close();\ndb.close();\nreturn cordarr;\n}\n<\/code><\/pre>\n<p><strong>LogCats after fix<\/strong><\/p>\n<p><em>First problem<\/em><\/p>\n<pre><code>01-22 14:15:07.080: E\/SQLiteDatabase(20980): close() was never explicitly called on database '\/data\/data\/com.example.androidbasic12\/databases\/android_api' \n01-22 14:15:07.080: E\/SQLiteDatabase(20980): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here\n01-22 14:15:07.080: E\/SQLiteDatabase(20980):    at android.database.sqlite.SQLiteDatabase.(SQLiteDatabase.java:2072)\n01-22 14:15:07.080: E\/SQLiteDatabase(20980):    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1126)\n01-22 14:15:07.080: E\/SQLiteDatabase(20980):    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1083)\n01-22 14:15:07.080: E\/SQLiteDatabase(20980):    at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1170)\n01-22 14:15:07.080: E\/SQLiteDatabase(20980):    at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:844)\n01-22 14:15:07.080: E\/SQLiteDatabase(20980):    at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:228)\n01-22 14:15:07.080: E\/SQLiteDatabase(20980):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)\n01-22 14:15:07.080: E\/SQLiteDatabase(20980):    at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:231)\n01-22 14:15:07.080: E\/SQLiteDatabase(20980):    at com.example.androidbasic12.library.DatabaseHandler.getUsername(DatabaseHandler.java:146)\n01-22 14:15:07.080: E\/SQLiteDatabase(20980):    at com.example.androidbasic12.library.UserFunctions.getUname(UserFunctions.java:87)\n01-22 14:15:07.080: E\/SQLiteDatabase(20980):    at com.example.androidbasic12.DashboardActivity.onCreate(DashboardActivity.java:59)\n01-22 14:15:07.080: E\/SQLiteDatabase(20980):    at android.app.Activity.performCreate(Activity.java:4470)\n01-22 14:15:07.080: E\/SQLiteDatabase(20980):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)\n01-22 14:15:07.080: E\/SQLiteDatabase(20980):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)\n01-22 14:15:07.080: E\/SQLiteDatabase(20980):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)\n01-22 14:15:07.080: E\/SQLiteDatabase(20980):    at android.app.ActivityThread.access$600(ActivityThread.java:127)\n01-22 14:15:07.080: E\/SQLiteDatabase(20980):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)\n01-22 14:15:07.080: E\/SQLiteDatabase(20980):    at android.os.Handler.dispatchMessage(Handler.java:99)\n01-22 14:15:07.080: E\/SQLiteDatabase(20980):    at android.os.Looper.loop(Looper.java:137)\n01-22 14:15:07.080: E\/SQLiteDatabase(20980):    at android.app.ActivityThread.main(ActivityThread.java:4511)\n01-22 14:15:07.080: E\/SQLiteDatabase(20980):    at java.lang.reflect.Method.invokeNative(Native Method)\n01-22 14:15:07.080: E\/SQLiteDatabase(20980):    at java.lang.reflect.Method.invoke(Method.java:511)\n01-22 14:15:07.080: E\/SQLiteDatabase(20980):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)\n01-22 14:15:07.080: E\/SQLiteDatabase(20980):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)\n01-22 14:15:07.080: E\/SQLiteDatabase(20980):    at dalvik.system.NativeStart.main(Native Method)\n01-22 14:15:07.080: E\/System(20980): Uncaught exception thrown by finalizer\n01-22 14:15:07.085: E\/System(20980): java.lang.IllegalStateException: Don't have database lock!\n01-22 14:15:07.085: E\/System(20980):    at android.database.sqlite.SQLiteDatabase.verifyLockOwner(SQLiteDatabase.java:2230)\n01-22 14:15:07.085: E\/System(20980):    at android.database.sqlite.SQLiteDatabase$1.entryRemoved(SQLiteDatabase.java:2322)\n01-22 14:15:07.085: E\/System(20980):    at android.database.sqlite.SQLiteDatabase$1.entryRemoved(SQLiteDatabase.java:2318)\n01-22 14:15:07.085: E\/System(20980):    at android.util.LruCache.trimToSize(LruCache.java:197)\n01-22 14:15:07.085: E\/System(20980):    at android.util.LruCache.evictAll(LruCache.java:285)\n01-22 14:15:07.085: E\/System(20980):    at android.database.sqlite.SQLiteDatabase.deallocCachedSqlStatements(SQLiteDatabase.java:2283)\n01-22 14:15:07.085: E\/System(20980):    at android.database.sqlite.SQLiteDatabase.closeClosable(SQLiteDatabase.java:1255)\n01-22 14:15:07.085: E\/System(20980):    at android.database.sqlite.SQLiteDatabase.finalize(SQLiteDatabase.java:2043)\n01-22 14:15:07.085: E\/System(20980):    at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:185)\n01-22 14:15:07.085: E\/System(20980):    at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:168)\n01-22 14:15:07.085: E\/System(20980):    at java.lang.Thread.run(Thread.java:856)\n<\/code><\/pre>\n<p><em>Second error<\/em><\/p>\n<pre><code>01-22 14:15:07.435: E\/AndroidRuntime(20980): FATAL EXCEPTION: main\n01-22 14:15:07.435: E\/AndroidRuntime(20980): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidbasic12\/com.example.androidbasic12.map}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0\n01-22 14:15:07.435: E\/AndroidRuntime(20980):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)\n01-22 14:15:07.435: E\/AndroidRuntime(20980):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)\n01-22 14:15:07.435: E\/AndroidRuntime(20980):    at android.app.ActivityThread.access$600(ActivityThread.java:127)\n01-22 14:15:07.435: E\/AndroidRuntime(20980):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)\n01-22 14:15:07.435: E\/AndroidRuntime(20980):    at android.os.Handler.dispatchMessage(Handler.java:99)\n01-22 14:15:07.435: E\/AndroidRuntime(20980):    at android.os.Looper.loop(Looper.java:137)\n01-22 14:15:07.435: E\/AndroidRuntime(20980):    at android.app.ActivityThread.main(ActivityThread.java:4511)\n01-22 14:15:07.435: E\/AndroidRuntime(20980):    at java.lang.reflect.Method.invokeNative(Native Method)\n01-22 14:15:07.435: E\/AndroidRuntime(20980):    at java.lang.reflect.Method.invoke(Method.java:511)\n01-22 14:15:07.435: E\/AndroidRuntime(20980):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)\n01-22 14:15:07.435: E\/AndroidRuntime(20980):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)\n01-22 14:15:07.435: E\/AndroidRuntime(20980):    at dalvik.system.NativeStart.main(Native Method)\n01-22 14:15:07.435: E\/AndroidRuntime(20980): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0\n01-22 14:15:07.435: E\/AndroidRuntime(20980):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)\n01-22 14:15:07.435: E\/AndroidRuntime(20980):    at java.util.ArrayList.get(ArrayList.java:304)\n01-22 14:15:07.435: E\/AndroidRuntime(20980):    at com.example.androidbasic12.map.onCreate(map.java:57)\n01-22 14:15:07.435: E\/AndroidRuntime(20980):    at android.app.Activity.performCreate(Activity.java:4470)\n01-22 14:15:07.435: E\/AndroidRuntime(20980):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)\n01-22 14:15:07.435: E\/AndroidRuntime(20980):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)\n01-22 14:15:07.435: E\/AndroidRuntime(20980):    ... 11 more\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have a SQLite database with two tables, one for holding Login information, such as Email adress and password, and the other one to hold GPS coordinates for the user. I create both tables in the OnCreate method in my DBadapter class &#8220;DatabaseHandler.class&#8221;. The login table works perfectly but the Coordinates table doesn&#8217;t. The program [&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-5873","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5873","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=5873"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5873\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}