开发者

How do i design database and get data by Category in android

For example data if i categorize data to 3 group : Attraction, City landmark, Park

id      title           content          address           category

1     GeneralPark     xxxxxxxxxxx      1234road         Park

2     GreatMuseum      wwwwwwwwww       9877road         Attraction

Look at General Park. its category is Park. and it also in Attraction category. the question is .. how can i design database? Is it possible to do like the following(put Park and Attraction in the same column):

id      title           content          address           category

1     GeneralPark     xxxxxxxxxxx      1234road         Park,Attraction

So, if it's not, then how can i design database? and How should i implement this line to retrive data by category; i.e. By Attraction

public Cursor getplaces() 
{
    Cursor 开发者_运维百科c = mDb.query(Constants.TABLE_NAME_PLACE, null, null, null, null, null, null);
    return c;

}

The expect result should show only Attraction Category:

id      title           content          address           category

1     GeneralPark     xxxxxxxxxxx      1234road          Attraction

2     GreatMuseum      wwwwwwwwww       9877road         Attraction

4     Middlehigh-school ttttttttt       555road           Attraction

5     ShoppingMall   ssssssssss        666road           Attraction

Thanks so much for your help. i'm quite new to developing. Thanks you


Don't put two things (i.e. 'Park,Attraction') in one column, that is almost always a mistake. The usual way to deal with this situation is to add another table with two columns:

create table place_categories (
    place_id int          not null references place(id),
    category varchar(255) not null,
    primary key (place_id, category)
)

You'd usually make a two column primary key to avoid duplicates as well, hence the primary key constraint at the bottom. A foreign key on place_id would be a good idea too, referential integrity saves a lot of pain, suffering, and confusion.

And then, if you want to work with the categories for a place, just join to place_categories:

select p.id
from place p
join place_categories c on (p.id = c.place_id)
where c.category = 'Attraction'

With this approach, your place table would look something like this:

create table place (
    id      int          not null primary key,
    title   varchar(100) not null,
    content varchar(100) not null,
    address varchar(100) not null
)

and the data would look something like this:

id      title           content          address
1     GeneralPark      xxxxxxxxxxx      1234road
2     GreatMuseum       wwwwwwwwww      9877road


Ok, first things first, learn about relational databases, and normalization. It may be a trivial thing for you, but it's not. Maybe someone can give you the code to solve this, but you won't learn anything from that.

http://en.wikipedia.org/wiki/Database_normalization

http://en.wikipedia.org/wiki/Relational_database

Then, you'll read some SQL to access files in a smart way. example of SQL> "SELECT DATE, NAME, ADDRESS FROM People WHERE NAME='Jane'"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜