【问题标题】:How to make dynamic column using Android SQLite Database如何使用 Android SQLite 数据库制作动态列
【发布时间】:2013-04-25 17:57:21
【问题描述】:

我在 Android 上编写应用程序并使用 SQlite 数据库。 我希望能够通过用户选择将列添加到我的表中。 因此用户可以将他想要的任何列添加到表中。例如,用户有“动物”表,他想为“狗”、“猫”和“鱼”添加列。

我已经阅读了一些解决方案,但没有看到可以帮助我的解决方案。 我读到添加列的简单方法是使用:

        @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        // If you need to add a column
        if (newVersion > oldVersion) {
            db.execSQL("ALTER TABLE " + TableName + " ADD COLUMN " + ColumnName);
        }
}

但是我的问题是我无法选择将由用户选择添加到表中的列的名称是什么,字符串没有参数。 所以我尝试使用这样的东西,并直接调用它。

        @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion, String newColumnName) {

        // If you need to add a column
        if (newVersion > oldVersion) {
            db.execSQL("ALTER TABLE " + TableName + " ADD COLUMN " + newColumnName);
        }
}

但是我得到了这个方法的错误。

我还有一个关于数据库版本的问题。 调用 onCreate 时会自动调用 onUpgrade 方法。 在 onUpgrade 中有用于数据库版本的 oldVersion 和 newVersion 参数。什么时候设置 oldVersion 和 newVersion 参数?我如何将我的 newVersion 参数设置为 2,3,4...?

【问题讨论】:

  • 听起来像是糟糕的数据库设计。你确定你不能使用关系来达到同样的目的吗?
  • @Gustek 谢谢你的回答。你是什​​么意思关系?开更多桌?但后来我有同样的问题。因为我的桌号仅限于我最初设置的数量。如果我的用户想添加“鸟”表,我没有可以做到的方法。
  • 为什么它必须是列,而不是另一个表中的行和多对多关系?您尝试存储的数据结构是什么?
  • 我正在尝试做购物清单日志。因此用户可以添加他想要的任何项目,并且该项目将作为新列添加到表中。如果不正确放入新表,当用户按“添加项目”或类似的东西时,我怎么能这样做,将创建一个新表。
  • 添加新行而不是列。

标签: android sqlite


【解决方案1】:

您可以创建一个辅助表来保存额外的列数据。对主表的查询可以转换为对新视图的查询。

create table if not exists animal (pk integer primary key, name);

create table if not exists animal_aux (animal_pk, col_name, col_val);

create view if not exists animal_view
    as select animal.name as name,
              ct.col_val as cat,
              dt.col_val as dog
        from animal, animal_aux as ct, animal_aux as dt
        where animal.pk = ct.animal_pk
          and animal.pk = dt.animal_pk
          and ct.col_name = 'cat'
          and dt.col_name = 'dog'
;

应增强此架构以使 animal_pk, col_name 成为主键,或者至少在 animal_aux 中使 unique。当您在animal 表中插入或删除条目时,您可能还需要触发器来添加或删除辅助表中的条目。

例子:

sqlite> select * from animal_view;
sqlite> insert into animal values (NULL, 'fred');
sqlite> select * from animal_view;
sqlite> select * from animal;
1|fred
sqlite> insert into animal_aux values (1, "dog", "beagle");
sqlite> insert into animal_aux values (1, "cat", "siamese");
sqlite> select * from animal_view;
fred|siamese|beagle
sqlite> 

每次添加虚拟列时,都需要

drop view animal_view;

然后使用适当的额​​外列和where 子句重新创建它。

【讨论】:

  • 感谢您的回答道格·柯里。但我不确定我是否理解你的回答。这段代码是用 Java 编写的吗?你写的第一个代码,是在什么方法下写的?在升级?这个方法的参数是什么?
  • 代码在 SQL 中,并使用 SQLite shell 进行了测试。在您的应用程序中,您必须构造这些语句并使用 execSQL 执行它们。
【解决方案2】:

final static String Database_name="empDb.db";

public DatabaseHelper(Context context) {
    super(context, Database_name, null, 1);
}
@Override    public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table emp_tbl (id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT,salary TEXT)");
}

@Override    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS tbl_emp");
}

博客:https://itmulc.blogspot.com/2016/08/android-sqlite-database-with-complete.html 获取有关它的更多信息。 https://www.youtube.com/watch?v=W8-Z85oPNmQ

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-29
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    • 2019-12-20
    • 1970-01-01
    • 2011-01-08
    • 1970-01-01
    相关资源
    最近更新 更多