I’m quite new to SQLite and SQL and I am struggling with how to approach the following:
My app will display a list of community members. If I click a member, I can see a list of posts made by the members. A post is an object with name, time and message. How can I store this in an SQLite database so that I can query the database by userid and get the list of posts for a specific user.
I have a Users table with these columns:
USER_ID | NAME
I have a Tweet table with these columns:
USER_ID | NAME | TIME | MESSAGE
My questions are: what the best approach / structure to link these two tables? Do I create a new tweet table for every user, or do I store all tweets in one long table with tweets for user 1 first then for user 2 etc?
I’m not necessarily looking for code dumps but rather an explanation of the logic.
This is a typical “JOIN” scenario where you have a one-to-many relationship between Users and Posts.
Here is an example of a query that would display all users and their posts:
This will produce a resultset with four columns. Each “Tweet” will be displayed with its related User record. The ‘u.’ and ‘p.’ syntax are table aliases used to make the query easier to read.