Error android.database.sqlite.Sqliteexception

Asked

Viewed 186 times

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;

Is generating the following error:

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

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);

  }
  • Edit your question, providing details of the problem, just pasting the code makes it difficult to analyze and, consequently, help you solve. Enjoy and read [Ask]

1 answer

2


If you add the parameters in SQL (sqliteHelper.EMAIL + " = '" + email + ), there is no need for the second parameter in the 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 amount of array parameters must be equal to the amount of '?' in sql.

I hope I’ve helped!

Greetings

Obs.:

For it to pass the next screen only if there is user in the bank, 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
        }

    }
  • I could not solve problem, obg for helping! It do not give any error, if you put email and password not registered accepted.

  • Can’t? What happens? can you debug? Do you know if you enter? We are here to help you friend!

  • All methods starting Activity passing controller, getting Repository.

  • I think I understand what VC means.... There in your VC Activity you should receive the User and check if he is null.... Or better if he is not null. To then invoke the other Activity... See where VC calls the search methodNew()... Regardless of what returns in your controller, it will always call the Optionsuseractivity.

  • I don’t know what else to do.

  • I added the example of how to validate the User in the answer ! Analyze and in case of doubt, post so we can help!

  • generated following error android.database.Cursorindexoutofboundsexception: Index 0 requested, with a size of 0

  • try like this: "SELECT "Sqlitehelper.EMAIL+" , "+Sqlitehelper.PASSWORD+" FROM "

  • I tried so it didn’t work out.

  • show where you create the table

  • edited question

  • By chance you use the NOME_TABELA to create the ride table?

  • that’s right ....

  • Then it should be over writing the User table. Try creating with different table names

  • I’ll try here.

  • didn’t work out, I still have same problem. I thought about Webservice

Show 11 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.