【问题标题】:How to add more column after table has been created in sqflite flutter如何在 sqflite 颤振中创建表后添加更多列
【发布时间】:2021-12-28 04:53:53
【问题描述】:

我创建了一个数据库,其中包含一个名为table_x 的表。 table_x 有 3 列(id、name、address)。这是创建表时的代码

Future create(Database db) async {
    await db.execute('''
          CREATE TABLE $table (
            $id INTEGER PRIMARY KEY,
            $name TEXT NOT NULL,
            $addr TEXT NOT NULL
          )
          ''');
  }

创建表后,我尝试通过添加一行来再添加一列:

Future create(Database db) async {
    await db.execute('''
          CREATE TABLE $table (
            $id INTEGER PRIMARY KEY,
            $name TEXT NOT NULL,
            $addr TEXT NOT NULL,
            $phone TEXT NOT NULL
          )
          ''');
  }

但是结果是报错:

DatabaseException(Error Domain=FMDatabase Code=1 "table table_x has no column named phone" UserInfo={NSLocalizedDescription=table table_x has no column named phone}) sql 'INSERT INTO table_x (id,name,addr) VALUES (?, ?, ?)' args [.........]}

有没有办法在创建表后修改数据列?

【问题讨论】:

标签: flutter dart sqflite


【解决方案1】:

按照此模式添加新列

ALTER TABLE database_name.table_name ADD COLUMN column_def...;

您正在使用 create table 语句在现有表中创建新列,这是错误的。

alter table 语句用于向现有表添加新列

【讨论】:

  • 很抱歉我的回复晚了,非常感谢...您的发言确实帮助我了解更多关于为什么我需要使用alter
猜你喜欢
  • 1970-01-01
  • 2020-08-29
  • 2021-08-05
  • 2022-10-13
  • 2021-10-10
  • 2021-05-07
  • 2020-06-01
  • 2022-01-18
  • 2019-10-14
相关资源
最近更新 更多