'How to correct error DatabaseException (near ")": syntax error

error DatabaseException(near ")": syntax error (code 1 SQLITE_ERROR): , while compiling: CREATE TABLE my_table ( I/flutter (21754): _id INTEGER PRIMARY KEY, I/flutter (21754): name TEXT NOT NULL, I/flutter (21754): address TEXT NOT NULL, I/flutter (21754): comments TEXT NOT NULL, I/flutter (21754): sigURL TEXT NOT NULL, I/flutter (21754): selfpicURL TEXT NOT NULL, I/flutter (21754): I/flutter (21754): hi INT NOT NULL, I/flutter (21754): time TEXT NOT NULL,

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]);
}
}


Solution 1:[1]

create table $tableTodo ( 
  $columnId integer primary key autoincrement, 
  $columnTitle text not null,
  $columnDone integer not null)
''');

if you look carefully in the last line $columnDone integer not null it has no comma like the other 2 top lines, that's the problem in your code.

for more info

Happy coding

Solution 2:[2]

SQLite doesn't support trailing commas. Try removing the comma of your last statement about creating a table. for example

if you are writing like this;

$createdTime TEXT NOT NULL,

try instead;

$createdTime TEXT NOT NULL

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Csala
Solution 2 Affan Minhas