Dart 颤振数据库异常(表类别\u表没有名为category\u id的列)
我试图从json对象插入数据,下面的代码是关于我使用的表的 我这样定义了数据库帮助器类:Dart 颤振数据库异常(表类别\u表没有名为category\u id的列),dart,flutter,sqflite,Dart,Flutter,Sqflite,我试图从json对象插入数据,下面的代码是关于我使用的表的 我这样定义了数据库帮助器类: class DatabaseHelper { static DatabaseHelper _databaseHelper; // Singleton DatabaseHelper static Database _database; // Singleton Database String category_Table = 'category_
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 ';
...
这里是insert函数
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]})
感谢您的帮助:)我使用的变量与口述中的数据库帮助器类不同1)首先检查您在模型中使用的变量文本是否与您的列同名 2) 更新数据库版本号
3) 卸载并在手机或模拟器上重新安装应用程序。对于我来说,错误是因为上一列后面缺少逗号和空格。 我所拥有的:
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)"
);
import'包:sqflite/sqflite.dart';
导入“package:path/path.dart”;
最终字符串categoryTable=“categoryTable”;
最后一个字符串idColumn=“idColumn”;
最终字符串nameColumn=“nameColumn”;
最终字符串weightColumn=“weightColumn”;
最终字符串typeColumn=“typeColumn”;
最终字符串gradeColumn=“gradeColumn”;
最后一个字符串professorColumn=“professorColumn”;
最后一个字符串colorColumn=“colorColumn”;
执行(
“创建表$categoryTable(”
$idColumn INTEGER主键
$nameColumn文本
$weightColumn数值
“$typeColumn NUMERIC”//尝试删除应用程序存储并重新安装,因为您可能已经以错误或旧的方式在手机上创建了数据库文件,甚至丢失了一列。如果您正确使用SQFLite,新的开始可能会起作用。我遇到了这个问题,尝试了完全不同的方法,但最后,对我有效的方法写在下面w
步骤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:清除所有应用程序数据,然后重新运行应用程序
我希望您的问题能够得到解决:)很抱歉,我的回答不正确。当您调用创建表sql代码时,可以尝试从您的设备/仿真器中删除应用程序(这样会删除数据库)然后重新安装?我这样做并将Unique更改为AUTOINCREMENT,但以下查询仍然存在语法错误``wait db.execute('CREATE TABLE$roomTable($colRoomId INTEGER主键自动递增,$colRoomTitle TEXT,$colRoomImage TEXT,$colRoomDevNo INTEGER,$colRoomDesc TEXT,$colRoomCode INTEGER);“`near.”:语法错误
static final dbVersion = 5;
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)');
});
}