How to debug moor: getting no such table error

Issue

I’m inserting data to my database and got the following error…

flutter: Moor: Sent INSERT INTO add_cash (name, amount, date, frequency, is_deleted) VALUES (?, ?, ?, ?, ?) with args [income, 100000, 1573102800, once, 0]

Does the question marks mean that the data wasn’t inserted into the database

Here is the button that sends data to database

CupertinoButton.filled(
  onPressed: () {
    scakey.currentState.onItemTapped(1);
    createAddCash();
  },
  child: Text('ADD CASH'),
),
void createAddCash() async {
  final database = Provider.of<AppDatabase>(context);
  final task = AddCashData(
      name: oneController.text,
      amount: int.parse(twoController.text),
      date: selectedDate,
      frequency: selectedItem,
      isDeleted: false);
  database.insertAddCash(task);
}

Here is my database

class AddCash extends Table {
  IntColumn get id => integer().autoIncrement()();
  TextColumn get name => text()();
  IntColumn get amount => integer()();
  DateTimeColumn get date => dateTime()();
  TextColumn get frequency => text()();
  BoolColumn get isDeleted => boolean()();
}


@UseMoor(tables: [AddCash])
class AppDatabase extends _$AppDatabase {
  AppDatabase()
      // Specify the location of the database file
      : super(
          (FlutterQueryExecutor.inDatabaseFolder(
            path: 'db.sqlite',
            // Good for debugging - prints SQL in the console
            logStatements: true,
          )),
        );
  @override
  int get schemaVersion => 1;

  Future insertAddCash(AddCashData addCashData) =>
      into(addCash).insert(addCashData);
}

Solution

Does the question marks mean that the data wasn’t inserted into the
database

No, it is not an error message. The question mark indicate the parameter that will be stored to the databse. I would suggest you add try-catch between your code, so you can check what is the error.

  try {
    final task = AddCashData(...);
    database.insertAddCash(task);
   } catch (e) {
    print(e.toStirng()); // this line will be printed if there is any errors 
   }

Answered By – John Joe

Answer Checked By – Mary Flores (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *