Wednesday, May 28, 2014

Android Login System using SQLite

I have already written several posts regarding Android database applications. This post might be similar to those tuts. However this is more complete Android Login System which uses SQLite as its database. All files can be downloaded here.



Let’s create the view. first. But I’m not going to show it in the post. Just two text boxes for username and password, two buttons for login and register.

DBHelper.java
  1. package com.my.members;
  2. import android.content.Context;
  3. import android.database.sqlite.SQLiteDatabase;
  4. import android.database.sqlite.SQLiteOpenHelper;
  5. public class DBHelper extends SQLiteOpenHelper {
  6. private static final String DATABASE_NAME = "membersdb";
  7. private static final int DATABASE_VERSION = 1;
  8. private static final String DATABASE_CREATE = "CREATE TABLE (_id integer primary key autoincrement,username text not null,password text not null);";
  9. public DBHelper(Context context) {
  10. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  11. }
  12. @Override
  13. public void onCreate(SQLiteDatabase db) {
  14. db.execSQL(DATABASE_CREATE);
  15. }
  16. @Override
  17. public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
  18. db.execSQL("DROP TABLE IF EXISTS members");
  19. onCreate(db);
  20. }
  21. }

DBAdapter.java
  1. package com.my.members;
  2. import android.content.ContentValues;
  3. import android.content.Context;
  4. import android.database.Cursor;
  5. import android.database.SQLException;
  6. import android.database.sqlite.SQLiteDatabase;
  7. public class DBAdapter
  8. {
  9. private static final String DATABASE_TABLE = "members";
  10. public static final String KEY_ROW_ID = "_id";
  11. public static final String KEY_USERNAME = "username";
  12. public static final String KEY_PASSWORD = "password";
  13. SQLiteDatabase mDb;
  14. Context mCtx;
  15. DBHelper mDbHelper;
  16. public DBAdapter(Context context)
  17. {
  18. this.mCtx = context;
  19. }
  20. public DBAdapter open() throws SQLException
  21. {
  22. mDbHelper = new DBHelper(mCtx);
  23. mDb = mDbHelper.getWritableDatabase();
  24. return this;
  25. }
  26. public void close()
  27. {
  28. mDbHelper.close();
  29. }
  30. public long register(String user,String pw)
  31. {
  32. ContentValues initialValues = new ContentValues();
  33. initialValues.put(KEY_USERNAME, user);
  34. initialValues.put(KEY_PASSWORD, pw);
  35. return mDb.insert(DATABASE_TABLE, null, initialValues);
  36. }
  37. public boolean Login(String username, String password) throws SQLException
  38. {
  39. Cursor mCursor = mDb.rawQuery("SELECT * FROM " + DATABASE_TABLE + " WHERE username=? AND password=?", new String[]{username,password});
  40. if (mCursor != null) {
  41. if(mCursor.getCount() > 0)
  42. {
  43. return true;
  44. }
  45. }
  46. return false;
  47. }
  48. }

MembersActivity
  1. package com.my.members;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.database.SQLException;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.view.inputmethod.InputMethodManager;
  10. import android.widget.Button;
  11. import android.widget.EditText;
  12. import android.widget.Toast;
  13. public class MembersActivity extends Activity {
  14. DBAdapter dbAdapter;
  15. EditText txtUserName;
  16. EditText txtPassword;
  17. Button btnLogin;
  18. Button btnRegister;
  19. @Override
  20. public void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.main);
  23. txtUserName = (EditText) findViewById(R.id.et_user);
  24. txtPassword = (EditText) findViewById(R.id.et_pw);
  25. btnLogin = (Button) findViewById(R.id.btn_login);
  26. btnRegister = (Button) findViewById(R.id.btn_reg);
  27. dbAdapter = new DBAdapter(this);
  28. dbAdapter.open();
  29. btnLogin.setOnClickListener(new OnClickListener() {
  30. @Override
  31. public void onClick(View arg0) {
  32. InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
  33. imm.hideSoftInputFromWindow(txtUserName.getWindowToken(), 0);
  34. imm.hideSoftInputFromWindow(txtPassword.getWindowToken(), 0);
  35. String username = txtUserName.getText().toString();
  36. String password = txtPassword.getText().toString();
  37. if (username.length() > 0 && password.length() > 0) {
  38. try {
  39. if (dbAdapter.Login(username, password)) {
  40. Toast.makeText(MembersActivity.this,
  41. "Successfully Logged In", Toast.LENGTH_LONG)
  42. .show();
  43. } else {
  44. Toast.makeText(MembersActivity.this,
  45. "Invalid username or password",
  46. Toast.LENGTH_LONG).show();
  47. }
  48. } catch (Exception e) {
  49. Toast.makeText(MembersActivity.this, "Some problem occurred",
  50. Toast.LENGTH_LONG).show();
  51. }
  52. } else {
  53. Toast.makeText(MembersActivity.this,
  54. "Username or Password is empty", Toast.LENGTH_LONG).show();
  55. }
  56. }
  57. });
  58. btnRegister.setOnClickListener(new OnClickListener() {
  59. @Override
  60. public void onClick(View arg0) {
  61. InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
  62. imm.hideSoftInputFromWindow(txtUserName.getWindowToken(), 0);
  63. imm.hideSoftInputFromWindow(txtPassword.getWindowToken(), 0);
  64. try {
  65. String username = txtUserName.getText().toString();
  66. String password = txtPassword.getText().toString();
  67. long i = dbAdapter.register(username, password);
  68. if(i != -1)
  69. Toast.makeText(MembersActivity.this, "You have successfully registered",Toast.LENGTH_LONG).show();
  70. } catch (SQLException e) {
  71. Toast.makeText(MembersActivity.this, "Some problem occurred",
  72. Toast.LENGTH_LONG).show();
  73. }
  74. }
  75. });
  76. }
  77. }

No comments:

Post a Comment