Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DatabaseHelper extends SQLiteOpenHelper {
- private static final String DATABASE_NAME="upgrade";
- private static final int DATABASE_VERSION=2;
- //details table details
- public static final String TABLE_NAME_Details="details";
- public static final String USERNAME="USERNAME";
- public static final String PASSWORD="PASSWORD";
- public static final String Name="Name"; //newly added column in version 2
- //create table statements for version 2
- public static final String Create_Table_Details = "CREATE TABLE "
- + TABLE_NAME_Details + " (" + USERNAME + " TEXT PRIMARY KEY,"
- + PASSWORD + " TEXT ,"
- + Name + " TEXT"
- +")";
- /* creation for version 1
- private static final String Create_Table_Details = "CREATE TABLE " + TABLE_NAME_Details + "("
- + USERNAME + " TEXT PRIMARY KEY,"
- + PASSWORD + " TEXT"
- + ")";*/
- //Alter table statements FOR onUpgrade()
- private static final String ALTER_Details = "ALTER TABLE" + TABLE_NAME_Details + "ADD COLUMN" + Name + "TEXT";
- public DatabaseHelper(Context context)
- {
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- try{
- db.execSQL(Create_Table_Details);
- Log.d("database","installed successfully");
- }catch (Exception e)
- {
- e.printStackTrace();
- Log.e("/test","Exception due to"+e.toString());
- }
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
- try{
- Log.d("eEmp/DBUpgrade", "oldVersion:" + oldVersion + ", NewVersion:" + newVersion);
- /* db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME_Details);
- onCreate(db);*/
- if(oldVersion < 2)
- {
- try{
- Cursor c = db.rawQuery("Select * from " + TABLE_NAME_Details + " WHERE 1 = 0", null);
- if (c.getColumnIndex(Name) == -1) {
- db.execSQL(ALTER_Details);
- }
- if (c != null)
- c.close();
- }catch(Exception upgradeexp)
- {
- upgradeexp.printStackTrace();
- Log.e("/test","Exception due to" + upgradeexp.toString());
- }
- }
- }catch(Exception dbUpgradeExp)
- {
- dbUpgradeExp.printStackTrace();
- Log.e("/test","Exception due to" + dbUpgradeExp.toString());
- }
- /* if(oldVersion < 2)
- {
- db.execSQL(ALTER_Details);
- }*/
- }
- //inserting login data to database
- public boolean insertData(String username, String password,String name)
- {
- try{
- SQLiteDatabase db = this.getWritableDatabase();
- ContentValues contentValues=new ContentValues();
- contentValues.put(USERNAME,username);
- contentValues.put(PASSWORD,password);
- contentValues.put(Name,name);
- long result=db.insert(TABLE_NAME_Details, null, contentValues);
- if(result==-1)
- return false;
- else
- return true;
- }catch(Exception e)
- {
- e.printStackTrace();
- Log.e("/test","Exception due to"+e.toString());
- return false;
- }
- }
- //getting data from database
- public Cursor getAllData()
- {
- SQLiteDatabase db = this.getWritableDatabase();
- Cursor res = db.rawQuery("select * from "+TABLE_NAME_Details,null);
- return res;
- }
- }
Add Comment
Please, Sign In to add comment