Sonhja
Sonhja

Reputation: 8448

How to retrieve a specific row using SQLite and Cursors?

I want to retrieve a specific user from a SQLite database on Android with a provided username using a Cursor. I've tried this:

String[] fields = new String[] { "username", "email", "dateRegister" };
Cursor c = db.query(tableName, fields, "username ='1234'", null);

But it isn't working. How can I retrieve a specific row with the information of a unique column?

Upvotes: 2

Views: 5838

Answers (2)

Mstack
Mstack

Reputation: 321

try this

db.query(tableName, null, "username = ?", new String[]{"username"}, null, null, null);

Upvotes: 4

mohamed
mohamed

Reputation: 303

lots of guys asked the same question ,so please search efficient.

String query = "select * from " + tableName + " where "+ KEY_USERNAME + " = '" + uname + "'";
SQLiteDatabase sql = this.getReadableDatabase();
Cursor cur = sql.rawQuery(query, null);
return cur;

Upvotes: 1

Related Questions