靠近" @ gmail"语法错误

时间:2015-01-23 05:07:50

标签: android sqlite android-activity login logcat

我正在使用布尔函数创建登录活动但是在匹配电子邮件地址与数据库中的电子邮件地址时会收到错误但是会出错。

这是我的dbHandler类方法:

 public Boolean loginCheck(String email,String password)
{
    Boolean flag=false;
    String selectQuery = "SELECT  * FROM " + TABLE_NAME + " WHERE email=" + email +" AND pass="+password;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    // Move to first row
    cursor.moveToFirst();
    if(cursor.getCount() == 1){
      flag=true;
    }
    else
    {
      flag=false;
    }

    return flag;
}

这是java文件代码

login.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String emailValue=edtEmail.getText().toString();//this is EditText
            String passValue=edtPass.getText().toString();

            if(db.loginCheck(emailValue, passValue))
            {
                Intent in = new Intent(getApplicationContext(), Home.class);
                startActivity(in);
                Toast.makeText(getApplicationContext(), "Login Successful", 2000).show();
            }
            else
            {
                Toast.makeText(getApplicationContext(), "Username and Password not match", 5000).show();
            }
        }
    });

得到错误

 01-23 04:58:30.519: E/AndroidRuntime(2422):                                                     android.database.sqlite.SQLiteException: near "@gmail": syntax error (code  1): , while compiling: SELECT  * FROM login WHERE email=stackoverflow@gmail.com AND pass=stackoverflow
 01-23 04:58:30.519: E/AndroidRuntime(2422):    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
  01-23 04:58:30.519: E/AndroidRuntime(2422):   at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)

请提供解决方案

2 个答案:

答案 0 :(得分:5)

问题是你没有用单引号传递字符串。

最好将rawQuery与参数'String array as:

一起使用
Cursor cursor = db.rawQuery(
        "select * from "+TABLE_NAME +" where email = ? and pass = ? ", new String[]{email, password});

希望这有帮助。

答案 1 :(得分:1)

您需要将@gmail放在引号中 - “/ '的Gmail /'”

tring selectQuery = "SELECT  * FROM " + TABLE_NAME + " WHERE email=\'"
+ email+"\'" +" AND pass=\'"+password+"\'";

您需要将字符串放在引号中。