0

I'm new to SQLite and I found a sample program that has an SQLite. I want to update the data but whenever I change the values, it doesn't update. Here's the code:

DatabaseHelper.Java

package com.example.ttg;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "employee_directory";

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    /*
     * Create the employee table and populate it with sample data.
     * In step 6, we will move these hardcoded statements to an XML document.
     */
    String sql = "CREATE TABLE IF NOT EXISTS employee (" +
                    "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
                    "firstName TEXT, " +
                    "lastName TEXT, " +
                    "title TEXT, " +
                    "officePhone TEXT, " +
                    "cellPhone TEXT, " +
                    "email TEXT, " +
                    "managerId INTEGER)";
    db.execSQL(sql);

    ContentValues values = new ContentValues();

    values.put("firstName", "John");
    values.put("lastName", "Smith");
    values.put("title", "CEO");
    values.put("officePhone", "617-219-2001");
    values.put("cellPhone", "617-456-7890");
    values.put("email", "jsmith@email.com");
    db.insert("employee", "lastName", values);

    values.put("firstName", "Robert");
    values.put("lastName", "Jackson");
    values.put("title", "VP Engineering");
    values.put("officePhone", "617-219-3333");
    values.put("cellPhone", "781-444-2222");
    values.put("email", "rjackson@email.com");
    values.put("managerId", "1");
    db.insert("employee", "lastName", values);

    values.put("firstName", "Marie");
    values.put("lastName", "Potter");
    values.put("title", "VP Sales");
    values.put("officePhone", "617-219-2002");
    values.put("cellPhone", "987-654-3210");
    values.put("email", "mpotter@email.com");
    values.put("managerId", "1");
    db.insert("employee", "lastName", values);

    values.put("firstName", "Lisa");
    values.put("lastName", "Jordan");
    values.put("title", "VP Marketing");
    values.put("officePhone", "617-219-2003");
    values.put("cellPhone", "987-654-7777");
    values.put("email", "ljordan@email.com");
    values.put("managerId", "2");
    db.insert("employee", "lastName", values);

    values.put("firstName", "Christophe");
    values.put("lastName", "Coenraets");
    values.put("title", "Evangelist");
    values.put("officePhone", "617-219-0000");
    values.put("cellPhone", "617-666-7777");
    values.put("email", "ccoenrae@adobe.com");
    values.put("managerId", "2");
    db.insert("employee", "lastName", values);

    values.put("firstName", "Paula");
    values.put("lastName", "Brown");
    values.put("title", "Director Engineering");
    values.put("officePhone", "617-612-0987");
    values.put("cellPhone", "617-123-9876");
    values.put("email", "pbrown@email.com");
    values.put("managerId", "2");
    db.insert("employee", "lastName", values);

    values.put("firstName", "Mark");
    values.put("lastName", "Taylor");
    values.put("title", "Lead Architect");
    values.put("officePhone", "617-444-1122");
    values.put("cellPhone", "617-555-3344");
    values.put("email", "mtaylor@email.com");
    values.put("managerId", "2");
    db.insert("employee", "lastName", values);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS employees");
    onCreate(db);
}

}

Example: I tried to change John Smith to another name but it doesn't update. How can I update that?

Thanks

1 Answers1

0

If you want onUpgrade to be called, you need to increment the database version.

public class DatabaseHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "employee_directory";
    public static final int DATABASE_VERSION = 2; //increment this version number: onUpgrade will be called once

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    ...
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //newVersion is 2 here
        db.execSQL("DROP TABLE IF EXISTS employees");
        onCreate(db);
    }
}
Pycpik
  • 2,316
  • 1
  • 16
  • 21
  • So the logic here is whenever I update I will add another DATABASE_VERSION? – Nikko del Valle Nov 15 '14 at 13:06
  • If you want to update the initial data, you have to call `onCreate`. There are two ways: increment your database version to call `onUpgrade` or delete your database (delete data of the app or uninstall it) to force `onCreate` – Pycpik Nov 15 '14 at 14:17
  • Otherwise, create a method to update data after as @Jibran Khan said – Pycpik Nov 15 '14 at 14:20