Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.38 KB | None | 0 0
  1. package com.example.alex.lab5_database;
  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. import android.util.Log;
  9.  
  10. import java.util.ArrayList;
  11. import java.util.List;
  12.  
  13. public class db extends SQLiteOpenHelper {
  14. //    private static final String DATABASE_NAME="classmates";
  15. //   private static final String TABLE_CLASS="Classmates";
  16. //
  17. //    public void onCreate(SQLiteDatabase db){
  18. //        db.execSQL("CREATE TABLE " + TABLE_CLASS + "(_id INTEGER PRIMARY KEY
  19. //                AUTOINCREMENT ," + COL NAME + " TEXT ,"+ COL PHONE + " TEXT);");
  20. //    }
  21. //    @Override
  22. //    public void onUpgrade(SQLiteDatabase db, intoldVersion, intnewVersion){
  23. //        db.execSQL("DROP TABLE IF EXISTS"+ TABLE_CLASS);
  24. //        onCreate(db);
  25. //    }
  26. //    Cursor query(String table, String[] columns, String selection,
  27. //                 String[] selectionArgs, String groupBy, String having, String sortOrder);
  28.  
  29.         public static final int DATABASE_VERSION = 1;
  30.         public static final String DATABASE_NAME = "classmates.db";
  31.         public static final String TABLE_CLASS = "Classmates";
  32.  
  33.         public static final String COLUMN_ID = "_id";
  34.         public static final String COLUMN_NAME = "full name";
  35.         public static final String COLUMN_TIME = "time registered";
  36.  
  37. //        public db(Context context) {
  38. //            super(context, DATABASE_NAME, null, DATABASE_VERSION);
  39. //        }
  40.  
  41.         public db(Context context) {
  42.             super(context, TABLE_CLASS, null, 1);
  43.         }
  44.  
  45.         // Database creation sql statement
  46.  
  47.         @Override
  48.         public void onCreate(SQLiteDatabase database) {
  49.             String DATABASE_CREATE = "create table "
  50.                     + TABLE_CLASS + " (" + COLUMN_ID
  51.                     + " integer primary key autoincrement, " + COLUMN_NAME
  52.                     + " text, "+ COLUMN_TIME + " long)";
  53.             database.execSQL(DATABASE_CREATE);
  54.         }
  55.  
  56.         @Override
  57.         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  58. //            Log.w(com.example.alex.lab5_database.db.class.getName(),
  59. //                    "Upgrading database from version " + oldVersion + " to "
  60. //                            + newVersion + ", which will destroy all old data");
  61. //            db.execSQL("DROP TABLE IF EXISTS " + TABLE_CLASS);
  62. //            onCreate(db);
  63.  
  64.             db.execSQL("DROP IF TABLE EXISTS " + TABLE_CLASS);
  65.             onCreate(db);
  66.         }
  67.  
  68.     // Adding a new classmate
  69.     boolean addClassmate(String name, long time) {
  70.         SQLiteDatabase db = this.getWritableDatabase();
  71.  
  72.         ContentValues values = new ContentValues();
  73.         values.put(COLUMN_NAME, name); // Classmate Name
  74.         values.put(COLUMN_TIME,time);
  75.  
  76.         // Inserting Row
  77.         long result = db.insert(TABLE_CLASS, null, values);
  78.  
  79. //        db.close();
  80.  
  81.         if (result == -1) {
  82.             return false;
  83.         } else {
  84.             return true;
  85.         }
  86.  
  87.     }
  88.  
  89.     // Getting single classmate
  90.     Classmate getClassmate(int id) {
  91.         SQLiteDatabase db = this.getReadableDatabase();
  92.  
  93.         Cursor cursor = db.query(TABLE_CLASS, new String[] { COLUMN_ID,
  94.                         COLUMN_NAME, COLUMN_TIME }, COLUMN_ID + "=?",
  95.                 new String[] { String.valueOf(id) }, null, null, null, null);
  96.         if (cursor != null)
  97.             cursor.moveToFirst();
  98.  
  99.         Classmate classmate = new Classmate(Integer.parseInt(cursor.getString(0)),
  100.                 cursor.getString(1), new java.sql.Time(cursor.getLong(2)));
  101.         // return classmate
  102.         return classmate;
  103.     }
  104.  
  105.     // Getting All Classmates
  106.     public List<Classmate> getAllClassmates() {
  107.         List<Classmate> classmateList = new ArrayList<Classmate>();
  108.         // Select All Query
  109.         String selectQuery = "SELECT  * FROM " + TABLE_CLASS;
  110.  
  111.         SQLiteDatabase db = this.getWritableDatabase();
  112.         Cursor cursor = db.rawQuery(selectQuery, null);
  113.  
  114.         // looping through all rows and adding to list
  115.         if (cursor.moveToFirst()) {
  116.             do {
  117.                 Classmate classmate = new Classmate();
  118.                 classmate.setId(Integer.parseInt(cursor.getString(0)));
  119.                 classmate.setName(cursor.getString(1));
  120.                 classmate.setTime(new java.sql.Time(cursor.getLong(2)));
  121.                 // Adding contact to list
  122.                 classmateList.add(classmate);
  123.             } while (cursor.moveToNext());
  124.         }
  125.  
  126.         // return contact list
  127.         return classmateList;
  128.     }
  129.  
  130.     // Updating single classmate
  131.     public int updateClassmate(Classmate classmate) {
  132.         SQLiteDatabase db = this.getWritableDatabase();
  133.         ContentValues values = new ContentValues();
  134.         values.put(COLUMN_NAME, classmate.getName());
  135.  
  136.         // updating row
  137.         return db.update(TABLE_CLASS, values, COLUMN_ID + " = ?",
  138.                 new String[] { String.valueOf(classmate.getId())});
  139.     }
  140.  
  141.     // Deleting single contact
  142.     public void deleteContact(Classmate classmate) {
  143.         SQLiteDatabase db = this.getWritableDatabase();
  144.         db.delete(TABLE_CLASS, COLUMN_ID + " = ?",
  145.                 new String[] { String.valueOf(classmate.getId()) });
  146.         db.close();
  147.     }
  148.  
  149.    
  150.  
  151.  
  152.  
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement