0

我正在创建一个登录页面,该页面将使用 e sqlite db 进行身份验证。当我将数据输入到我编辑文本字段并单击“登录”时,应将文本字段中的数据与 sqlite db 中的数据进行比较。出于某种原因,我得到了我的捕获异常错误“null”。我相信我的查询没有提取需要比较的数据。当我的程序运行时,除了捕获异常之外,我没有收到任何错误......令人沮丧!

我的 OnClick 方法比较和登录:

 lsLogin.setOnClickListener (new OnClickListener() {

                @Override
                public void onClick(View v) {
                    //check login
                    String username = lsUsername.getText().toString();
                    String password = lsPassword.getText().toString();
                    try{
                        if(LoginDB.Login(username,password)) {
                            Intent goToNextActivity = new Intent(getApplicationContext(), menu.class);
                            startActivity(goToNextActivity);

                                Toast.makeText(LoginScrExample.this,"Login Sucessful",Toast.LENGTH_LONG).show();
                            }else{
                                Toast.makeText(LoginScrExample.this,"Invalid Username/Password",Toast.LENGTH_LONG).show();
                            }
                            }
                    catch(Exception err)
                       {
                           Toast.makeText(LoginScrExample.this," Something is wrong " + err.getMessage(),Toast.LENGTH_LONG).show();
                       }    

我的数据库类:

        public LoginDB(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub      

        String sqlDataStore = "create table if not exists " +
        TABLE_NAME_CREDENTIALS + " ("+ BaseColumns._ID + " integer primary key autoincrement,"

                    + COLUMN_NAME_USERNAME + " text not null,"
                    + COLUMN_NAME_PASSWORD + " text not null);";

        db.execSQL(sqlDataStore);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        if(oldVersion == 1 && newVersion == 2){
            //Upgrade the database
    }   

}

    public static boolean Login(String username, String password) {
        Cursor c =   DBHelper.getReadableDatabase().rawQuery(
                "SELECT * FROM " + TABLE_NAME_CREDENTIALS + " WHERE "
                        + COLUMN_NAME_USERNAME + "='" + username +"'AND "+COLUMN_NAME_PASSWORD+"='"+ password +"'" ,  null);
       if (c.getCount()>0)
          return true;
          return false;
    }

    public void open() {
        // TODO Auto-generated method stub
        db = DBHelper.getWritableDatabase();
    }
    public void close() {
        DBHelper.close();
    }

我也尝试了这个查询,但得到了相同的结果:

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

错误日志:

       08-23 13:38:24.034: W/System.err(1027): java.lang.NullPointerException
       08-23 13:38:24.034: W/System.err(1027):  at com.LoginScr.Example.LoginDB.Login(LoginDB.java:58)
       08-23 13:38:24.043: W/System.err(1027):  at com.LoginScr.Example.LoginScrExample$1.onClick(LoginScrExample.java:50)
       08-23 13:38:24.043: W/System.err(1027):  at android.view.View.performClick(View.java:3511)
       08-23 13:38:24.043: W/System.err(1027):  at android.view.View$PerformClick.run(View.java:14105)
       08-23 13:38:24.054: W/System.err(1027):  at android.os.Handler.handleCallback(Handler.java:605)
       08-23 13:38:24.054: W/System.err(1027):  at android.os.Handler.dispatchMessage(Handler.java:92)
       08-23 13:38:24.054: W/System.err(1027):  at android.os.Looper.loop(Looper.java:137)
       08-23 13:38:24.054: W/System.err(1027):  at android.app.ActivityThread.main(ActivityThread.java:4424)
       08-23 13:38:24.083: W/System.err(1027):  at java.lang.reflect.Method.invokeNative(Native Method)
       08-23 13:38:24.083: W/System.err(1027):  at java.lang.reflect.Method.invoke(Method.java:511)
       08-23 13:38:24.094: W/System.err(1027):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
       08-23 13:38:24.094: W/System.err(1027):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
       08-23 13:38:24.094: W/System.err(1027):  at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

1
"SELECT * FROM " + TABLE_NAME_CREDENTIALS + " WHERE "
                        + COLUMN_NAME_USERNAME + "='" + username +"'AND "+

在 AND 之前需要这个空间,所以它应该看起来像

DBHelper.getReadableDatabase().rawQuery(
                "SELECT * FROM " + TABLE_NAME_CREDENTIALS + " WHERE "
                        + COLUMN_NAME_USERNAME + " = '" + username +"' AND "+COLUMN_NAME_PASSWORD+" = '"+ password +"'" ,  null);
于 2012-08-23T18:39:58.917 回答