Introduction
Android SQLite is a powerful and popular database system used to store data on Android devices. Unfortunately, it is not uncommon to encounter errors when working with SQLite databases. One of the most common errors is “no such table,” which indicates that the database does not contain the table that is being referenced. This error can be caused by a variety of issues, including incorrect syntax, missing tables, or corrupt databases. In this article, we will discuss how to troubleshoot and solve this error. We will cover topics such as verifying the table exists, checking the syntax, and restoring a backup. By the end of this article, you should have a better understanding of how to solve the “no such table” error in Android SQLite.
Solution
The most likely cause of this error is that the table does not exist in the database. To resolve this issue, you need to create the table in the database. This can be done using an SQL query such as CREATE TABLE
Your onCreate()
has SQL syntax errors:
String DATABASE_CREATE_FIRST ="CREATE TABLE IF NOT EXISTS"+ NAME_TABLE
+"(" + KEY_NAME +TEXT_TYPE+ COMMA_SEP +
KEY_HR+TEXT_TYPE+ COMMA_SEP +
KEY_IBI +TEXT_TYPE+ COMMA_SEP+
KEY_HMIN+TEXT_TYPE+ COMMA_SEP+
KEY_HMAX+TEXT_TYPE+ COMMA_SEP+
KEY_IMIN+TEXT_TYPE+ COMMA_SEP+
KEY_IMAX+TEXT_TYPE+ COMMA_SEP+
KEY_BMIN+TEXT_TYPE+ COMMA_SEP+
KEY_BMAX+TEXT_TYPE+ COMMA_SEP+
")";
-
No space between
EXISTS
and table name. -
Extra comma after last column specification.
So:
String DATABASE_CREATE_FIRST ="CREATE TABLE IF NOT EXISTS "+ NAME_TABLE
+"(" + KEY_NAME +TEXT_TYPE+ COMMA_SEP +
KEY_HR+TEXT_TYPE+ COMMA_SEP +
KEY_IBI +TEXT_TYPE+ COMMA_SEP+
KEY_HMIN+TEXT_TYPE+ COMMA_SEP+
KEY_HMAX+TEXT_TYPE+ COMMA_SEP+
KEY_IMIN+TEXT_TYPE+ COMMA_SEP+
KEY_IMAX+TEXT_TYPE+ COMMA_SEP+
KEY_BMIN+TEXT_TYPE+ COMMA_SEP+
KEY_BMAX+TEXT_TYPE+
")";
Clean up your app data when changing the db schema to force onCreate()
to be called again.
Edited to add after comments: Your onUpgrade()
is also broken. It fails to do any modifications to an existing database and will cause infinite recursion (causing StackoverflowError
). Easy but data-losing solution is to DROP TABLE tablename
and then call onCreate()
.
4
solved Android::SQLite Error: no such table
If you are getting an error message that says “no such table” when trying to access an SQLite database on your Android device, then you are likely dealing with a corrupted database. This can happen for a variety of reasons, such as an application crash, a power outage, or a hardware failure. Fortunately, there are a few steps you can take to try and fix the issue.
Step 1: Check the Database File
The first step is to check the database file itself. If the file is missing or corrupted, then the error message will appear. To check the file, open the file explorer on your device and navigate to the folder where the database is stored. If the file is missing, then you will need to restore it from a backup. If the file is present but corrupted, then you will need to delete it and create a new one.
Step 2: Check the Database Structure
If the database file is present and intact, then the next step is to check the database structure. This can be done by opening the database in a SQLite editor and examining the tables and columns. If any of the tables or columns are missing or corrupted, then you will need to recreate them. This can be done by running the appropriate SQL commands in the editor.
Step 3: Check the Application Code
If the database structure is intact, then the next step is to check the application code. This is the code that is used to access the database and perform operations on it. If the code is incorrect or outdated, then it can cause the “no such table” error. To check the code, open the application in a code editor and examine the database access code. If any of the code is incorrect or outdated, then you will need to update it.
Conclusion
If you are getting an error message that says “no such table” when trying to access an SQLite database on your Android device, then you are likely dealing with a corrupted database. To fix the issue, you will need to check the database file, the database structure, and the application code. If any of these are incorrect or outdated, then you will need to update them in order to resolve the issue.