Guest User

Untitled

a guest
Dec 19th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. public class DatabaseHelper extends SQLiteOpenHelper {
  2. private static final String DATABASE_NAME="upgrade";
  3. private static final int DATABASE_VERSION=2;
  4.  
  5. //details table details
  6. public static final String TABLE_NAME_Details="details";
  7. public static final String USERNAME="USERNAME";
  8. public static final String PASSWORD="PASSWORD";
  9. public static final String Name="Name"; //newly added column in version 2
  10.  
  11. //create table statements for version 2
  12. public static final String Create_Table_Details = "CREATE TABLE "
  13. + TABLE_NAME_Details + " (" + USERNAME + " TEXT PRIMARY KEY,"
  14. + PASSWORD + " TEXT ,"
  15. + Name + " TEXT"
  16. +")";
  17.  
  18. /* creation for version 1
  19. private static final String Create_Table_Details = "CREATE TABLE " + TABLE_NAME_Details + "("
  20. + USERNAME + " TEXT PRIMARY KEY,"
  21. + PASSWORD + " TEXT"
  22. + ")";*/
  23.  
  24.  
  25. //Alter table statements FOR onUpgrade()
  26. private static final String ALTER_Details = "ALTER TABLE" + TABLE_NAME_Details + "ADD COLUMN" + Name + "TEXT";
  27.  
  28. public DatabaseHelper(Context context)
  29. {
  30. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  31. }
  32.  
  33. @Override
  34. public void onCreate(SQLiteDatabase db) {
  35. try{
  36. db.execSQL(Create_Table_Details);
  37. Log.d("database","installed successfully");
  38.  
  39. }catch (Exception e)
  40. {
  41. e.printStackTrace();
  42. Log.e("/test","Exception due to"+e.toString());
  43. }
  44.  
  45. }
  46.  
  47. @Override
  48. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
  49. try{
  50. Log.d("eEmp/DBUpgrade", "oldVersion:" + oldVersion + ", NewVersion:" + newVersion);
  51. /* db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME_Details);
  52. onCreate(db);*/
  53.  
  54. if(oldVersion < 2)
  55. {
  56. try{
  57. Cursor c = db.rawQuery("Select * from " + TABLE_NAME_Details + " WHERE 1 = 0", null);
  58. if (c.getColumnIndex(Name) == -1) {
  59. db.execSQL(ALTER_Details);
  60. }
  61. if (c != null)
  62. c.close();
  63. }catch(Exception upgradeexp)
  64. {
  65. upgradeexp.printStackTrace();
  66. Log.e("/test","Exception due to" + upgradeexp.toString());
  67. }
  68. }
  69.  
  70. }catch(Exception dbUpgradeExp)
  71. {
  72. dbUpgradeExp.printStackTrace();
  73. Log.e("/test","Exception due to" + dbUpgradeExp.toString());
  74. }
  75. /* if(oldVersion < 2)
  76. {
  77. db.execSQL(ALTER_Details);
  78. }*/
  79.  
  80. }
  81.  
  82. //inserting login data to database
  83. public boolean insertData(String username, String password,String name)
  84. {
  85. try{
  86. SQLiteDatabase db = this.getWritableDatabase();
  87. ContentValues contentValues=new ContentValues();
  88.  
  89. contentValues.put(USERNAME,username);
  90. contentValues.put(PASSWORD,password);
  91. contentValues.put(Name,name);
  92. long result=db.insert(TABLE_NAME_Details, null, contentValues);
  93. if(result==-1)
  94. return false;
  95. else
  96. return true;
  97. }catch(Exception e)
  98. {
  99. e.printStackTrace();
  100. Log.e("/test","Exception due to"+e.toString());
  101. return false;
  102. }
  103. }
  104.  
  105. //getting data from database
  106. public Cursor getAllData()
  107. {
  108. SQLiteDatabase db = this.getWritableDatabase();
  109. Cursor res = db.rawQuery("select * from "+TABLE_NAME_Details,null);
  110. return res;
  111. }
  112.  
  113. }
Add Comment
Please, Sign In to add comment