【问题标题】:near "@gmail" Syntax error“@gmail”附近的语法错误
【发布时间】:2015-01-23 05:07:50
【问题描述】:

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

这是我的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)

请提供解决方案

【问题讨论】:

  • 你需要转义你的参数。使用query 代替rawQuery 并传递email 参数。

标签: android sqlite android-activity login logcat


【解决方案1】:

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

最好将rawQuery 与参数的字符串数组一起使用:

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

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    您需要将您的@gmail 放在引号中 - "/'gmail/'"

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-22
      • 2017-08-31
      • 2023-03-03
      • 2016-02-16
      • 2018-08-21
      • 2017-01-12
      • 2014-03-12
      • 1970-01-01
      相关资源
      最近更新 更多