You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

132 lines
4.1 KiB

  1. package at.ac.perg.htl.bhif16.coffeeapp;
  2. import android.content.ContentValues;
  3. import android.content.Context;
  4. import android.database.Cursor;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.database.sqlite.SQLiteOpenHelper;
  7. import android.util.Log;
  8. import static android.content.Context.MODE_PRIVATE;
  9. public class UserDB extends SQLiteOpenHelper {
  10. private static final String DATABASE_NAME = "USERS.db";
  11. private static final int DATABASE_VERSION = 1;
  12. private static final String TABLE_STUDENTS = "STUDENTS";
  13. private static final String COLUMN_STUDENT_ID = "Student_Id";
  14. private static final String COLUMN_STUDENT_NAME = "Student_Name";
  15. private static final String COLUMN_STUDENT_COUNT_DRINKS = "Student_Count";
  16. public UserDB(Context context){
  17. super(context, DATABASE_NAME,null,DATABASE_VERSION);
  18. }
  19. //create Table
  20. @Override
  21. public void onCreate(SQLiteDatabase db) {
  22. Log.i("SQLite","GOOO...");
  23. String query = "CREATE TABLE " + TABLE_STUDENTS + "("
  24. + COLUMN_STUDENT_ID + " INTEGER PRIMARY KEY," + COLUMN_STUDENT_NAME + " TEXT,"
  25. + COLUMN_STUDENT_COUNT_DRINKS + " INTEGER" + ")";
  26. db.execSQL(query);
  27. }
  28. @Override
  29. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  30. Log.i("SQLite", "MyDatabaseHelper.onUpgrade ... ");
  31. // Drop older table if existed
  32. db.execSQL("DROP TABLE IF EXISTS " + TABLE_STUDENTS);
  33. // Create tables again
  34. onCreate(db);
  35. }
  36. public void createDefaultNotesIfNeed() {
  37. int count = this.getNotesCount();
  38. if(count ==0 ) {
  39. Student s1 = new Student(0,"Achleitner",0,0);
  40. Student s2 = new Student(1,"Aichinger",2,0);
  41. this.addNote(s1);
  42. this.addNote(s2);
  43. this.updateStudent(s1);
  44. }
  45. }
  46. public int getNotesCount() {
  47. Log.i("SQLite", "MyDatabaseHelper.getNotesCount ... " );
  48. String countQuery = "SELECT * FROM " + TABLE_STUDENTS;
  49. SQLiteDatabase db = this.getReadableDatabase();
  50. Cursor cursor = db.rawQuery(countQuery, null);
  51. int count = cursor.getCount();
  52. cursor.close();
  53. // return count
  54. return count;
  55. }
  56. public Student getNote(int id) {
  57. Log.i("SQLite", "MyDatabaseHelper.getNote ... " + id);
  58. SQLiteDatabase db = this.getReadableDatabase();
  59. Cursor cursor = db.query(TABLE_STUDENTS, new String[] { COLUMN_STUDENT_ID,
  60. COLUMN_STUDENT_NAME, COLUMN_STUDENT_COUNT_DRINKS }, COLUMN_STUDENT_ID + "=?",
  61. new String[] { String.valueOf(id) }, null, null, null, null);
  62. if (cursor != null)
  63. cursor.moveToFirst();
  64. Student note = new Student(Integer.parseInt(cursor.getString(0)),cursor.getString(1),Integer.parseInt(cursor.getString(2)),Integer.parseInt(cursor.getString(3)));
  65. // return note
  66. return note;
  67. }
  68. public void addNote(Student note) {
  69. Log.i("SQLite", "MyDatabaseHelper.addNote ... " + note.getName());
  70. SQLiteDatabase db = this.getWritableDatabase();
  71. ContentValues values = new ContentValues();
  72. values.put(COLUMN_STUDENT_ID, note.getStudentId());
  73. values.put(COLUMN_STUDENT_NAME, note.getName());
  74. values.put(COLUMN_STUDENT_COUNT_DRINKS, note.getConsumedDrinks());
  75. // Inserting Row
  76. db.insert(TABLE_STUDENTS, null, values);
  77. // Closing database connection
  78. db.close();
  79. }
  80. public void updateStudent(Student note) {
  81. Log.i("SQLite", "MyDatabaseHelper.update ... " + note.getName());
  82. SQLiteDatabase db = this.getWritableDatabase();
  83. ContentValues values = new ContentValues();
  84. values.put(COLUMN_STUDENT_ID, note.getStudentId());
  85. values.put(COLUMN_STUDENT_NAME, note.getName());
  86. values.put(COLUMN_STUDENT_COUNT_DRINKS, note.getConsumedDrinks());
  87. // Inserting Row
  88. db.update(TABLE_STUDENTS,values,"Student_Id="+note.getStudentId(),null);
  89. Log.i("SQLite", "ANAL ... ");
  90. // Closing database connection
  91. db.close();
  92. }
  93. }