SHARE
TWEET

Untitled

a guest Mar 20th, 2016 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ve.itis.hackathon.authentication;
  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.HashMap;
  11.  
  12. import ve.itis.hackathon.R;
  13.  
  14. /**
  15.  * Created by Root on 19/03/2016.
  16.  */
  17. public class SQLiteHandlerAuth extends SQLiteOpenHelper {
  18.  
  19.     private static final String TAG = SQLiteHandlerAuth.class.getSimpleName();
  20.     // All Static variables
  21.     // Database Version
  22.     private static final int DATABASE_VERSION = 1;
  23.     private static Context context;
  24.  
  25.     // Database Name
  26.     private static final String DATABASE_NAME = context.getResources().getString(R.string.app_name);
  27.  
  28.     // Login table name
  29.     private static final String TABLE_USER = "user";
  30.  
  31.     // Login Table Columns names
  32.     private static final String KEY_ID = "id";
  33.     private static final String KEY_NAME = "name";
  34.     private static final String KEY_EMAIL = "email";
  35.     private static final String KEY_PASSWORD = "psw";
  36.  
  37.     public SQLiteHandlerAuth(Context context) {
  38.         super(context, DATABASE_NAME, null, DATABASE_VERSION);
  39.         this.context = context;
  40.     }
  41.  
  42.     // Creating Tables
  43.     @Override
  44.     public void onCreate(SQLiteDatabase db) {
  45.         String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_USER + "("
  46.                 + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
  47.                 + KEY_EMAIL + " TEXT UNIQUE," + KEY_PASSWORD + " TEXT)";
  48.         db.execSQL(CREATE_LOGIN_TABLE);
  49.  
  50.         Log.d(TAG, "Database tables created");
  51.     }
  52.  
  53.     // Upgrading database
  54.     @Override
  55.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  56.         // Drop older table if existed
  57.         db.execSQL("DROP TABLE IF EXISTS " + TABLE_USER);
  58.  
  59.         // Create tables again
  60.         onCreate(db);
  61.     }
  62.  
  63.     /**
  64.      * Storing user details in database
  65.      * */
  66.     public void addUser(String name, String email, String uid) {
  67.         SQLiteDatabase db = this.getWritableDatabase();
  68.  
  69.         ContentValues values = new ContentValues();
  70.         values.put(KEY_NAME, name); // Name
  71.         values.put(KEY_EMAIL, email); // Email
  72.         values.put(KEY_PASSWORD, uid); // Password
  73.  
  74.         // Inserting Row
  75.         long id = db.insert(TABLE_USER, null, values);
  76.         db.close(); // Closing database connection
  77.  
  78.         Log.d(TAG, "New user inserted into sqlite: " + id);
  79.     }
  80.  
  81.     /**
  82.      * Getting user data from database
  83.      * */
  84.     public HashMap<String, String> getUserDetails() {
  85.         HashMap<String, String> user = new HashMap<String, String>();
  86.         String selectQuery = "SELECT  * FROM " + TABLE_USER;
  87.  
  88.         SQLiteDatabase db = this.getReadableDatabase();
  89.         Cursor cursor = db.rawQuery(selectQuery, null);
  90.         // Move to first row
  91.         cursor.moveToFirst();
  92.         if (cursor.getCount() > 0) {
  93.             user.put("name", cursor.getString(1));
  94.             user.put("email", cursor.getString(2));
  95.             user.put("psw", cursor.getString(3));
  96.         }
  97.         cursor.close();
  98.         db.close();
  99.         // return user
  100.         Log.d(TAG, "Fetching user from Sqlite: " + user.toString());
  101.  
  102.         return user;
  103.     }
  104.  
  105.     /**
  106.      * Re crate database Delete all tables and create them again
  107.      * */
  108.     public void deleteUsers() {
  109.         SQLiteDatabase db = this.getWritableDatabase();
  110.         // Delete All Rows
  111.         db.delete(TABLE_USER, null, null);
  112.         db.close();
  113.  
  114.         Log.d(TAG, "Deleted all user info from sqlite");
  115.     }
  116.  
  117. }
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