Karl Caday Karl Caday - 2 months ago 3x
Java Question

How to add column on existing table in android SQLite?

how to add column in the existing table LOGIN. here's my sample code.

this is my DataBaseAdapter class:

public class DataBaseHelper extends SQLiteOpenHelper
public DataBaseHelper(Context context, String name,CursorFactory factory, int version)
super(context, name, factory, version);
// Called when no database exists in disk and the helper class needs
// to create a new one.
public void onCreate(SQLiteDatabase _db)

// Called when there is a database version mismatch meaning that the version
// of the database on disk needs to be upgraded to the current version.
public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion)
// Log the version upgrade.
Log.w("TaskDBAdapter", "Upgrading from version " +_oldVersion + " to " +_newVersion + ", which will destroy all old data");

// Upgrade the existing database to conform to the new version. Multiple
// previous versions can be handled by comparing _oldVersion and _newVersion
// values.
// The simplest case is to drop the old table and create a new one.
// Create a new one.

this is my LoginDataBaseAdapter

public class LoginDataBaseAdapter
static final String DATABASE_NAME = "login.db";
static final int DATABASE_VERSION = 1;
public static final int NAME_COLUMN = 1;
// TODO: Create public field for each column in your table.
// SQL Statement to create a new database.
static final String DATABASE_CREATE = "create table "+"LOGIN"+
"( " +"ID"+" integer primary key autoincrement,"+ "USERNAME text,PASSWORD text); ";
// Variable to hold the database instance
public SQLiteDatabase db;
// Context of the application using the database.
private final Context context;
// Database open/upgrade helper
private DataBaseHelper dbHelper;
public LoginDataBaseAdapter(Context _context)
context = _context;
dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
public LoginDataBaseAdapter open() throws SQLException
db = dbHelper.getWritableDatabase();
return this;
public void close()

public SQLiteDatabase getDatabaseInstance()
return db;

public void insertEntry(String userName,String password)
ContentValues newValues = new ContentValues();
// Assign values for each row.
newValues.put("USERNAME", userName);

// Insert the row into your table
db.insert("LOGIN", null, newValues);
///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
public int deleteEntry(String UserName)
//String id=String.valueOf(ID);
String where="USERNAME=?";
int numberOFEntriesDeleted= db.delete("LOGIN", where, new String[]{UserName}) ;
// Toast.makeText(context, "Number fo Entry Deleted Successfully : "+numberOFEntriesDeleted, Toast.LENGTH_LONG).show();
return numberOFEntriesDeleted;
public String getSinlgeEntry(String userName1)
Cursor cursor=db.query("LOGIN", null, " USERNAME=?", new String[]{userName1}, null, null, null);
if(cursor.getCount()<1) // UserName Not Exist
return "NOT EXIST";
String password= cursor.getString(cursor.getColumnIndex("PASSWORD"));
return password;
public void updateEntry(String userName,String password)
// Define the updated row content.
ContentValues updatedValues = new ContentValues();
// Assign values for each row.
updatedValues.put("USERNAME", userName);

String where="USERNAME = ?";
db.update("LOGIN",updatedValues, where, new String[]{userName});

how can I add the columns FIRSTNAME(from TextView), LASTNAME(from TextView), DEPARTMENT(from Spinner).


You'll have to update your SQLite database version first of all, then that will run your onUpgrade() method, which will drop all of your data. Then the table will be remade with the new schema that you have defined in your DATABASE_CREATE string.

So the main issue that this comes with is that you'll have to find a way to recover the data that is already existing in the table as the table will be dropped. You do get to run onUpgrade before this happens though so use this method as a point to save any data you need to from the database.

static final int DATABASE_VERSION = 2;

and update your database creation string.