2

我正在尝试从 json 对象插入数据,以下代码是关于我使用的表的

我这样定义了数据库助手类:

class DatabaseHelper {

    static DatabaseHelper _databaseHelper;    // Singleton DatabaseHelper
    static Database _database;                // Singleton Database

    String category_Table = 'category_Table';
  String category_id  = 'category_id';
    String device_type_id = 'device_type_id';
    String room_id  = 'room_id ';
...

await db.execute(
      'CREATE TABLE $category_Table($category_id INTEGER PRIMARY KEY UNIQUE  , $device_type_id INTEGER, '
                '$room_id INTEGER)');
        print('category created!');

这是插入功能

        Database db = await this.database;
    var result = await db.insert(category_Table, category.toMap());
    print('category inserted');
        return result;


    }

这是错误

Exception has occurred.
SqfliteDatabaseException (DatabaseException(table category_Table has no column named category_id (code 1): , while compiling: INSERT INTO category_Table (category_id, device_type_id, room_id) VALUES (?, ?, ?)) sql 'INSERT INTO category_Table (category_id, device_type_id, room_id) VALUES (?, ?, ?)' args [1, 1, 1]})

感谢您的帮助:)

4

5 回答 5

4

1)首先检查您在模型中使用的相同变量文本,Map是否与您的列同名

2) 更新数据库版本号

3)在您的手机或模拟器上卸载并重新安装该应用程序。

于 2019-07-24T11:12:47.933 回答
1

对我来说,错误是因为在上一列之后缺少逗号和空格。我有什么:

import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';

final String categoryTable = "categoryTable";
final String idColumn = "idColumn";
final String nameColumn = "nameColumn";
final String weightColumn = "weightColumn";
final String typeColumn = "typeColumn";
final String gradeColumn = "gradeColumn";
final String professorColumn = "professorColumn";
final String colorColumn = "colorColumn";

db.execute(
  "CREATE TABLE $categoryTable("
    "$idColumn INTEGER PRIMARY KEY, "
    "$nameColumn TEXT, "
    "$weightColumn NUMERIC, "
    "$typeColumn NUMERIC" // <- missing comma and space
    "$professorColumn TEXT, " // Error when I try to add something to this column
    "$colorColumn TEXT)"
);

为我解决的是:

db.execute(
  "CREATE TABLE $categoryTable("
    "$idColumn INTEGER PRIMARY KEY, "
    "$nameColumn TEXT, "
    "$weightColumn NUMERIC, "
    "$typeColumn NUMERIC, " // <- Fixed the error
    "$professorColumn TEXT, "
    "$colorColumn TEXT)"
);
于 2020-04-29T13:48:53.483 回答
1

我遇到了这个问题,我尝试了完全不同的事情,但最后,对我有用的东西写在下面

步骤 1:更改数据库版本号

之前是这样的

 static final dbVersion = 1;

我后来改成

static final dbVersion = 5;

这是我初始化数据库的函数。我正在创建一个包含四列的表格,即 id、title、description 和 date。

    Future _initDatabase() async {
    Directory _directory = await getApplicationDocumentsDirectory();
    var path = join(_directory.path, dbName);
    print(path);
    return await openDatabase(path, version: dbVersion,//here is the dbVersion
        onCreate: (Database db, int dbVersion) async {
      await db.execute(
          'CREATE TABLE $table($colId INTEGER PRIMARY KEY AUTOINCREMENT, $colTitle TEXT, $colDescription TEXT, $colDate TEXT)');
    });
  }

步骤 2:清除所有应用程序数据,然后重新运行应用程序。

我希望你的问题能得到解决:)

于 2021-04-27T06:11:24.163 回答
0

尝试删除应用程序存储并重新安装它,因为您可能已经以错误或旧方式在手机上创建了数据库文件,甚至缺少列。如果您正确使用 SQFLite,重新开始可能会奏效。

于 2020-07-01T10:21:59.907 回答
0

我使用的toMAP方法的变量与听写中的数据库助手类不同

于 2019-05-10T22:54:34.000 回答