如何使用androidsqlite数据库制作动态列

如何使用androidsqlite数据库制作动态列,android,sqlite,Android,Sqlite,Im在Android上编写应用程序,Im使用SQlite数据库。 我希望能够通过用户选择向表中添加列。 因此,用户可以向表中添加他想要的任何列。例如,用户有“动物”表,他想为“狗”、“猫”和“鱼”添加列 我读过一些解决方案,但没有一个能帮到我。 我了解到添加列的简单方法是使用: @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // I

Im在Android上编写应用程序,Im使用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);
        }
}
但我的问题是,我无法选择将由用户选择添加到表中的列的名称,因为string没有参数。 所以我试着用这样的方法,直接调用它

        @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 get时,将自动调用onUpgrade方法get。
在onUpgrade中,数据库版本有oldVersion和newVersion参数。何时设置oldVersion和newVersion参数?如何将newVersion参数设置为2,3,4…?

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

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\u pk、col\u name
成为主键,或至少在
animal\u aux
中使
唯一。在
animal
表中插入或删除条目时,您可能还需要触发器来添加或删除aux表中的条目

例如:

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
子句重新创建它。

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

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\u pk、col\u name
成为主键,或至少在
animal\u aux
中使
唯一。在
animal
表中插入或删除条目时,您可能还需要触发器来添加或删除aux表中的条目

例如:

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
子句重新创建它。

最终静态字符串数据库\u name=“empDb.db”

博客: 获取更多关于它的信息。

最终静态字符串数据库\u name=“empDb.db”

博客: 获取更多关于它的信息。

听起来像是糟糕的数据库设计。“你确定不能用关系来达到同样的目的吗?”古斯泰克谢谢你的回答。什么是关系?要开更多的桌子吗?但我也有同样的问题。因为我的桌号仅限于我最初设置的桌号。如果我的用户想添加“Bird”表,我没有方法可以这样做。为什么它必须是列,而不是另一个表中的行和多对多关系?你想存储什么样的数据结构?我想记录购物清单。因此,用户可以添加他想要的任何项,并且该项将作为新列添加到表中。如果我要放入新表,当用户按下“添加项”或类似的按钮时,我如何才能做到这一点,将创建一个新表。添加新行而不是列。听起来像是糟糕的数据库设计。“你确定不能用关系来达到同样的目的吗?”古斯泰克谢谢你的回答。什么是关系?要开更多的桌子吗?但我也有同样的问题。因为我的桌号仅限于我最初设置的桌号。如果我的用户想添加“Bird”表,我没有方法可以这样做。为什么它必须是列,而不是另一个表中的行和多对多关系?你想存储什么样的数据结构?我想记录购物清单。因此,用户可以添加他想要的任何项,并且该项将作为新列添加到表中。如果我要放入新表,当用户按“添加项”或类似的按钮时,我如何才能这样做,将创建一个新表。添加新行而不是列。谢谢您的回答Doug Currie。但我不确定我是否理解你的答案。这段代码是用Java编写的吗?你写的第一个代码是用什么方法写的?升级?这个方法的参数是什么?代码是用SQL编写的,并用SQLite外壳进行了测试。在您的应用程序中,您必须构造这些语句并使用execSQL执行它们。感谢您的回答Doug Currie。但我不确定我是否理解你的答案。这段代码是用Java编写的吗?你写的第一个代码是用什么方法写的?升级?这个方法的参数是什么?代码是用SQL编写的,并用SQLite外壳进行了测试。在应用程序中,必须构造这些语句并使用execSQL执行它们。