使用SQlite验证Android中的登录

使用SQlite验证Android中的登录,android,sqlite,login,cursor,Android,Sqlite,Login,Cursor,我正在尝试为Android中的应用程序创建登录屏幕。我已将有关用户的信息存储在数据库的“用户”表中。 我正在尝试使用游标对象将登录屏幕上输入的用户名和密码与数据库中的值进行匹配,但它不起作用,导致应用程序崩溃。 是否有人可以推荐或修改该方法,如果可能的话,使用一些代码片段。 非常感谢,谢谢 下面是LoginForm类的代码。(它使用DBAdapter类连接到数据库) 包com.androidbook.LoginForm import android.app.Activity; import an

我正在尝试为Android中的应用程序创建登录屏幕。我已将有关用户的信息存储在数据库的“用户”表中。 我正在尝试使用游标对象将登录屏幕上输入的用户名和密码与数据库中的值进行匹配,但它不起作用,导致应用程序崩溃。 是否有人可以推荐或修改该方法,如果可能的话,使用一些代码片段。 非常感谢,谢谢

下面是LoginForm类的代码。(它使用DBAdapter类连接到数据库)

包com.androidbook.LoginForm

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Toast;

public class LoginForm extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final DBAdapter db = new DBAdapter(getBaseContext());
        final AutoCompleteTextView username = (AutoCompleteTextView)this.findViewById(R.id.AutoComUsernameLogin);
        final AutoCompleteTextView password = (AutoCompleteTextView)this.findViewById(R.id.AutoComPasswordLogin);

        Button Register = (Button) findViewById(R.id.ClicktoRegister);
        Register.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), RegistrationForm.class);
                startActivityForResult(myIntent, 0);    
            }
        });
     //************************** LOG IN LOGIC******************************//   
        Button Login = (Button) findViewById(R.id.LoginButton);
        Login.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                final String Username = username.getText().toString();
                final String Password=  password.getText().toString();

                db.open();

                Cursor c = db.getAllTitles();

                   while(c.moveToNext())
                   {
                       String c1=c.getString(2);
                       String c2=c.getString(3);

                       if(c1 == Username)
                        {
                            if(c2 == Password)
                            {
                            Toast.makeText(LoginForm.this,                 
                            "You are succesfully logged in.",
                            Toast.LENGTH_LONG).show();

                                Intent myIntent = new Intent(view.getContext(), Menu.class);
                                startActivityForResult(myIntent, 0); 
                            }
                            else
                            {
                                Toast.makeText(LoginForm.this, "Incorrect password",Toast.LENGTH_LONG).show();
                            }
                            Intent myIntent = new Intent(view.getContext(), LoginForm.class);
                            startActivityForResult(myIntent, 0); 
                        }

                       else
                        Toast.makeText(LoginForm.this, "Incorrect",Toast.LENGTH_LONG).show();
                   }

                db.close();


            }
        });
    }
     }

您可以在数据库类中创建一个函数,该函数将返回整数。如果整数是1,那么我们可以说用户名和密码匹配,或者说登录失败。不需要编写复杂的代码

public int Login(String username,String password)
{
    try
    {
        int i = 0;
        Cursor c = null;
        c = db.rawQuery("select * from login_table where username =" + "\""+ username.trim() + "\""+" and password="+ "\""+ password.trim() + "\""+, null);
        c.moveToFirst();
        i = c.getCount(); 
        c.close(); 
        return i;
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    return 0;
}

当我们尝试这段代码时,它要求将类型更改为布尔值

public int Login(String username,String password)
{
    try
    {
        int i = 0;
        Cursor c = null;
        c = db.rawQuery("select * from login_table where username =" + "\""+ username.trim() + "\""+" and password="+ "\""+ password.trim() + "\""+, null);
        c.moveToFirst();
        i = c.getCount(); 
        c.close(); 
        return i;
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    return 0;
}

Chirag Raval给出的答案具有上述功能,但易受SQL注入的影响。恶意用户可以使用基本SQLi负载轻松绕过身份验证机制(只要数据库中至少有一个条目被检查)

具有有界值的参数化查询是更安全的方法

修复代码段:

public int Login(String username,String password)
{
    String[] selectionArgs = new String[]{username, password};
    try
    {
        int i = 0;
        Cursor c = null;
        c = db.rawQuery("select * from login_table where username=? and password=?", selectionArgs);
        c.moveToFirst();
        i = c.getCount(); 
        c.close(); 
        return i;
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    return 0;
}

这个简单的改进更安全,也更容易编码。

如果您认为这是一个非常好的答案,那么您可以对此进行投票,以便其他用户可以使用此代码…在处理游标对象时始终调用游标#close()。在插入sql语句之前,您应该先处理user/pass。这就像java编写的语句一样,可以防止sql注入攻击。