如何更正错误 DatabaseException(靠近“)”:语法错误
How to correct error DatabaseException (near ")": syntax error
error DatabaseException(near ")": syntax error (code 1 SQLITE_ERROR): , 编译时: CREATE TABLE my_table (
I/flutter (21754): _id 整数主键,
I/flutter (21754): name TEXT NOT NULL,
I/flutter (21754): 地址 TEXT NOT NULL,
I/flutter (21754): 评论 TEXT NOT NULL,
I/flutter (21754): sigURL 文本不为空,
I/flutter (21754): selfpicURL 文本不为空,
I/flutter (21754):
I/flutter (21754): 你好 INT NOT NULL,
I/flutter (21754): 时间文本不为空,
import 'dart:io';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path_provider/path_provider.dart';
class DatabaseHelper {
static final _databaseName = "MyDatabase.db";
static final _databaseVersion = 1;
static final table = 'my_table';
static final columnId = '_id';
static final columnName = 'name';
static final address = 'address';
static final comments = 'comments';
static final sigURL = 'sigURL';
static final columnAge = 'hi';
static final selfpicURL = 'selfpicURL';
static final createdTime = 'time';
DatabaseHelper._privateConstructor();
static final DatabaseHelper instance = DatabaseHelper._privateConstructor();
static Database? _database;
Future<Database> get database async {
if (_database != null) return _database!;
// lazily instantiate the db the first time it is accessed
_database = await _initDatabase();
return _database!;
}
_initDatabase() async {
Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = join(documentsDirectory.path, _databaseName);
return await openDatabase(path,
version: _databaseVersion,
onCreate: _onCreate);
}
Future _onCreate(Database db, int version) async {
await db.execute('''
CREATE TABLE $table (
$columnId INTEGER PRIMARY KEY,
$columnName TEXT NOT NULL,
$address TEXT NOT NULL,
$comments TEXT NOT NULL,
$sigURL TEXT NOT NULL,
$selfpicURL TEXT NOT NULL,
$columnAge INT NOT NULL,
$createdTime TEXT NOT NULL,
)
''');
}
Future<int> insert(Map<String, dynamic> row) async {
Database db = await instance.database;
return await db.insert(table, row);
}
Future<List<Map<String, dynamic>>> queryAllRows() async {
Database db = await instance.database;
return await db.query(table);
}
Future queryRowCount() async {
Database db = await instance.database;
return Sqflite.firstIntValue(await db.rawQuery('SELECT COUNT(*) FROM $table'));
}
Future<int> update(Map<String, dynamic> row) async {
Database db = await instance.database;
int id = row[columnId];
return await db.update(table, row, where: '$columnId = ?', whereArgs: [id]);
}
Future<int> delete(int id) async {
Database db = await instance.database;
return await db.delete(table, where: '$columnId = ?', whereArgs: [id]);
}
}
SQLite SQL 解析器不支持结束括号前的尾随逗号
而不是(在你的声明末尾)
...
$createdTime TEXT NOT NULL,
)
做
...
$createdTime TEXT NOT NULL
)
create table $tableTodo (
$columnId integer primary key autoincrement,
$columnTitle text not null,
$columnDone integer not null)
''');
如果你仔细查看最后一行 $columnDone integer not null 它没有像其他 2 行一样的逗号,那是你的代码中的问题。
编码愉快
SQLite 不支持尾随逗号。尝试删除关于创建 table 的最后一条语句中的逗号。例如
如果你是这样写的;
$createdTime TEXT NOT NULL,
试试看;
$createdTime TEXT NOT NULL
error DatabaseException(near ")": syntax error (code 1 SQLITE_ERROR): , 编译时: CREATE TABLE my_table ( I/flutter (21754): _id 整数主键, I/flutter (21754): name TEXT NOT NULL, I/flutter (21754): 地址 TEXT NOT NULL, I/flutter (21754): 评论 TEXT NOT NULL, I/flutter (21754): sigURL 文本不为空, I/flutter (21754): selfpicURL 文本不为空, I/flutter (21754): I/flutter (21754): 你好 INT NOT NULL, I/flutter (21754): 时间文本不为空,
import 'dart:io';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path_provider/path_provider.dart';
class DatabaseHelper {
static final _databaseName = "MyDatabase.db";
static final _databaseVersion = 1;
static final table = 'my_table';
static final columnId = '_id';
static final columnName = 'name';
static final address = 'address';
static final comments = 'comments';
static final sigURL = 'sigURL';
static final columnAge = 'hi';
static final selfpicURL = 'selfpicURL';
static final createdTime = 'time';
DatabaseHelper._privateConstructor();
static final DatabaseHelper instance = DatabaseHelper._privateConstructor();
static Database? _database;
Future<Database> get database async {
if (_database != null) return _database!;
// lazily instantiate the db the first time it is accessed
_database = await _initDatabase();
return _database!;
}
_initDatabase() async {
Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = join(documentsDirectory.path, _databaseName);
return await openDatabase(path,
version: _databaseVersion,
onCreate: _onCreate);
}
Future _onCreate(Database db, int version) async {
await db.execute('''
CREATE TABLE $table (
$columnId INTEGER PRIMARY KEY,
$columnName TEXT NOT NULL,
$address TEXT NOT NULL,
$comments TEXT NOT NULL,
$sigURL TEXT NOT NULL,
$selfpicURL TEXT NOT NULL,
$columnAge INT NOT NULL,
$createdTime TEXT NOT NULL,
)
''');
}
Future<int> insert(Map<String, dynamic> row) async {
Database db = await instance.database;
return await db.insert(table, row);
}
Future<List<Map<String, dynamic>>> queryAllRows() async {
Database db = await instance.database;
return await db.query(table);
}
Future queryRowCount() async {
Database db = await instance.database;
return Sqflite.firstIntValue(await db.rawQuery('SELECT COUNT(*) FROM $table'));
}
Future<int> update(Map<String, dynamic> row) async {
Database db = await instance.database;
int id = row[columnId];
return await db.update(table, row, where: '$columnId = ?', whereArgs: [id]);
}
Future<int> delete(int id) async {
Database db = await instance.database;
return await db.delete(table, where: '$columnId = ?', whereArgs: [id]);
}
}
SQLite SQL 解析器不支持结束括号前的尾随逗号
而不是(在你的声明末尾)
...
$createdTime TEXT NOT NULL,
)
做
...
$createdTime TEXT NOT NULL
)
create table $tableTodo (
$columnId integer primary key autoincrement,
$columnTitle text not null,
$columnDone integer not null)
''');
如果你仔细查看最后一行 $columnDone integer not null 它没有像其他 2 行一样的逗号,那是你的代码中的问题。
编码愉快
SQLite 不支持尾随逗号。尝试删除关于创建 table 的最后一条语句中的逗号。例如
如果你是这样写的;
$createdTime TEXT NOT NULL,
试试看;
$createdTime TEXT NOT NULL