baalman

Untitled

Dec 18th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.89 KB | None | 0 0
  1. package com.example.achma.simpasfinal;
  2.  
  3. /**
  4.  * Created by achma on 08/08/2016.
  5.  */
  6. import android.content.ContentValues;
  7. import android.content.Context;
  8. import android.database.Cursor;
  9. import android.database.sqlite.SQLiteOpenHelper;
  10. import android.database.sqlite.SQLiteDatabase;
  11.  
  12. import java.util.ArrayList;
  13.  
  14. public class DBHelper extends SQLiteOpenHelper {
  15.     public static final String DATABASE_NAME = "password.db";
  16.     public static final String TABLE_NAME = "master";
  17.     public static final String TABLE_NAME1 = "pass_acc";
  18.     public static final String COLUMN_MASTER0 = "id";
  19.     public static final String COLUMN_MASTER1 = "password";
  20.     public static final String COLUMN_MASTER2 = "pin";
  21.     public static final String COLUMN_PASS0 = "id";
  22.     public static final String COLUMN_PASS1 = "title";
  23.     public static final String COLUMN_PASS2 = "username";
  24.     public static final String COLUMN_PASS3 = "password";
  25.     public static final String COLUMN_PASS4 = "website";
  26.     public static final String COLUMN_PASS5 = "notes";
  27.  
  28.     private static DBHelper mDbHelper;
  29.  
  30.     public static synchronized DBHelper getInstance(Context context) {
  31.         // Use the application context, which will ensure that you
  32.         // don't accidentally leak an Activity's context.
  33.  
  34.         if (mDbHelper == null) {
  35.             mDbHelper = new DBHelper(context.getApplicationContext());
  36.         }
  37.         return mDbHelper;
  38.     }
  39.  
  40.     public DBHelper(Context context) {
  41.         super(context, DATABASE_NAME, null, 1);
  42.     }
  43.  
  44.  
  45.     @Override
  46.     public void onCreate(SQLiteDatabase db) {
  47.         db.execSQL("create table "+ TABLE_NAME + "(" + COLUMN_MASTER0 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_MASTER1 +
  48.                 " STRING, " + COLUMN_MASTER2 + " STRING);");
  49.         db.execSQL("create table " + TABLE_NAME1 + "(" + COLUMN_PASS0 + " INTEGER PRIMARY KEY AUTOINCREMENT,"
  50.                 +  COLUMN_PASS1 + " STRING, " +  COLUMN_PASS2 + " STRING, " +  COLUMN_PASS3 + " STRING, " + COLUMN_PASS4
  51.                 + "  STRING," + COLUMN_PASS5 + " STRING);");
  52.     }
  53.     @Override
  54.  
  55.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  56.         db.execSQL("DROP TABLE IF EXIST " + TABLE_NAME);
  57.     }
  58.     //Checking table master
  59.     public boolean isemptypas(){
  60.         SQLiteDatabase db = this.getWritableDatabase();
  61.         Cursor mCursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
  62.         Boolean rowExists;
  63.  
  64.         if (mCursor.moveToFirst())
  65.         {
  66.             // DO SOMETHING WITH CURSOR
  67.             rowExists = true;
  68.  
  69.         } else
  70.         {
  71.             // I AM EMPTY
  72.             rowExists = false;
  73.         }
  74.         return rowExists;
  75.     }
  76.  
  77.     //Insert master password and master pin
  78.     public void insertPass(){
  79.         getset getset = new getset();
  80.         SQLiteDatabase db = this.getWritableDatabase();
  81.         String inspas = getset.getMasPass();
  82.         String inspin = getset.getMaspin();
  83.         ContentValues values = new ContentValues();
  84.         values.put(COLUMN_MASTER1, inspas);
  85.         values.put(COLUMN_MASTER2, inspin);
  86.         db.insert(TABLE_NAME, null, values);
  87.     }
  88.  
  89.     //Insert account data
  90.     public void insertAkun(){
  91.         getset getset = new getset();
  92.         SQLiteDatabase db = this.getWritableDatabase();
  93.         String inTitle = getset.getTitle2();
  94.         String inUser = getset.getUsername2();
  95.         String inPassword = getset.getPassword2();
  96.         String inWeb = getset.getWebsite2();
  97.         String inNotes = getset.getNotes2();
  98.  
  99.         ContentValues values = new ContentValues();
  100.         values.put(COLUMN_PASS1, inTitle);
  101.         values.put(COLUMN_PASS2, inUser);
  102.         values.put(COLUMN_PASS3, inPassword);
  103.         values.put(COLUMN_PASS4, inWeb);
  104.         values.put(COLUMN_PASS5, inNotes);
  105.         db.insert(TABLE_NAME1, null, values);
  106.     }
  107.  
  108.     //Get master password and master pin for login
  109.     public String ambilPass(String pin){
  110.         SQLiteDatabase db = this.getReadableDatabase();
  111.         Cursor cursor= db.query(TABLE_NAME, null, " " + COLUMN_MASTER2 + "=?", new String[]{pin}, null, null, null);
  112.         if(cursor.getCount()<1) // UserName Not Exist
  113.         {
  114.             cursor.close();
  115.             return String.valueOf(0);
  116.         }
  117.         cursor.moveToFirst();
  118.         String password= cursor.getString(cursor.getColumnIndex("PASSWORD"));
  119.         cursor.close();
  120.         return password;
  121.     }
  122.  
  123.     public getset getUser(int user_id) {
  124.         SQLiteDatabase db = this.getReadableDatabase();
  125.  
  126.         Cursor cursor = db.query(TABLE_NAME1, new String[]{COLUMN_PASS0,
  127.                         COLUMN_PASS1,}, COLUMN_PASS0 + "=?",
  128.                 new String[]{String.valueOf(user_id)}, null, null, null, null);
  129.         if (cursor != null)
  130.             cursor.moveToFirst();
  131.  
  132.         getset user = new getset();
  133.         user.setId2(cursor.getString(1));
  134.         user.setTitle2(cursor.getString(2));
  135.         return user;
  136.     }
  137.  
  138.     public ArrayList<getset> getAllUsers() {
  139.         ArrayList<getset> usersList = new ArrayList<getset>();
  140.         String selectQuery = "SELECT  * FROM " + TABLE_NAME1;
  141.  
  142.         SQLiteDatabase db = this.getWritableDatabase();
  143.         Cursor cursor = db.rawQuery(selectQuery, null);
  144.  
  145.         // looping through all rows and adding to list
  146.         if (cursor.moveToFirst()) {
  147.             do {
  148.                 getset user = new getset();
  149.                 user.setId2(cursor.getString(1));
  150.                 user.setTitle2(cursor.getString(2));
  151.                 usersList.add(user);
  152.             } while (cursor.moveToNext());
  153.         }
  154.         return usersList;
  155.     }
  156.  
  157.     public int getUsersCount() {
  158.         String countQuery = "SELECT  * FROM " + TABLE_NAME1;
  159.         SQLiteDatabase db = this.getReadableDatabase();
  160.         Cursor cursor = db.rawQuery(countQuery, null);
  161.         cursor.close();
  162.         return cursor.getCount();
  163.     }
  164. }
Add Comment
Please, Sign In to add comment