4

I`m trying to insert data from a json object ,the following code is a bout the table I used

I defined the database helper class like this:

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!');

and here is the insert function

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


    }

here is the Error

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

thanks for any helps:)

ElhamKeshavarz
  • 411
  • 2
  • 8
  • 19
  • I'm sorry, my answer was incorrect. when are you calling the create table sql code, maybe try deleting the app from your device/emulator (so it would delete the database) and then reinstalling? – Zvi Karp May 09 '19 at 05:21
  • I do this and change the Unique to AUTOINCREMENT but there is still a syntax Error for the following querry ``` await db.execute( 'CREATE TABLE $roomTable($colRoomId INTEGER PRIMARY KEY AUTOINCREMENT, $colRoomTitle TEXT, ' '$colRoomImage TEXT, $colRoomDevNo INTEGER, $colRoomDesc TEXT , $colRoomCode INTEGER )' ); ``` near ".": syntax error – ElhamKeshavarz May 10 '19 at 21:33

6 Answers6

7

1) Firstly check if the same variable text you use in your model, Map is the same name with your column

2) update the Database version number

3) Uninstall and reinstall the App on your phone or emulator.

SilenceCodder
  • 2,874
  • 24
  • 28
3

For me, the error was because of missing comma and space after the previous column. What I had:

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)"
);

What solved for me was:

db.execute(
  "CREATE TABLE $categoryTable("
    "$idColumn INTEGER PRIMARY KEY, "
    "$nameColumn TEXT, "
    "$weightColumn NUMERIC, "
    "$typeColumn NUMERIC, " // <- Fixed the error
    "$professorColumn TEXT, "
    "$colorColumn TEXT)"
);
FMorschel
  • 799
  • 5
  • 18
3

I encountered this problem and i tried whole different sort of things but finally, what worked for me is written below

Step 1 : Change the database version number

Earlier it was like this

 static final dbVersion = 1;

I later changed it to

static final dbVersion = 5;

This is the function where I initialized the database. I was creating a table with four columns viz id, title, description, and 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)');
    });
  }

Step 2: Clear all the app data and then re run the app.

I hope your problem will get solved :)

GAURAV JOSHI
  • 659
  • 8
  • 7
0

the variables I used toMAP method was not as same as the database helper class in dictation

ElhamKeshavarz
  • 411
  • 2
  • 8
  • 19
0

Try to delete the application storage and reinstall it,as maybe you already created the database file on your phone in a wrong or old way or even a missing column. A fresh start might work if you properly used SQFLite.

Shalabyer
  • 555
  • 7
  • 23
0

Check if the DB generated was generated with all fields declared. In negative case, you can just delete the DB and run query to create the tables.

Work here in this way