简体   繁体   中英

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 ie 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;
  }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM