Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package garbage360.jean.garbage360;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.SQLException;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * Created by Terence on 2/27/2017.
- */
- public class LoginDatabase extends SQLiteOpenHelper
- {
- // Database Version
- private static final int DATABASE_VERSION = 1;
- // Database Name
- private static final String DATABASE_NAME = "UserData.db";
- // User table name
- private static final String TABLE_USER = "user";
- // User Table Columns names
- private static final String COLUMN_USER_ID = "user_id";
- private static final String COLUMN_USER_NAME = "user_name";
- private static final String COLUMN_USER_EMAIL = "user_email";
- private static final String COLUMN_USER_ADDRESS = "user_address";
- private static final String COLUMN_USER_PASSWORD = "user_password";
- // create table sql query
- private String CREATE_USER_TABLE = "CREATE TABLE " + TABLE_USER + "("
- + COLUMN_USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_USER_NAME + " TEXT," + COLUMN_USER_EMAIL + " TEXT," + COLUMN_USER_ADDRESS + " TEXT," + COLUMN_USER_PASSWORD + " TEXT" + ")";
- // drop table sql query
- private String DROP_USER_TABLE = "DROP TABLE IF EXISTS " + TABLE_USER;
- /**
- * Constructor
- *
- * @param context
- */
- public LoginDatabase(Context context) {
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL(CREATE_USER_TABLE);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- //Drop User Table if exist
- db.execSQL(DROP_USER_TABLE);
- // Create tables again
- onCreate(db);
- }
- /**
- * This method is to create user record
- *
- * @param user
- */
- public void addUser(UserModel user) {
- SQLiteDatabase db = this.getWritableDatabase();
- ContentValues values = new ContentValues();
- values.put(COLUMN_USER_NAME, user.getName());
- values.put(COLUMN_USER_EMAIL, user.getEmail());
- values.put(COLUMN_USER_ADDRESS, user.getAddress());
- values.put(COLUMN_USER_PASSWORD, user.getPassword());
- // Inserting Row
- db.insert(TABLE_USER, null, values);
- db.close();
- }
- /**
- * This method is to fetch all user and return the list of user records
- *
- * @return list
- */
- public List<UserModel> getAllUser() {
- // array of columns to fetch
- String[] columns = {
- COLUMN_USER_ID,
- COLUMN_USER_EMAIL,
- COLUMN_USER_NAME,
- COLUMN_USER_ADDRESS,
- // COLUMN_USER_PASSWORD
- };
- // sorting orders
- String sortOrder =
- COLUMN_USER_NAME + " ASC";
- List<UserModel> userList = new ArrayList<UserModel>();
- SQLiteDatabase db = this.getReadableDatabase();
- // query the user table
- /**
- * Here query function is used to fetch records from user table this function works like we use sql query.
- * SQL query equivalent to this query function is
- * SELECT user_id,user_name,user_email,user_password FROM user ORDER BY user_name;
- */
- Cursor cursor = db.query(TABLE_USER, //Table to query
- columns, //columns to return
- null, //columns for the WHERE clause
- null, //The values for the WHERE clause
- null, //group the rows
- null, //filter by row groups
- sortOrder); //The sort order
- // Traversing through all rows and adding to list
- if (cursor.moveToFirst()) {
- do {
- UserModel user = new UserModel();
- user.setId(Integer.parseInt(cursor.getString(cursor.getColumnIndex(COLUMN_USER_ID))));
- user.setName(cursor.getString(cursor.getColumnIndex(COLUMN_USER_NAME)));
- user.setEmail(cursor.getString(cursor.getColumnIndex(COLUMN_USER_EMAIL)));
- user.setAddress(cursor.getString(cursor.getColumnIndex(COLUMN_USER_ADDRESS)));
- // user.setPassword(cursor.getString(cursor.getColumnIndex(COLUMN_USER_PASSWORD)));
- // Adding user record to list
- userList.add(user);
- } while (cursor.moveToNext());
- }
- cursor.close();
- db.close();
- // return user list
- return userList;
- }
- /**
- * This method to update user record
- *
- * @param user
- */
- public void updateUser(UserModel user) {
- SQLiteDatabase db = this.getWritableDatabase();
- ContentValues values = new ContentValues();
- values.put(COLUMN_USER_NAME, user.getName());
- values.put(COLUMN_USER_EMAIL, user.getEmail());
- values.put(COLUMN_USER_ADDRESS, user.getAddress());
- values.put(COLUMN_USER_PASSWORD, user.getPassword());
- // updating row
- db.update(TABLE_USER, values, COLUMN_USER_ID + " = ?",
- new String[]{String.valueOf(user.getId())});
- db.close();
- }
- /**
- * This method is to delete user record
- *
- * @param user
- */
- public void deleteUser(UserModel user) {
- SQLiteDatabase db = this.getWritableDatabase();
- // delete user record by id
- db.delete(TABLE_USER, COLUMN_USER_ID + " = ?",
- new String[]{String.valueOf(user.getId())});
- db.close();
- }
- /**
- * This method to check user exist or not
- *
- * @param email
- * @return true/false
- */
- public boolean checkUser(String email) {
- // array of columns to fetch
- String[] columns = {
- COLUMN_USER_ID
- };
- SQLiteDatabase db = this.getReadableDatabase();
- // selection criteria
- String selection = COLUMN_USER_EMAIL + " = ?";
- // String selection = COLUMN_USER_ADDRESS + "=?";
- // selection argument
- String[] selectionArgs = {email};
- // query user table with condition
- /**
- * Here query function is used to fetch records from user table this function works like we use sql query.
- * SQL query equivalent to this query function is
- * SELECT user_id FROM user WHERE user_email = 'jack@androidtutorialshub.com';
- */
- Cursor cursor = db.query(TABLE_USER, //Table to query
- columns, //columns to return
- selection, //columns for the WHERE clause
- selectionArgs, //The values for the WHERE clause
- null, //group the rows
- null, //filter by row groups
- null); //The sort order
- int cursorCount = cursor.getCount();
- cursor.close();
- db.close();
- if (cursorCount > 0) {
- return true;
- }
- return false;
- }
- /**
- * This method to check user exist or not
- *
- * @param email
- * @param password
- * @return true/false
- */
- public boolean checkUser(String email, String password) {
- // array of columns to fetch
- String[] columns = {
- COLUMN_USER_ID
- };
- SQLiteDatabase db = this.getReadableDatabase();
- // selection criteria
- String selection = COLUMN_USER_EMAIL + " = ?" + " AND " + COLUMN_USER_PASSWORD + " = ?";
- // selection arguments
- String[] selectionArgs = {email, password};
- // query user table with conditions
- /**
- * Here query function is used to fetch records from user table this function works like we use sql query.
- * SQL query equivalent to this query function is
- * SELECT user_id FROM user WHERE user_email = 'jack@androidtutorialshub.com' AND user_password = 'qwerty';
- */
- Cursor cursor = db.query(TABLE_USER, //Table to query
- columns, //columns to return
- selection, //columns for the WHERE clause
- selectionArgs, //The values for the WHERE clause
- null, //group the rows
- null, //filter by row groups
- null); //The sort order
- int cursorCount = cursor.getCount();
- cursor.close();
- db.close();
- if (cursorCount > 0) {
- return true;
- }
- return false;
- }
- }
- /**
- static final String DATABASE_NAME = "login.db";
- static final int DATABASE_VERSION = 1;
- public static final int NAME_COLUMN = 1;
- // TODO: Create public field for each column in your table.
- // SQL Statement to create a new database.
- static final String DATABASE_CREATE = "create table "+"LOGIN"+
- "( " +"ID"+" integer primary key autoincrement,"+ "USERNAME text,PASSWORD text); ";
- // Variable to hold the database instance
- public SQLiteDatabase db;
- // Context of the application using the database.
- private final Context context;
- // Database open/upgrade helper
- private DatabaseHelper dbHelper;
- public LoginDatabase(Context _context)
- {
- context = _context;
- dbHelper = new DatabaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
- }
- public LoginDatabase open() throws SQLException
- {
- db = dbHelper.getWritableDatabase();
- return this;
- }
- public void close()
- {
- db.close();
- }
- public SQLiteDatabase getDatabaseInstance()
- {
- return db;
- }
- public void insertEntry(String userName,String password)
- {
- ContentValues newValues = new ContentValues();
- // Assign values for each row.
- newValues.put("USERNAME", userName);
- newValues.put("PASSWORD",password);
- // Insert the row into your table
- db.insert("LOGIN", null, newValues);
- ///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
- }
- public int deleteEntry(String UserName)
- {
- //String id=String.valueOf(ID);
- String where="USERNAME=?";
- int numberOFEntriesDeleted= db.delete("LOGIN", where, new String[]{UserName}) ;
- // Toast.makeText(context, "Number fo Entry Deleted Successfully : "+numberOFEntriesDeleted, Toast.LENGTH_LONG).show();
- return numberOFEntriesDeleted;
- }
- public String getSinlgeEntry(String userName)
- {
- Cursor cursor=db.query("LOGIN", null, " USERNAME=?", new String[]{userName}, null, null, null);
- if(cursor.getCount()<1) // UserName Not Exist
- {
- cursor.close();
- return "NOT EXIST";
- }
- cursor.moveToFirst();
- String password= cursor.getString(cursor.getColumnIndex("PASSWORD"));
- cursor.close();
- return password;
- }
- public void updateEntry(String userName,String password)
- {
- // Define the updated row content.
- ContentValues updatedValues = new ContentValues();
- // Assign values for each row.
- updatedValues.put("USERNAME", userName);
- updatedValues.put("PASSWORD",password);
- String where="USERNAME = ?";
- db.update("LOGIN",updatedValues, where, new String[]{userName});
- }**/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement