images

Android Login Authentication With Local Database(Sqlite)

Hay,geeks in this tutorial you are going to learn how to authenticate user login information using local database(I mean sqlite). I think if your android database concepts are clear then you can easily authenticate user enter data. On more think also this is not a secure way to authenticate user login, because in this tutorial [...]


Hay,geeks in this tutorial you are going to learn how to authenticate user login information using local database(I mean sqlite).
I think if your android database concepts are clear then you can easily authenticate user enter data.
On more think also this is not a secure way to authenticate user login, because in this tutorial i did’t used password encryption. so if user if like us(mobile geek) can able to see and change password.
Download Full source code

Download

Tutorial step

  • Create a simple login form
  • Get the data enter by user
  • Compere that data to your local db and authenticate login

Your LoginActivity
LoginActivity.java

package com.example.loginfromlocal;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		final EditText txtUserName = (EditText)findViewById(R.id.txtUsername);
		final EditText txtPassword = (EditText)findViewById(R.id.txtPassword);
		Button btnLogin = (Button)findViewById(R.id.btnLogin);
		btnLogin.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				String username = txtUserName.getText().toString();
				String password = txtPassword.getText().toString();
				try{
					if(username.length() > 0 && password.length() >0)
					{
						DBUserAdapter dbUser = new DBUserAdapter(LoginActivity.this);
						dbUser.open();

						if(dbUser.Login(username, password))
						{
							Toast.makeText(LoginActivity.this,"Successfully Logged In", Toast.LENGTH_LONG).show();
						}else{
							Toast.makeText(LoginActivity.this,"Invalid Username/Password", Toast.LENGTH_LONG).show();
						}
						dbUser.close();
					}

				}catch(Exception e)
				{
					Toast.makeText(LoginActivity.this,e.getMessage(), Toast.LENGTH_LONG).show();
				}
			}

		});
	}
}

Now checking user enter data with database

DBUserAdapter.java


	package com.example.loginfromlocal;

	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 android.util.Log;

	public class DBUserAdapter
	{
		public static final String KEY_ROWID = "_id";
		public static final String KEY_USERNAME= "username";
		public static final String KEY_PASSWORD = "password";
		private static final String TAG = "DBAdapter";

		private static final String DATABASE_NAME = "usersdb";
		private static final String DATABASE_TABLE = "users";
		private static final int DATABASE_VERSION = 1;

		private static final String DATABASE_CREATE =
			"create table users (_id integer primary key autoincrement, "
			+ "username text not null, "
			+ "password text not null);";

		private Context context = null;
		private DatabaseHelper DBHelper;
		private SQLiteDatabase db;

		public DBUserAdapter(Context ctx)
		{
			this.context = ctx;
			DBHelper = new DatabaseHelper(context);
		}

		private static class DatabaseHelper extends SQLiteOpenHelper
		{
			DatabaseHelper(Context context)
			{
				super(context, DATABASE_NAME, null, DATABASE_VERSION);
			}

			@Override
			public void onCreate(SQLiteDatabase db)
			{
				db.execSQL(DATABASE_CREATE);
			}

			@Override
			public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
			{
				Log.w(TAG, "Upgrading database from version " + oldVersion
						+ " to "
						+ newVersion + ", which will destroy all old data");
				db.execSQL("DROP TABLE IF EXISTS users");
				onCreate(db);
			}
		}    

		public void open() throws SQLException
		{
			db = DBHelper.getWritableDatabase();
		}

		public void close()
		{
			DBHelper.close();
		}    

		public long AddUser(String username, String password)
		{
			 ContentValues initialValues = new ContentValues();
			 initialValues.put(KEY_USERNAME, username);
			 initialValues.put(KEY_PASSWORD, password);
			 return db.insert(DATABASE_TABLE, null, initialValues);

		}

		public boolean Login(String username, String password) throws SQLException
		{
			Cursor mCursor = db.rawQuery("SELECT * FROM " + DATABASE_TABLE + " WHERE username=? AND password=?", new String[]{username,password});
			if (mCursor != null) {
				if(mCursor.getCount() > 0)
				{
					return true;
				}
			}
		 return false;
		}

	}

Download full source code

Download

I think its simple to authentication to local database.
If you want to learn Android login authentication with remote (MySql)database click here !

Related posts:

  1. Android login authentication with remote (MySql)database
  2. Android Icon Generator – Android Asset Studio
  3. Android GUI Set free PSD files