daily pastebin goal
45%
SHARE
TWEET

Untitled

a guest Mar 26th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class DatabaseHelper extends SQLiteOpenHelper {
  2.  
  3.     public static final String DB_NAME = "biodata.db";
  4.     public static final int DB_VERSION = 1;
  5.  
  6.     public static final String TABLE_NAME = "student";
  7.     public static final String ID = "id";
  8.     public static final String NAME = "name";
  9.     public static final String CITY = "city";
  10.     public static final String CURRENT_DATE = "current_date";
  11.  
  12.     public DatabaseHelper(Context context) {
  13.         super(context, DB_NAME, null, DB_VERSION);
  14.     }
  15.  
  16.     @Override
  17.     public void onCreate(SQLiteDatabase db) {
  18.         String sql = "CREATE TABLE " + TABLE_NAME + "(" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
  19.                 NAME + " TEXT, " +
  20.                 CITY + " TEXT, " +
  21.                 CURRENT_DATE + "DATE DEFAULT CURRENT_DATE);";
  22.         db.execSQL(sql);
  23.     }
  24.  
  25.     @Override
  26.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  27.         db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
  28.         onCreate(db);
  29.     }
  30.  
  31.     public void addStudent(Student student) {
  32.         SQLiteDatabase db = this.getWritableDatabase();
  33.         ContentValues values = new ContentValues();
  34.         values.put(NAME, student.getName());
  35.         values.put(CITY, student.getCity());
  36.  
  37.         db.insert(TABLE_NAME, null, values);
  38.     }
  39.  
  40.     public List<Student> getAllStudent() {
  41.         List<Student> studentList = new ArrayList<>();
  42.         String selectQuery = "SELECT * FROM " + TABLE_NAME + " ORDER BY " + ID + " DESC";
  43.         SQLiteDatabase db = this.getReadableDatabase();
  44.         Cursor cursor = db.rawQuery(selectQuery, null);
  45.  
  46.         cursor.moveToFirst();
  47.         for (int i = 0; i < cursor.getCount(); i++) {
  48.             cursor.moveToPosition(i);
  49.  
  50.             Student student = new Student();
  51.             student.setId(cursor.getString(cursor.getColumnIndex(ID)));
  52.             student.setName(cursor.getString(cursor.getColumnIndex(NAME)));
  53.             student.setCity(cursor.getString(cursor.getColumnIndex(CITY)));
  54.             student.setDate(cursor.getString(3));
  55.             studentList.add(student);
  56.         }
  57.  
  58.         return studentList;
  59.     }
  60.  
  61.     public void updateStudent(Student student) {
  62.         SQLiteDatabase db = this.getWritableDatabase();
  63.         ContentValues values = new ContentValues();
  64.         values.put(NAME, student.getName());
  65.         values.put(CITY, student.getCity());
  66.  
  67.         db.update(TABLE_NAME, values, ID + "=?", new String[]{String.valueOf(student.getId())});
  68.         db.close();
  69.     }
  70.  
  71.     public void deleteStudent(String id) {
  72.         SQLiteDatabase db = this.getWritableDatabase();
  73.         db.delete(TABLE_NAME, "id=?", new String[]{id});
  74.         db.close();
  75.     }
  76.  
  77.     public void deleteAllStudent() {
  78.         SQLiteDatabase db = this.getWritableDatabase();
  79.         db.execSQL("DELETE FROM " + TABLE_NAME);
  80.         db.close();
  81.     }
  82. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top