Eamon Mac
Eamon Mac

Reputation: 181

How to retrieve data using foreign key SQLite?

I have a simple app that has two tables in a database. A logged in user adds "players" and the users id is stored as the foreign key in the "players" table Right now my code retrieves all the records regardless of the logged in user. How do i change my query to select only the data entered by the logged in user i.e. the foreign key?

"where" sql statement gives me an error if placed below

My query code
===========


  public Cursor getAllRows(){
        SQLiteDatabase db = this.getReadableDatabase();
       Cursor cursor = db.rawQuery("select  Player_id _id, * from Player"  
, null);


      return cursor;
  }

Upvotes: 0

Views: 790

Answers (1)

forpas
forpas

Reputation: 164064

You need a query like:

Cursor cursor = db.rawQuery("select * from Player where user_id = " + userid, null);

where user_id is the column in the table Player containing the user's id and userid is a variable where the user's id is stored.
If the user's id is a string (data type TEXT) then it must be enclosed in single quotes:

Cursor cursor = db.rawQuery("select * from Player where user_id = '" + userid + "'", null);

Upvotes: 1

Related Questions