Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package com.example.restoalan;
  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. import androidx.annotation.Nullable;
  10.  
  11. public class DatabaseHelper extends SQLiteOpenHelper {
  12.     public DatabaseHelper(Context context) { super(context, "LoginSQLite.db", null, 1);}
  13.  
  14.  
  15.     @Override
  16.     public void onCreate(SQLiteDatabase db) {
  17.         db.execSQL("CREATE TABLE session(id integer PRIMARY KEY, login text)");
  18.         db.execSQL("CREATE TABLE user(id integer PRIMARY KEY AUTOINCREMENT, username text, password text)");
  19.         db.execSQL("INSERT INTO session(id, login) VALUES(1, 'kosong')");
  20.     }
  21.  
  22.     @Override
  23.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  24.         db.execSQL("DROP TABLE IF EXISTS session");
  25.         db.execSQL("DROP TABLE IF EXISTS user");
  26.         onCreate(db);
  27.     }
  28. //check seassion
  29. public Boolean checkSession(String sessionValues) {
  30.     SQLiteDatabase db = this.getReadableDatabase();
  31.     Cursor cursor = db.rawQuery("SELECT * FROM session WHERE login = ?", new String[]{sessionValues});
  32.     if (cursor.getCount() > 0) {
  33.         return true;
  34.     }
  35.     else {
  36.         return false;
  37.     }
  38. }
  39.     //upgrade session
  40.     public Boolean upgradeSession(String sessionValues, int id) {
  41.         SQLiteDatabase db = this.getWritableDatabase();
  42.         ContentValues contentValues = new ContentValues();
  43.         contentValues.put("login", sessionValues);
  44.         long update = db.update("session", contentValues, "id="+id, null);
  45.         if (update == -1) {
  46.             return false;
  47.         }
  48.         else {
  49.             return true;
  50.         }
  51.     }
  52.  
  53.     //insert user
  54.     public Boolean insertUser(String username, String password) {
  55.         SQLiteDatabase db = this.getWritableDatabase();
  56.         ContentValues contentValues = new ContentValues();
  57.         contentValues.put("username", username);
  58.         contentValues.put("password", password);
  59.         long insert = db.insert("user", null, contentValues);
  60.         if (insert == -1) {
  61.             return false;
  62.         }
  63.         else {
  64.             return true;
  65.         }
  66.     }
  67.  
  68.     //check login
  69.     public Boolean checkLogin(String username, String password) {
  70.         SQLiteDatabase db = this.getReadableDatabase();
  71.         Cursor cursor = db.rawQuery("SELECT * FROM user WHERE username = ? AND password = ?", new String[]{username, password});
  72.         if (cursor.getCount() > 0) {
  73.             return true;
  74.         }
  75.         else {
  76.             return false;
  77.         }
  78.     }
  79. }