2

In my Android app, I need to temporarily store some data in a form of table such as follows:

id | column 1 | column 2 | ... | column n

The data are downloaded from a server whenever users press a button. However, the data table doesn't have a fix number of column (as well as row) every time user downloads it from the server. For example, the server may send data with 3 columns the first time. Then it might send data with 5 columns the second time, etc...

Given this scenario, I think the database is probably the right data structure to use. My plan is to create a database, then add and delete tables as necessary. So I have been reading various tutorials on Android database (one example is this one http://www.codeproject.com/Articles/119293/Using-SQLite-Database-with-Android#). It seems to me I cannot create new table with variable number of columns using the sqlite database. Is this correct? In the onCreate(SQLiteDatabase db) method, the "create table" command must be specified with known number of columns and their data types. I could provide several "create table" commands, each with different number of columns but that seems like very crude. Is there a way to create database tables with variable number of columns on the fly?

Another alternative probably using several hash tables, each storing one column of the data table. I'm seriously considering this approach if the database approach is not possible. Any better suggestion is welcomed.

4

1 回答 1

5

SQLite 数据库中没有可变数量的列。此外,动态添加和删除表似乎是一种可怕的 hack。

听起来您想存储与 id 关联的值数组。我建议您考虑行,而不是列。使用像 (id, index, value) 这样的表结构;服务器返回的每个值数组都会产生存储值所需的行数。

于 2012-07-03T21:20:31.397 回答