Advertisement
Guest User

DatabaseHelper

a guest
Apr 25th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. package example.uuj.employeerecords;
  2.  
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.database.sqlite.SQLiteDatabase;
  7. import android.database.sqlite.SQLiteOpenHelper;
  8.  
  9. public class DatabaseHelper extends SQLiteOpenHelper
  10. {
  11. // Declaring variables
  12. public static final String DATABASE_NAME = "EmployeeRecords.db";
  13. public static final String TABLE_NAME = "Employees";
  14. public static final String COL_1 = "ID";
  15. public static final String COL_2 = "Name";
  16. public static final String COL_3 = "DOB";
  17. public static final String COL_4 = "Address";
  18. public static final String COL_5 = "Town";
  19. public static final String COL_6 = "Postcode";
  20. public static final String COL_7 = "ContactNumber";
  21. public static final String COL_8 = "Email";
  22. public static final String COL_9 = "JobTitle";
  23. public static final String COL_10 = "Salary";
  24.  
  25. public DatabaseHelper(Context context)
  26. {
  27. super(context, DATABASE_NAME, null, 1);
  28. }
  29.  
  30. @Override
  31. public void onCreate(SQLiteDatabase db)
  32. {
  33. // Create the database table
  34. db.execSQL("create table " + TABLE_NAME +" " +
  35. "(ID INTEGER PRIMARY KEY AUTOINCREMENT, Name VARCHAR, DOB REAL, " +
  36. "Address VARCHAR, Town VARCHAR, Postcode VARCHAR, ContactNumber INT, Email VARCHAR," +
  37. "JobTitle VARCHAR, Salary REAL)");
  38. }
  39.  
  40. @Override
  41. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
  42. {
  43. // Drop table statement
  44. db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
  45. onCreate(db);
  46. }
  47.  
  48. public boolean addData(String Name, String DOB, String Address,String Town,String Postcode, String ContactNumber,
  49. String Email, String JobTitle, String Salary)
  50. {
  51. // Add data into the database
  52. SQLiteDatabase db = this.getWritableDatabase();
  53.  
  54. ContentValues contentValues = new ContentValues();
  55.  
  56. contentValues.put(COL_2,Name);
  57. contentValues.put(COL_3,DOB);
  58. contentValues.put(COL_4,Address);
  59. contentValues.put(COL_5,Town);
  60. contentValues.put(COL_6,Postcode);
  61. contentValues.put(COL_7,ContactNumber);
  62. contentValues.put(COL_8,Email);
  63. contentValues.put(COL_9,JobTitle);
  64. contentValues.put(COL_10,Salary);
  65.  
  66. long result = db.insert(TABLE_NAME,null ,contentValues);
  67.  
  68. if(result == -1)
  69. return false;
  70. else
  71. return true;
  72. }
  73.  
  74. // Method to display all the data within the database
  75. public Cursor getAllData()
  76. {
  77. SQLiteDatabase db = this.getWritableDatabase();
  78. Cursor allData = db.rawQuery("select * from "+TABLE_NAME,null);
  79. return allData;
  80. }
  81.  
  82.  
  83. // May not need? Delete later if not used.
  84. public Integer deleteData (String ID)
  85. {
  86. SQLiteDatabase db = this.getWritableDatabase();
  87. return db.delete(TABLE_NAME, "ID = ?",new String[] {ID});
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement