Error android.database.sqlite.SQLiteException

0

This method of my activity:

private void loginEmailSenha() {

        editTextEmailLogin.setError(null);
        editTextSenhaLogin.setError(null);

        controllerUsuario = new ControllerUsuario(this);
        String email = editTextEmailLogin.getText().toString();
        String senha = editTextSenhaLogin.getText().toString();

        boolean cancel = false;
        View focusView = null;

        if (TextUtils.isEmpty(email)) {
            editTextEmailLogin.setError(getString(R.string.error_field));
            focusView = editTextEmailLogin;
            cancel = true;

        }

        if (TextUtils.isEmpty(senha)) {
            editTextSenhaLogin.setError(getString(R.string.error_field));
            focusView = editTextSenhaLogin;
            cancel = true;

        }

        if (cancel) {
            focusView.requestFocus();

        } else {

            controllerUsuario.buscarEmailSenha(email, senha);

            Intent intent1 = new Intent(getApplicationContext(), OptionsUserActivity.class);
            startActivity(intent1);
            limparCampos();

        }
    }

controller method:

public User buscarEmailSenha(String email, String senha) {
        return ir.buscarEmailSenha(email, senha);
    }

method of my repository:

@Override

public User buscarEmailSenha(String email, String senha) {

User user = null;

        Cursor cursor = abrirBanco().rawQuery("SELECT * FROM " + SqliteHelper.NOME_TABELA + " WHERE " +
            sqliteHelper.EMAIL + " = '" + email + "' AND " + sqliteHelper.SENHA + " = '" + senha + "'", new String[]{email, senha} );

        if(cursor.moveToFirst()) {
            user = new User();
            user.setEmail(cursor.getString(cursor.getColumnIndex("email")));
            user.setSenha(cursor.getString(cursor.getColumnIndex("senha")));
        }

        return user;

You are generating the following error:

  

android.database.sqlite.SQLiteException: near "@fteste": syntax error   (code 1):, while compiling: SELECT * FROM user WHERE email =   [email protected] AND password = teste123.

Class SqliHelper

 public static final String NOME_TABELA = "usuario";
 public static final String NOME = "nome";
 public static final String EMAIL = "email";   
 public static final String TELEFONE = "telefone";   
 public static final String SENHA = "senha";
 public static final String COLUNA_ID = "_id";

 public static final String TABLE_USER = "CREATE TABLE " + NOME_TABELA "( "

 COLUNA_ID + " INTEGER PRIMARY KEY NOT NULL, "            
 NOME + " TEXT NOT NULL,"            
 EMAIL + " TEXT NOT NULL,"            
 TELEFONE + " TEXT NOT NULL,"            
 SENHA + " TEXT NOT NULL)";

 @Override

 public void onCreate(SQLiteDatabase db) {

       db.execSQL(TABLE_USER);        
       db.execSQL(TABLE_CARONA);

  }
    
asked by anonymous 23.10.2015 / 01:28

1 answer

2

If you add the parameters in SQL ( sqliteHelper.EMAIL + " = '" + email + ), there is no need for the second parameter in rawQuery .

Example:

Cursor cursor = abrirBanco().rawQuery("SELECT * FROM " + SqliteHelper.NOME_TABELA + " WHERE " +
            sqliteHelper.EMAIL + " = '" + email + "' AND " + sqliteHelper.SENHA + " = '" + senha + "'", null );

or

         Cursor cursor = abrirBanco().rawQuery("SELECT * FROM " + SqliteHelper.NOME_TABELA + " WHERE " +
                sqliteHelper.EMAIL = ? AND " + sqliteHelper.SENHA + " = ?", new String[]{email, senha} );

The number of Array parameters must equal the amount of '?' in sql.

I hope I have helped!

Greetings

Note:

To make it go to the next screen only if there is a user in the database, try this:

  if (cancel) {
        focusView.requestFocus();

    } else {

         User user = controllerUsuario.buscarEmailSenha(email, senha);

       if(null != user){
        Intent intent1 = new Intent(getApplicationContext(),         OptionsUserActivity.class);
        startActivity(intent1);

        }else{
          // Nao existe usuario com este login e senha
        }

    }
    
23.10.2015 / 02:10