Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8447649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:13:53+00:00 2026-06-10T10:13:53+00:00

Im trying to call onUpgrade() to update my Table with few columns. however, when

  • 0

Im trying to call onUpgrade() to update my Table with few columns. however, when i run the app I get an error on the insert statement saying columns do not exist in this table (the columns i try to add in the onUpgrade(). I would really appreciate your help.
Here’s my SQLite helper class:
//UPDATED AND WORKING

public class TasksSQLiteOpenHelper extends SQLiteOpenHelper {
public static final int VERSION = 3;
public static final String DB_NAME = "tasks_db.sqlite";
public static final String TASKS_TABLE = "tasks";
public static final String TASK_ID = "id";
public static final String TASK_NAME = "name";
public static final String TASK_RESPONSIBLE = "responsible";
public static final String TASK_PRIORITY = "priority";
public static final String TASK_DATETIME = "taskdatetime";


public TasksSQLiteOpenHelper(Context context) {
    super(context, DB_NAME, null, VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    createTables(db);
}

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

    Log.v("test",Integer.toString(oldVersion));
    Log.v("test", Integer.toString(newVersion));
switch(oldVersion){

case 1: db.execSQL("ALTER TABLE "+ TASKS_TABLE +" ADD COLUMN "+ TASK_RESPONSIBLE +" TEXT");
        db.execSQL("ALTER TABLE "+ TASKS_TABLE +" ADD COLUMN "+ TASK_PRIORITY +" TEXT");
        Log.v("test","res + pri created");
        break;

case 2: db.execSQL("ALTER TABLE "+ TASKS_TABLE +" ADD COLUMN "+ TASK_DATETIME +" TEXT");
        Log.v("test","date created");
        break;
    }
}

    protected void createTables(SQLiteDatabase db) {
        db.execSQL(
        "create table " + TASKS_TABLE+" (" + 
        TASK_ID + " integer primary key,"+ 
        TASK_NAME + " text"+ 
        ");"
        );
}

}
and here’s my Application class //UPDATED AND WORKING SOLUTION

    public class TaskManagerApplication extends Application {

private ArrayList<Task> currentTasks;
public SQLiteDatabase database;

@Override
public void onCreate() {
    super.onCreate();
    TasksSQLiteOpenHelper helper = new TasksSQLiteOpenHelper(this);
    database = helper.getWritableDatabase();
    if (null == currentTasks) {
        loadTasks();
    }
}
private void loadTasks() {
    currentTasks = new ArrayList<Task>();
    Cursor tasksCursor = database.query(TASKS_TABLE, new String[] {TASK_ID, TASK_NAME,TASK_RESPONSIBLE,TASK_PRIORITY,TASK_DATETIME}, null, null, null, null, null);
    tasksCursor.moveToFirst();
    Task t; 
    if (! tasksCursor.isAfterLast()) {
        do {
            long id = tasksCursor.getLong(0);
            String name  = tasksCursor.getString(1);
            String responsible  = tasksCursor.getString(2);
            String priority  = tasksCursor.getString(3);
            String datetime = tasksCursor.getString(4);
            t = new Task(name, priority, responsible, datetime);
            t.setId(id);
            currentTasks.add(t);
        } while (tasksCursor.moveToNext());
    }
    tasksCursor.close();
}

public void setCurrentTask(ArrayList<Task> currentTask) {
    this.currentTasks = currentTask; // set tasks in array
}

public ArrayList<Task> getCurrentTasks() {
    return currentTasks; //call back tasks
}

public void addTask(Task t) { // add new t of type Task
    assert(null != t); //check for null entries - avoid crashing
    ContentValues values = new ContentValues();
    values.put(TASK_NAME, t.getName());
    values.put(TASK_PRIORITY, t.getPriority());
    values.put(TASK_RESPONSIBLE, t.getResponsible());
    values.put(TASK_DATETIME, t.getDatetime());
    //Log.v("test", TASK_DATETIME);
    t.setId(database.insert(TASKS_TABLE, null, values));
    currentTasks.add(t);
}

public void remove(long id){
    //remove statement from table using where statement id
    database.delete(TASKS_TABLE, TASK_ID +" ="+ id, null);
}

}

also here’s my logcat output

    08-28 16:30:41.170: I/Database(8931): sqlite returned: error code = 1, msg = table      tasks has no column named datetime
    08-28 16:30:41.190: E/Database(8931): Error inserting datetime=Aug 28, 2012 4:30:41    PM responsible=Me priority=Medium name=go chi
    08-28 16:30:41.190: E/Database(8931): android.database.sqlite.SQLiteException: table   tasks has no column named datetime: , while compiling: INSERT INTO tasks(datetime,     responsible, priority, name) VALUES(?, ?, ?, ?);
    08-28 16:30:41.190: E/Database(8931):   at     android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
    08-28 16:30:41.190: E/Database(8931):   at  android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
    08-28 16:30:41.190: E/Database(8931):   at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
    08-28 16:30:41.190: E/Database(8931):   at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
    08-28 16:30:41.190: E/Database(8931):   at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:41)
    08-28 16:30:41.190: E/Database(8931):   at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1290)
    08-28 16:30:41.190: E/Database(8931):   at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1710)
  08-28 16:30:41.190: E/Database(8931):     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1567)
  08-28 16:30:41.190: E/Database(8931):     at com.id11078995.ghnaim.exercise1.TaskManagerApplication.addTask(TaskManagerApplication.java:64)
  08-28 16:30:41.190: E/Database(8931):     at com.id11078995.ghnaim.exercise1.AddTaskActivity.addTask(AddTaskActivity.java:59)
  08-28 16:30:41.190: E/Database(8931):     at com.id11078995.ghnaim.exercise1.AddTaskActivity.onOptionsItemSelected(AddTaskActivity.java:134)
  08-28 16:30:41.190: E/Database(8931):     at and roid.app.Activity.onMenuItemSelected(Activity.java:2211)
  08-28 16:30:41.190: E/Database(8931):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:775)
  08-28 16:30:41.190: E/Database(8931):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
  08-28 16:30:41.190: E/Database(8931):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:860)
  08-28 16:30:41.190: E/Database(8931):     at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532)
  08-28 16:30:41.190: E/Database(8931):     at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
  08-28 16:30:41.190: E/Database(8931):     at android.view.View$PerformClick.run(View.java:9152)
  08-28 16:30:41.190: E/Database(8931):     at android.os.Handler.handleCallback(Handler.java:587)
  08-28 16:30:41.190: E/Database(8931):     at android.os.Handler.dispatchMessage(Handler.java:92)
  08-28 16:30:41.190: E/Database(8931):     at android.os.Looper.loop(Looper.java:130)
  08-28 16:30:41.190: E/Database(8931):     at android.app.ActivityThread.main(ActivityThread.java:3691)
  08-28 16:30:41.190: E/Database(8931):     at java.lang.reflect.Method.invokeNative(Native Method)
  08-28 16:30:41.190: E/Database(8931):     at java.lang.reflect.Method.invoke(Method.java:507)
  08-28 16:30:41.190: E/Database(8931):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
  08-28 16:30:41.190: E/Database(8931):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
  08-28 16:30:41.190: E/Database(8931):     at dalvik.system.NativeStart.main(Native Method)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T10:13:55+00:00Added an answer on June 10, 2026 at 10:13 am

    If there is no need to save the old data, I think that is a much easier way to update your database:

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      Log.w(MySQLiteHelper.class.getName(),
        "Upgrading database from version " + oldVersion + " to "
            + newVersion + ", which will destroy all old data");
      db.execSQL("DROP TABLE IF EXISTS " + TABLE_TASKS);
      onCreate(db);
    }
    
    @Override
    public void onCreate(SQLiteDatabase database) {
      database.execSQL(DATABASE_CREATE);
    }
    

    Your final String DATABASE_CREATE includes all fields. Remove the “NOT NULL” and “autoincrement” in the primary key definition.

    • clean your project
    • uninstall your application
    • reinstall it again.

    For further information: Vogella

    Try this solution without losing the data:

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      Log.w(MySQLiteHelper.class.getName(),
        "Upgrading database from version " + oldVersion + " to "
            + newVersion);
      boolean lDateTimeExists = isColumnExisting(TASK_DATETIME);
      boolean lResponsible = isColumnExisting(TASK_RESPONSIBLE);
      boolean lPrio = isColumnExisting(TASK_PRIORITY);
      if (!lDateTime){
        db.execSQL("ALTER TABLE "+ TASKS_TABLE +" ADD COLUMN "+ TASK_DATETIME +" TEXT"); 
      }
      if (!lResponsible){
        db.execSQL("ALTER TABLE "+ TASKS_TABLE +" ADD COLUMN "+ TASK_RESPONSIBLE);       
      }
      if (!lPrio){
        db.execSQL("ALTER TABLE "+ TASKS_TABLE +" ADD COLUMN "+ TASK_PRIORITY +"   TEXT");       
      }
    }
    
    private boolean isColumnExisting(String pColumnName){
      try{
        db.execSQL("SELECT "+pColumnName+" from "+TABLE_TASKS);
      }catch(Exception e){
        return false;
      }
      return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to call the SQL statement below but get the following error: System.Data.SqlClient.SqlException:
When trying to call this function in my code i get the error in
When trying to call my ToString() method I get the above error message. I'm
I get this error trying call my service in WcfTestClient My config: <services> <service
Trying to call a SAP SOAP Web Service from a generated sudzc app shows
I trying to call some ejb bean method from tread. and getting error :
Scenario : Trying to call the .AttachAll method on a table in my LinqToSql
I am trying to insert rows into a SQLite database within Android. A call
When trying to call Close or Dispose on an SqlDataReader i get a timeout
I am trying to call a webservice using ssl. How do i get the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.