SQLiteDatabase:创建多个表在运行时动态? [英] SQLiteDatabase: Create multiple tables dynamically at runtime?

查看:234
本文介绍了SQLiteDatabase:创建多个表在运行时动态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我创造,需要我来存储多个播放列表在内存中的应用程序。每个播放列表由一组歌曲按指定顺序的。显然是不合理的,存储在数据库本身整个MP3。我将其存储的方式是,存储其名称,位置,艺术家,专辑和一个唯一的ID号。

So I am creating an application that requires me to store multiple playlists in memory. Each playlists consists of a group of songs in specified order. Obviously it is unreasonable to store the entire mp3 in the database itself. The way I am storing it is, storing its Name, Location, Artist, Album and a unique ID number.

我的问题是这样的:

如何使它所以每个播放列表可以是自己的数据库中的表?

How to make it so each playlist can be its own table in the database?

因此​​,每个播放列表的表看起来是这样的:

So each playlist table looks like this:

Playlist {
    _id    PRIMARY KEY AUTOINCREMENT INTEGER,
    title  TEXT,
    uri    TEXT, // Location of file in the phone
    album  TEXT,
    artist TEXT
}

每个教程中我已经寻找了表predefined,不能在运行时动态创建的。

Every tutorial I have searched for has tables predefined, not dynamically created at runtime.

另外,如果你们认为创建一个新表,每个播放列表是不是去最好的办法,你有什么建议?

Also if you guys think that creating a new table for each playlist is not the best way to go about this, what do you suggest?

推荐答案

不要在运行时动态这太疯狂了创建表。为此,您可以用一个非常简单的表结构,这是任何人都能轻松理解应程序永远不会给别人。

Do not create tables dynamically at runtime that's crazy. You can do this with a very simple table structure, and it's easy for anyone to understand should your program ever be given to someone else.

所有你需要做的就是创建一个播放列表表,PlaylistItems。播放列表将包含在最低限度(ID,姓名)。 PlayListItems会(ID,playlist_id,艺术家,专辑,位置排序)。 PlayListItems.playlist_id将是一个外键返回到PlayList.id领域。通常情况下,很容易,因为你的名字显示播放列表给用户。然后,当它的点击,你会成为一个独立的电话来​​获取播放列表中的内容(选择PlayListItems *其中playlist_id =?)。

All you need to do is create a Playlist table, and PlaylistItems. Playlist would contain at a bare minimum (id, name). PlayListItems would be (id, playlist_id, artist, album, location, ordering). PlayListItems.playlist_id would be a foreign key back to the PlayList.id field. Normally that's easy because you'll display a list of playlists by name to the user. Then when the click on it you'll make a separate call to fetch the contents of the playlist (select * from PlayListItems where playlist_id=?).

另一种方法是简单地拥有PlayListItems包含(ID,playlist_id,media_id,排序),其中media_id将是一个外键到媒体表,该表将持有你的设备上找到的所有MP3音乐。在这种情况下,你不会需要复制的歌手名,专辑和文件位置成表,它可以帮助节省空间,以及保持任何重复工作来更新这些表应媒体的变化。缺点是,你需要加入PlayListItems与媒体合作,得到的信息,但在表中的索引应该保持它的表演很快。

An alternative would be to simply have PlayListItems contain (id, playlist_id, media_id, ordering) where media_id would be a foreign key into your Media table which would hold all MP3s you've found on the device. In that case you wouldn't need to duplicate the artist name, album, and file location into that table which can help save space as well as keep any duplication of work to update those tables should media change. The downside is you'll need to join PlayListItems and Media together to get the information, but an index on the table should keep it performing quickly.

这篇关于SQLiteDatabase:创建多个表在运行时动态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆