Return string in a query in SQLITE

0

I need to return a user's name in an SQLITE query: I'm using the following code:

public String verificarUsuario(String login)
    {

        try
        {

        String selectQuery = "select nome from usuarios where login = " + login;

        Cursor cursor = db.rawQuery(selectQuery, null);
        cursor.moveToFirst();

        String nomeString = cursor.getString(cursor.getColumnIndex("tipoFunc"));

        StringBuilder conversor = new StringBuilder();
        conversor.append(nomeString);
        return conversor.toString();


        }

But the following error is appearing:

  

no such column: lgomes (code 1):, while compiling: select name from users where login = lgomes

    
asked by anonymous 26.06.2017 / 20:44

1 answer

3

The user name must be enclosed in quotation marks to represent string .

If you do not use the quotation marks, the database will attempt to compare the login column with another column. See the error message:

  

no such column: lgomes

Free Translation

  

No column named lgomes

Your code should look something like:

String selectQuery = "select nome from usuarios where login = '" + login + "'";

The second error is because you have to use the index column:

String nomeString = cursor.getString(cursor.getColumnIndex("nome"))

It would be nice to note that assembling queries concatenating strings may leave your application susceptible to SQL injection attacks. Be careful with this.

Read these questions if you want to know more.

26.06.2017 / 20:46