news and informationbusiness,health,entertainment,technology automotive,business,crime,health,life,politics,science,technology,travel
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 !
Hello, A.P
Everything went nice.but one problem is that i think u haven’t mentioned that the content of database should be copied to device.until unless copied it it displays only invalid username and password. after that it works fine. thanks alot
Ohh nice
its just verify by 1 & 0
braja pal told “Everything went nice.but one problem is that i think u haven’t mentioned that the content of database should be copied to device.until unless copied it it displays only invalid username and password. after that it works fine.|”
I just done the above login validation.it shows invalid username and password
so please can u tell me how can copy the content of databaseto device.
ohh i think still you both not getting me , php file file just echo 1 for true login , and 0 for false , android device need to get just 1 or 0
and if you want to get usernae echo “$username”; but its less security
Hello Ajay patel,
I didnt get you.should we create php coding for this i just done the above project in eclipse but it shows invalid usrname and password.I am doing project on this android so please help
Yes you have to create php file
You can add username and password using the arguments in the method, but then you will
have to use another xml file for getting the user to register/add username and password first
into the database, then you can use the added username and password to login. I wanted it simple so I added the username and password directly into the database avoiding the user registration. But I have learnt a lot from this tutorial and the author is a good teacher, simple , plain and easy to understand
k thanks
I am connecting my application into the local database. So i done copy ur coding and implement in my project but it shows only invalid username and password.
use your own check.php file hosted on live server
It’s a good tutorial, helps newbies like me to get right on the button with Android SQLite database creation. However you realize when you use the exact code here, the login fails, because data(username, password) are not written into the database, the fields are empty in the database. The method “AddUser()” is not called anywhere. To get it work, I changed it slightly to get me up to speed.
You can use the method this way :
1. In DBUserAdapter.java class
//comment out the arguments and add username,password directly
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);
}
2.In LoginActivity.java class
Call the method :
Just beneath the :
dbUser.open();
Call the method “AddUser”
dbUser.AddUser();
3. Now when you run the application, you can login with “username” and “password”
4.It is still a brilliantly work done by the author
Thanks for sharing this…
Thanks to give such a wonderful tutorial…I have one problem with this…Me too done this task in the same way but it give error like “01-09 18:14:18.337: E/dalvikvm(873): Unable to open stack trace file ‘/data/anr/traces.txt’: Permission denied” How to solve this please help me….
[...] Android Login Authentication With Local Database(Sqlite) [...]
@ sirni you should add this permision in manifest <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" to allow using Log in your app
Thanks for the article..great work..
I copied the code into my exhisting simple two screen app, but my “btnLogin” does nothing when clicked. Eclipse shows no errors when run. Since I am new to this, this is what I don’t understand: 1) do I have to create a DATABASE with username and password fields? 2) if yes, how exactly do I do that? I created a Db using Mozilla SQL manager, but don’t know how to import it to Eclipse. This is what I have. When the app is run, it opens main.xml with Login button. When you press login, the app takes you to the login screen where I have the username text box, password text box and the “btnLogin” that does nothing when clicked. Any help is appreciated. Thank you.
I created a new project with your code only, and I still can’t seem to get the lgnButton working. When clicked….nothing. You mentioned the PHP file. How do I create that file and where?
can we have a code for registre user with android/php/mysql
Hi Ajay,
I am very new to android. I want to learn it. I have been learning the layouts. Can you please guide me where to start and how should i go? Can you please tell me how create the database in android using SQLite with explanation…. Can you be my mentor please… Please do mail me at purplerain.moon@gmail.com
Thank you,
Khalid