|
- package at.ac.perg.htl.bhif16.coffeeapp;
-
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.util.Log;
-
- import static android.content.Context.MODE_PRIVATE;
-
- public class UserDB extends SQLiteOpenHelper {
-
- private static final String DATABASE_NAME = "USERS.db";
- private static final int DATABASE_VERSION = 1;
- private static final String TABLE_STUDENTS = "STUDENTS";
-
- private static final String COLUMN_STUDENT_ID = "Student_Id";
- private static final String COLUMN_STUDENT_NAME = "Student_Name";
- private static final String COLUMN_STUDENT_COUNT_DRINKS = "Student_Count";
-
- public UserDB(Context context){
- super(context, DATABASE_NAME,null,DATABASE_VERSION);
- }
-
- //create Table
- @Override
- public void onCreate(SQLiteDatabase db) {
- Log.i("SQLite","GOOO...");
- String query = "CREATE TABLE " + TABLE_STUDENTS + "("
- + COLUMN_STUDENT_ID + " INTEGER PRIMARY KEY," + COLUMN_STUDENT_NAME + " TEXT,"
- + COLUMN_STUDENT_COUNT_DRINKS + " INTEGER" + ")";
- db.execSQL(query);
- }
-
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- Log.i("SQLite", "MyDatabaseHelper.onUpgrade ... ");
- // Drop older table if existed
- db.execSQL("DROP TABLE IF EXISTS " + TABLE_STUDENTS);
-
- // Create tables again
- onCreate(db);
- }
-
- public void createDefaultNotesIfNeed() {
- int count = this.getNotesCount();
- if(count ==0 ) {
- Student s1 = new Student(0,"Achleitner",0,0);
- Student s2 = new Student(1,"Aichinger",2,0);
-
- this.addNote(s1);
- this.addNote(s2);
- this.updateStudent(s1);
- }
-
- }
-
- public int getNotesCount() {
- Log.i("SQLite", "MyDatabaseHelper.getNotesCount ... " );
-
- String countQuery = "SELECT * FROM " + TABLE_STUDENTS;
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor = db.rawQuery(countQuery, null);
-
- int count = cursor.getCount();
-
- cursor.close();
-
- // return count
- return count;
- }
-
- public Student getNote(int id) {
- Log.i("SQLite", "MyDatabaseHelper.getNote ... " + id);
-
- SQLiteDatabase db = this.getReadableDatabase();
-
- Cursor cursor = db.query(TABLE_STUDENTS, new String[] { COLUMN_STUDENT_ID,
- COLUMN_STUDENT_NAME, COLUMN_STUDENT_COUNT_DRINKS }, COLUMN_STUDENT_ID + "=?",
- new String[] { String.valueOf(id) }, null, null, null, null);
- if (cursor != null)
- cursor.moveToFirst();
-
- Student note = new Student(Integer.parseInt(cursor.getString(0)),cursor.getString(1),Integer.parseInt(cursor.getString(2)),Integer.parseInt(cursor.getString(3)));
-
- // return note
- return note;
- }
-
- public void addNote(Student note) {
- Log.i("SQLite", "MyDatabaseHelper.addNote ... " + note.getName());
-
- SQLiteDatabase db = this.getWritableDatabase();
-
- ContentValues values = new ContentValues();
- values.put(COLUMN_STUDENT_ID, note.getStudentId());
- values.put(COLUMN_STUDENT_NAME, note.getName());
- values.put(COLUMN_STUDENT_COUNT_DRINKS, note.getConsumedDrinks());
-
- // Inserting Row
- db.insert(TABLE_STUDENTS, null, values);
-
- // Closing database connection
- db.close();
- }
-
- public void updateStudent(Student note) {
- Log.i("SQLite", "MyDatabaseHelper.update ... " + note.getName());
-
- SQLiteDatabase db = this.getWritableDatabase();
-
- ContentValues values = new ContentValues();
- values.put(COLUMN_STUDENT_ID, note.getStudentId());
- values.put(COLUMN_STUDENT_NAME, note.getName());
- values.put(COLUMN_STUDENT_COUNT_DRINKS, note.getConsumedDrinks());
-
- // Inserting Row
- db.update(TABLE_STUDENTS,values,"Student_Id="+note.getStudentId(),null);
- Log.i("SQLite", "ANAL ... ");
-
- // Closing database connection
- db.close();
- }
-
-
-
-
-
-
- }
|