0

I am having following issue I have tried to many things but, I can't solve issue can anyone please help me out?? It is saying workType field is there I am not sure why there is an error.

E/SQLiteLog(10808): (1) table income_table has no column named workType E/flutter (10808): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: DatabaseException(table income_table has no column named workTyp e (code 1): , while compiling: INSERT INTO income_table (workType, money) VALUES (?, ?)) sql 'INSERT INTO income_table (workType, money) VALUES (?, ?)' arg s [tih, wj]

Errors:

I/flutter (10808): Save button clicked
E/SQLiteLog(10808): (1) table income_table has no column named workType
E/flutter (10808): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: DatabaseException(table income_table has no column named workTyp
e (code 1): , while compiling: INSERT INTO income_table (workType, money) VALUES (?, ?)) sql 'INSERT INTO income_table (workType, money) VALUES (?, ?)' arg
s [tih, wj]
E/flutter (10808): #0      wrapDatabaseException (package:sqflite/src/exception_impl.dart:11:7)
E/flutter (10808): <asynchronous suspension>
E/flutter (10808): #1      SqfliteDatabaseMixin.txnRawInsert.<anonymous closure> (package:sqflite_common/src/database_mixin.dart:549:14)
E/flutter (10808): <asynchronous suspension>
E/flutter (10808): #2      BasicLock.synchronized (package:synchronized/src/basic_lock.dart:33:16)
E/flutter (10808): <asynchronous suspension>
E/flutter (10808): #3      SqfliteDatabaseMixin.txnSynchronized (package:sqflite_common/src/database_mixin.dart:490:14)
E/flutter (10808): <asynchronous suspension>
E/flutter (10808): #4      _IncomePageState._Save (package:expense/screens/incomepage.dart:133:16)
E/flutter (10808): <asynchronous suspension>
E/flutter (10808):

Database helper:

import 'Income.dart';
import 'package:sqflite/sqflite.dart';
import 'dart:async';
import 'dart:io';
import 'package:path_provider/path_provider.dart';

class DatabaseHelper {
  static final DatabaseHelper databaseHelper = DatabaseHelper._createInstance();
  static Database? _database;

  String incomeTable = 'income_table';
  String colID = 'id';
  String colWorkType = 'workType';
  String colMoney = 'money';

  DatabaseHelper._createInstance();

  Future<Database?> get database async {
    if (_database != null) {
      return _database;
    }
    _database = await initializeDatabase();
    return _database;
  }

  Future<Database> initializeDatabase() async {
    Directory directory = await getApplicationDocumentsDirectory();
    String path = directory.path + 'incomes.db';

    var incomeDatabase = openDatabase(path, version: 1, onCreate: _createDb);
    return incomeDatabase;
  }

  void _createDb(Database db, int newVersion) async {
    db
        .execute(
            'CREATE TABLE $incomeTable($colID INTEGER PRIMARY KEY AUTOINCREMENT,$colWorkType TEXT,$colMoney TEXT)')
        .catchError((error) => print(error.toString()));
  }

  Future<List<Map<String, dynamic>>> getIncomeMapList() async {
    Database? db = await database;
    var result = db!.query(incomeTable, orderBy: '$colMoney ASC');
    return result;
  }

  Future<int> insertIncome(Income income) async {
    Database? db = await database;
    var result = db!.insert(incomeTable, income.toMap());
    return result;
  }

  Future<int> updateIncome(Income income) async {
    Database? db = await database;
    var result = await db!.update(incomeTable, income.toMap(),
        where: '$colID=?', whereArgs: [income.id]);
    return result;
  }

  Future<int> deleteIncome(int? id) async {
    Database? db = await database;
    var result =
        await db!.rawDelete('DELETE FROM $incomeTable where $colID=$id');
    return result;
  }

  Future<int?> getCount() async {
    Database? db = await database;
    List<Map<String, dynamic>> x =
        await db!.rawQuery('SELECT COUNT(*) from $incomeTable');
    int? result = Sqflite.firstIntValue(x);
    return result;
  }

  Future<List<Income>> getIncomeList() async {
    var incomeMapList = await getIncomeMapList();

    int count = incomeMapList.length;

    List<Income> incomeList = <Income>[];
    for (var i = 0; i < count; i++) {
      incomeList.add(Income.fromMapObject(incomeMapList[i]));
    }
    return incomeList;
  }
}
Nihar Shah
  • 37
  • 5
  • You can check the table schema to make sure it meets your exepectations. Check out this link https://stackoverflow.com/questions/4654762/how-can-one-see-the-structure-of-a-table-in-sqlite. not sure it works in flutter tho – Axel Jul 07 '23 at 07:55
  • Did you try to uninstall the application from your device and remove all its data? Try this and build your application again. – Naqeeb Abid Jul 07 '23 at 09:26

0 Answers0