“@gmail”附近的语法错误
near "@gmail" Syntax error
我正在使用布尔函数创建登录名 activity,但在将电子邮件地址与数据库中的电子邮件地址进行匹配时出现错误,但出现错误。
这是我的dbHandler
class方法:
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=Whosebug@gmail.com AND pass=Whosebug
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)
请提供解决方案
问题是您没有在单引号中传递字符串。
最好将 rawQuery
与参数的字符串数组一起使用:
Cursor cursor = db.rawQuery(
"select * from "+TABLE_NAME +" where email = ? and pass = ? ", new String[]{email, password});
希望对您有所帮助。
您需要将您的@gmail 放在引号中,因为-
"/'gmail/'"
tring selectQuery = "SELECT * FROM " + TABLE_NAME + " WHERE email=\'"
+ email+"\'" +" AND pass=\'"+password+"\'";
您需要将字符串放在引号中。
我正在使用布尔函数创建登录名 activity,但在将电子邮件地址与数据库中的电子邮件地址进行匹配时出现错误,但出现错误。
这是我的dbHandler
class方法:
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=Whosebug@gmail.com AND pass=Whosebug
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)
请提供解决方案
问题是您没有在单引号中传递字符串。
最好将 rawQuery
与参数的字符串数组一起使用:
Cursor cursor = db.rawQuery(
"select * from "+TABLE_NAME +" where email = ? and pass = ? ", new String[]{email, password});
希望对您有所帮助。
您需要将您的@gmail 放在引号中,因为- "/'gmail/'"
tring selectQuery = "SELECT * FROM " + TABLE_NAME + " WHERE email=\'"
+ email+"\'" +" AND pass=\'"+password+"\'";
您需要将字符串放在引号中。