java - How to add column on existing table in android SQLite? -


how add column in existing table login. here's sample code.

this 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 , helper class needs     // create new one.     @override     public void oncreate(sqlitedatabase _db)      {             _db.execsql(logindatabaseadapter.database_create);      }     // called when there database version mismatch meaning version     // of database on disk needs upgraded current version.     @override     public void onupgrade(sqlitedatabase _db, int _oldversion, int _newversion)      {             // log version upgrade.             log.w("taskdbadapter", "upgrading version " +_oldversion + " " +_newversion + ", destroy old data");               // upgrade existing database conform new version. multiple             // previous versions can handled comparing _oldversion , _newversion             // values.             // simplest case drop old table , create new one.             _db.execsql("drop table if exists " + "template");             // create new one.             oncreate(_db);     } 

this 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 each column in table.         // sql statement create new database.         static final string database_create = "create table "+"login"+                                      "( " +"id"+" integer primary key autoincrement,"+ "username  text,password text); ";         // variable hold database instance         public  sqlitedatabase db;         // context of application using 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()          {             db.close();         }          public  sqlitedatabase getdatabaseinstance()         {             return db;         }          public void insertentry(string username,string password)         {            contentvalues newvalues = new contentvalues();             // assign values each row.             newvalues.put("username", username);             newvalues.put("password",password);              // insert row table             db.insert("login", null, newvalues);             ///toast.maketext(context, "reminder 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 : "+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             {                 cursor.close();                 return "not exist";             }             cursor.movetofirst();             string password= cursor.getstring(cursor.getcolumnindex("password"));             cursor.close();             return password;                         }         public void  updateentry(string username,string password)         {             // define updated row content.             contentvalues updatedvalues = new contentvalues();             // assign values each row.             updatedvalues.put("username", username);             updatedvalues.put("password",password);              string where="username = ?";             db.update("login",updatedvalues, where, new string[]{username});                        }        } 

how can add columns firstname(from textview), lastname(from textview), department(from spinner).

you'll have update sqlite database version first of all, run onupgrade() method, drop of data. table remade new schema have defined in database_create string.

so main issue comes you'll have find way recover data existing in table table dropped. run onupgrade before happens though use method point save data need database.

static final int database_version = 2; 

and update database creation string.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -