Android: Standalone login with SQLiteOpenHelper
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/**
* Created by Chandra on 31-10-2015.
*/
public class LoginSQLiteHelper extends SQLiteOpenHelper {
public static final String TABLE_NAME = "table_login";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_USER_EMAIL = "user_email";
public static final String COLUMN_USER_PWD = "user_pwd";
public static final String COLUMN_USER_NAME = "user_name";
public static final String COLUMN_USER_NO = "user_no";
private static final String DATABASE_NAME = "login.db";
private static final int DATABASE_VERSION = 1;
// Database creation sql statement
private static final String SQL_CREATE_ENTRIES = "CREATE TABLE "
+ TABLE_NAME + "(" + COLUMN_ID + " INTEGER PRIMARY KEY, "
+ COLUMN_USER_EMAIL + " TEXT, "
+ COLUMN_USER_PWD + " TEXT, "
+ COLUMN_USER_NAME + " TEXT, "
+ COLUMN_USER_NO + " TEXT)" ;
public LoginSQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(SQL_CREATE_ENTRIES);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(LoginSQLiteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
/**
* Created by Chandra on 31-10-2015.
*/
public class LoginDataSource {
private static Context mContext;
private SQLiteDatabase database;
private LoginSQLiteHelper dbHelper;
private String[] allColumns = {LoginSQLiteHelper.COLUMN_ID,
LoginSQLiteHelper.COLUMN_USER_EMAIL,
LoginSQLiteHelper.COLUMN_USER_PWD,
LoginSQLiteHelper.COLUMN_USER_NAME,
LoginSQLiteHelper.COLUMN_USER_NO};
private static LoginDataSource ourInstance = null;
public static LoginDataSource getInstance() {
if (null == ourInstance) {
ourInstance = new LoginDataSource();
}
return ourInstance;
}
public void setAppContext(Context context) {
mContext = context;
}
private LoginDataSource() {
}
public void open() throws SQLException {
dbHelper = new LoginSQLiteHelper(mContext);
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public List<UserInfo> getAllEntry() {
List<UserInfo> entryList = new ArrayList<>();
Cursor cursor = database.query(LoginSQLiteHelper.TABLE_NAME,
allColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
UserInfo entry = UserManager.getInstance().cursorToEntry(cursor);
entryList.add(entry);
cursor.moveToNext();
}
cursor.close();
return entryList;
}
public UserInfo createEntry(String email, String pwd, String name,
String no) {
ContentValues values = new ContentValues();
values.put(LoginSQLiteHelper.COLUMN_USER_EMAIL, email);
values.put(LoginSQLiteHelper.COLUMN_USER_PWD, pwd);
values.put(LoginSQLiteHelper.COLUMN_USER_NAME, name);
values.put(LoginSQLiteHelper.COLUMN_USER_NO, no);
long insertId = database.insert(LoginSQLiteHelper.TABLE_NAME, null, values);
Cursor cursor = database.query(LoginSQLiteHelper.TABLE_NAME,
allColumns, LoginSQLiteHelper.COLUMN_ID + " = " + insertId, null,
null, null, null);
cursor.moveToFirst();
UserInfo newEntry = UserManager.getInstance().cursorToEntry(cursor);
cursor.close();
return newEntry;
}
public void deleteEntry(UserInfo entry) {
String id = entry.getEmail();
database.delete(LoginSQLiteHelper.TABLE_NAME, LoginSQLiteHelper.COLUMN_USER_EMAIL
+ " = " + id, null);
}
public Cursor login(String userId, String password) {
Cursor myCursor = database.query(LoginSQLiteHelper.TABLE_NAME,
new String[]{LoginSQLiteHelper.COLUMN_USER_EMAIL, LoginSQLiteHelper.COLUMN_USER_PWD,
LoginSQLiteHelper.COLUMN_USER_NAME, LoginSQLiteHelper.COLUMN_USER_NO},
LoginSQLiteHelper.COLUMN_USER_EMAIL + "='" + userId + "' AND " +
LoginSQLiteHelper.COLUMN_USER_PWD + "='" + password + "'", null, null, null, null);
if (myCursor != null) {
myCursor.moveToFirst();
}
return myCursor;
}
public Cursor getUserInfo(String userId) {
Cursor myCursor = database.query(LoginSQLiteHelper.TABLE_NAME,
new String[]{LoginSQLiteHelper.COLUMN_USER_EMAIL,
LoginSQLiteHelper.COLUMN_USER_NAME, LoginSQLiteHelper.COLUMN_USER_NO},
LoginSQLiteHelper.COLUMN_USER_EMAIL + "='" + userId + "'", null, null, null, null);
if (myCursor != null) {
myCursor.moveToFirst();
}
return myCursor;
}
}
https://github.com/sekhar15aug/Chandra201
ReplyDelete