By laalto

2014-02-19 13:31:07 8 Comments

I have created my tables in my SQLiteOpenHelper onCreate() but receive

SQLiteException: no such table


SQLiteException: no such column

errors. Why?


(This is the amalgamated summary of tens of similar questions every week. Attempting to provide a "canonical" community wiki question/answer here so that all those questions can be directed to a good reference.)


@sushant suryawanshi 2018-12-22 11:53:58

Sqlite database override two methods

1) onCreate(): This method invoked only once when the application is start at first time . So it called only once

2)onUpgrade() This method called when we change the database version,then this methods gets invoked.It is used for the alter the table structure like adding new column after creating DB Schema

@venu 2018-10-04 07:30:18

Recheck your query in ur DatabaseHandler/DatabaseManager class(which ever you have took)

@Faxriddin Abdullayev 2018-01-15 07:49:56

onCreate is called for the first time when creation of tables are needed. We need to override this method where we write the script for table creation which is executed by SQLiteDatabase. execSQL method. After executing in first time deployment, this method will not be called onwards.

onUpgrade This method is called when database version is upgraded. Suppose for the first time deployment , database version was 1 and in second deployment there was change in database structure like adding extra column in table. Suppose database version is 2 now.

@phreakhead 2017-09-28 04:52:48

If you forget to provide a "name" string as the second argument to the constructor, it creates an "in-memory" database which gets erased when you close the app.

@Omer Haqqani 2017-08-01 08:08:06

Your database name must end with .db also your query strings must have a terminator (;)

@Enamul Haque 2017-07-04 18:51:09

You can create database & table like

public class DbHelper extends SQLiteOpenHelper {
private static final String DBNAME = "testdatbase.db";
private static final int VERSION = 1;

public DbHelper(Context context) {
    super(context, DBNAME, null, VERSION);
    // TODO Auto-generated constructor stub

public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL("create table BookDb(id integer primary key autoincrement,BookName text,Author text,IssuedOn text,DueDate text,Fine text,Totalfine text");


public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS BookDb");

Note : if you want create another table or add columns or no such table, just increment the VERSION

@Bharat Lalwani 2017-07-04 10:14:04

no such table found is mainly when you have not opened the SQLiteOpenHelper class with getwritabledata() and before this you also have to call make constructor with databasename & version. And OnUpgrade is called whenever there is upgrade value in version number given in SQLiteOpenHelper class.

Below is the code snippet (No such column found may be because of spell in column name):

public class database_db {
    entry_data endb;
    String file_name="Record.db";
    SQLiteDatabase sq;
    public database_db(Context c)
        endb=new entry_data(c, file_name, null, 8);
    public database_db open()
        return this;
    public Cursor getdata(String table)
        return sq.query(table, null, null, null, null, null, null);
    public long insert_data(String table,ContentValues value)
        return sq.insert(table, null, value);
    public void close()
    public void delete(String table)
class entry_data extends SQLiteOpenHelper

    public entry_data(Context context, String name, SQLiteDatabase.CursorFactory factory,
                      int version) {
        super(context, name, factory, version);
        // TODO Auto-generated constructor stub

    public void onCreate(SQLiteDatabase sqdb) {
        // TODO Auto-generated method stub

        sqdb.execSQL("CREATE TABLE IF NOT EXISTS 'YOUR_TABLE_NAME'(Column_1 text not null,Column_2 text not null);");


    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


@Kush 2017-03-06 06:08:19

May be I am too late but I would like to share my short and sweet answer. Please check Answer for a same problem. It will definitely help you. No more deep specifications.

If you are confident about syntax for creating table, than it may happen when you add new column in your same table, for that...

1) Uninstall from your device and run it again.


2) Setting -> app -> ClearData


3) Change DATABASE_VERSION in your "DatabaseHandler" class (If you have added new column than it will upgrade automatically)

public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);


4) Change DATABASE_NAME in your "DatabaseHandler" class (I faced same problem. But I succeed by changing DATABASE_NAME.)

@Behzad 2017-10-31 08:07:26

I have my own DB and using SQLiteAssetHelper class. So, I did create the DB by sql script before and the db was created. By using the SQLiteAssetHelper it couldn't copy the DB until uninstalling the app form the emulator or device, because it was a db with the same version.

@Aun 2014-11-13 19:37:37

To further add missing points here, as per the request by Jaskey

Database version is stored within the SQLite database file.

catch is the constructor

SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)

So when the database helper constructor is called with a name (2nd param), platform checks if the database exists or not and if the database exists, it gets the version information from the database file header and triggers the right call back

As already explained in the older answer, if the database with the name doesn't exists, it triggers onCreate.

Below explanation explains onUpgrade case with an example.

Say, your first version of application had the DatabaseHelper (extending SQLiteOpenHelper) with constructor passing version as 1 and then you provided an upgraded application with the new source code having version passed as 2, then automatically when the DatabaseHelper is constructed, platform triggers onUpgrade by seeing the file already exists, but the version is lower than the current version which you have passed.

Now say you are planing to give a third version of application with db version as 3 (db version is increased only when database schema is to be modified). In such incremental upgrades, you have to write the upgrade logic from each version incrementally for a better maintainable code

Example pseudo code below:

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  switch(oldVersion) {
    case 1:
       //upgrade logic from version 1 to 2
    case 2:
       //upgrade logic from version 2 to 3
    case 3:
       //upgrade logic from version 3 to 4
       throw new IllegalStateException(
                "onUpgrade() with unknown oldVersion " + oldVersion);

Notice the missing break statement in case 1 and 2. This is what I mean by incremental upgrade.

Say if the old version is 2 and new version is 4, then the logic will upgrade the database from 2 to 3 and then to 4

If old version is 3 and new version is 4, it will just run the upgrade logic for 3 to 4

@joe p 2014-11-14 18:19:29

I think you want your switch(newVersion) to be switch(oldVersion) instead. You may also want to verify that newVersion is 4 (and not 5, or 3; because your logic is assuming the new version should be 4).As it is, if the old version is 2 and new version is 5, you'll hit the case 4: and upgrade from 3 to 4 (which should probably not be expected behavior).

@Aun 2014-11-14 18:52:56

right - typo.. but if new version is 5 -> then it will always throw IllegalStateException and developer will be fixing it by adding case 5..

@Paramvir Singh 2015-02-05 09:41:14

What if the user upgrades his app from version 2 to 3 only? In that case also, all the cases upto case 4 will run.

@Habeeb Perwad 2015-02-19 12:06:35

@param user cannot do that. He can upgrade 2 to latest (here 4) only.

@jeet parmar 2015-06-16 13:18:37


  1. When we create DataBase at a first time (i.e Database is not exists) onCreate() create database with version which is passed in SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)

  2. onCreate() method is creating the tables you’ve defined and executing any other code you’ve written. However, this method will only be called if the SQLite file is missing in your app’s data directory (/data/data/your.apps.classpath/databases).

  3. This method will not be called if you’ve changed your code and relaunched in the emulator. If you want onCreate() to run you need to use adb to delete the SQLite database file.


  1. SQLiteOpenHelper should call the super constructor.
  2. The onUpgrade() method will only be called when the version integer is larger than the current version running in the app.
  3. If you want the onUpgrade() method to be called, you need to increment the version number in your code.

@abarisone 2015-06-16 13:35:27

Could you please elaborate more your answer adding a little more description about the solution you provide?

@Vishwa Pratap 2016-12-30 16:54:01

Sqliteopenhelper's method have methods create and upgrade,create is used when any table is first time created and upgrade method will called everytime whenever table's number of column is changed.

@Roger Huang 2017-04-26 03:01:09

onUpgrade method is called when the database version increases, not when the number of column is changed. Ref:…, int, int)

@JibinNajeeb 2016-10-25 04:35:37

Points to remember when extending SQLiteOpenHelper

  1. super(context, DBName, null, DBversion); - This should be invoked first line of constructor
  2. override onCreate and onUpgrade (if needed)
  3. onCreate will be invoked only when getWritableDatabase() or getReadableDatabase() is executed. And this will only invoked once when a DBName specified in the first step is not available. You can add create table query on onCreate method
  4. Whenever you want to add new table just change DBversion and do the queries in onUpgrade table or simply uninstall then install the app.

@Nand gopal 2016-03-11 11:32:27

Uninstall your application from the emulator or device. Run the app again. (OnCreate() is not executed when the database already exists)

@laalto 2014-02-19 13:31:07

SQLiteOpenHelper onCreate() and onUpgrade() callbacks are invoked when the database is actually opened, for example by a call to getWritableDatabase(). The database is not opened when the database helper object itself is created.

SQLiteOpenHelper versions the database files. The version number is the int argument passed to the constructor. In the database file, the version number is stored in PRAGMA user_version.

onCreate() is only run when the database file did not exist and was just created. If onCreate() returns successfully (doesn't throw an exception), the database is assumed to be created with the requested version number. As an implication, you should not catch SQLExceptions in onCreate() yourself.

onUpgrade() is only called when the database file exists but the stored version number is lower than requested in constructor. The onUpgrade() should update the table schema to the requested version.

When changing the table schema in code (onCreate()), you should make sure the database is updated. Two main approaches:

  1. Delete the old database file so that onCreate() is run again. This is often preferred at development time where you have control over the installed versions and data loss is not an issue. Some ways to to delete the database file:

    • Uninstall the application. Use the application manager or adb uninstall from shell.

    • Clear application data. Use the application manager.

  2. Increment the database version so that onUpgrade() is invoked. This is slightly more complicated as more code is needed.

    • For development time schema upgrades where data loss is not an issue, you can just use execSQL("DROP TABLE IF EXISTS <tablename>") in to remove your existing tables and call onCreate() to recreate the database.

    • For released versions, you should implement data migration in onUpgrade() so your users don't lose their data.

@bCliks 2014-09-14 14:29:46

@Laalto //data migration in onUpgrade()// Can you please explain about this.

@laalto 2014-09-14 15:27:42

@bala Not in the scope of this question/answer. If you have a question, feel free to post it as a question.

@Jaskey 2014-10-13 11:20:26

"onUpgrade() is only called when the database file exists but the stored version number is lower than requested in constructor. " Would you please explain for this a little bit more? Do you mean the when the client create a DBHelper, it should know the db version? How can the client pass a right db version without knowing the db ?

@laalto 2014-10-13 11:26:32

@Jaskey The version number is for your code i.e. what schema version the code expects to run against. If the file is older (from a previous version of your app), it needs to be upgraded.

@Jaskey 2014-10-13 11:30:02

So, I need to hard code the DB VERSION in the SQLiteHelper each time I modify the schema, so that when the old app runs and get the db connection and find it is old, and then onUpgrade will be trgiigered instead of onCreate, is this right?

@laalto 2014-10-13 11:32:01

When you update your database schema to an incompatible version then yes, you need to update the version number to distinguish incompatible database files. onCreate() is never triggered if the database already exists.

@Jaskey 2014-10-13 11:56:01

Thank you ! This make sense to me. Please verify if I understand well.So we need to do 1. every time we update the schema, modify the DB_VERSION variable(hard code). 2. In onUpdate(), check every old version and do proper data migration . And then when a user update their app(they have old db files), onUpgrade will be Triggered, and if the user is newly install, onCreate() is triggered.

Related Questions

Sponsored Content

25 Answered Questions

[SOLVED] Is there a way to run Python on Android?

9 Answered Questions

[SOLVED] Improve INSERT-per-second performance of SQLite?

4 Answered Questions

[SOLVED] When the SQLiteOpenHelper onCreate method is called?

4 Answered Questions

[SOLVED] SQLiteOpenHelper onUpgrade() Confusion Android

5 Answered Questions

1 Answered Questions

[SOLVED] SQLiteOpenHelper problems

1 Answered Questions

[SOLVED] SQLiteOpenHelper onCreate running twice?

3 Answered Questions

[SOLVED] correct use of SQLiteOpenHelper (and some consultation)

0 Answered Questions

onUpgrade method from SQLiteOpenHelper never called when replacing the database file

  • 2013-03-30 12:49:35
  • Charlie
  • 328 View
  • 2 Score
  • 0 Answer
  • Tags:   android sqlite

Sponsored Content