Android sqlite - Return _id logged ListView only a value selected using Where condition

0

Good evening guys, I'm new here, I'm new to Android programming, but I decided to do my TCC, an Android project. by doing it alone, finding stuff on the internet, without taking Android classes, I'm going to do it well (or not), but I ran aground at a certain point.

I have a ListView that returns me the data from the SQLite database.

If I leave SELECT * FROM tbl_user; it brings me all the users registered in the database, but I just wanted to let the user who logged in to the system appear.

That is, I wanted to make a condition inside the Select, getting "SELECT * FROM tbl_user WHERE _id = id that is logged into the system."

But I can not, I've already lost two days looking for a way to do it, I've already changed the code structure and I'm lost.

Well, my code is there.

public class Area_Usuario extends ActionBarActivity{
private String TABLE;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.area_usuario);
    TABLE = "tbl_usuario";


    DatabaseHandler db = new DatabaseHandler(this);
    Usuario usuario = new Usuario();
    SQLiteDatabase db1 = db.getReadableDatabase();
    String sql = ("SELECT * FROM " + TABLE);
    Cursor cursor= db1.rawQuery(sql,new String[]{String.valueOf(usuario.get_id())});
    String[] from = {"nome", "email", "senha"};
    int[] to = {R.id.Usuario_RowNome, R.id.Usuario_RowEmail, R.id.Usuario_RowSenha};
    ListView ltUsuario = (ListView) findViewById(R.id.listViewDadosUsuario);
    SimpleCursorAdapter ad = new SimpleCursorAdapter(getBaseContext(), R.layout.usuario_row, cursor, from, to, 1);
    ltUsuario.setAdapter(ad);
    db1.close();
   }
}
    
asked by anonymous 01.04.2015 / 07:04

1 answer

1

If you want to use the WHERE clause you will need to indicate it in the Query:

String sql = ("SELECT * FROM " + TABLE + " WHERE _id = ?");
Cursor cursor= db1.rawQuery(sql,new String[]{String.valueOf(usuario.get_id())});

? is a 'placeholder' to receive the value indicated in new String[]{String.valueOf(usuario.get_id())}

I suggest that instead of db.rawQuery , use the methods SQLiteDatabase makes available for CRUD operations.

In the case of reading data, you must use one of the variants of SQLiteDatabase.query

public Cursor query(String table, String[] columns, String selection, String[] selectionArgs,
                    String groupBy, String having, String orderBy)

It would look like this:

Cursor cursor = db1.query(TABLE, null, "_id = ?",
                           new String[]{String.valueOf(usuario.get_id())},null,null,null);
    
01.04.2015 / 10:46