0

I need the fix the problem when I put all information and click save > > there's nothing on the database


0

I need the fix the problem when I put all information and click save there's nothing on the database

Error:

E/flutter (12919): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: DatabaseException(table PERSON has no column named salary (code 1 SQLITE_ERROR): ,

Error:

E/flutter (12919): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: DatabaseException(table PERSON has no column named salary (code 1 SQLITE_ERROR):

enter image description here

litedb.dart:
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
class LiteDb {

  static Database? _db;

  Future<Database?> get getInstance async {
    if (_db == null) {
      _db = await instance();
      return _db;
    } else {
      return _db;
    }
  }

  instance() async {
    // Get a location using getDatabasesPath
    var databasesPath = await getDatabasesPath();
    String path = join(databasesPath, 'lite_sql.db');

    // open the database
    Database database = await openDatabase(path, version: 2,
        onCreate: (Database db, int version) async {
          // When creating the db, create the table
          await db.execute(
              '''  
                CREATE TABLE PERSON (id INTEGER  PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, age INTEGER);
                CREATE TABLE ACCOUNT (id INTEGER  PRIMARY KEY AUTOINCREMENT, PERSON_ID INTEGER NOT  NULL, ACCOUNT INTEGER NOT NULL, VALUE REAL);
              ''');
          print('Text Database has been created');
        },
        onUpgrade: (Database db, int oldVersion, int newVersion) async {
          if (newVersion >= 2) {
            await db.execute('''
            ALTER TABLE PERSON ADD COLUMN salary REAL NULL
            ''');
          }
        }
    );
    print(' Database connected');
    return database;
  }

  inquiry(String sqlTxt) async {
    Database? db = await getInstance;
    // Get the records
    List<Map> list = await db!.rawQuery(sqlTxt);
    return list;
  }

  insert(String sqlTxt) async {
    Database? db = await getInstance;
    // Insert some record
    int count = await db!.rawInsert(sqlTxt);
    return count;
  }

  update(String sqlTxt) async {
    Database? db = await getInstance;
    // Update some record
    int count = await db!.rawUpdate(sqlTxt);
    return count;
  }

  delete(String sqlTxt) async {
    Database? db = await getInstance;
    // Delete some record
    int count = await db!.rawDelete(sqlTxt);
    return count;
  }
}

I need help please?????
1
  • 1
    as it says your table is missing the salary column. check your table creation code. Commented Feb 1, 2023 at 5:04

1 Answer 1

2

You will need to either add the "salary" column to the "PERSON" table in the SQLite database, or update your Flutter code to reference a different column name.

You can add a new column "salary" to an existing table "PERSON"

Future<void> addSalaryColumn() async {
  final db = await database;
  await db.execute(
    "ALTER TABLE PERSON ADD COLUMN salary INTEGER DEFAULT 0"
  );
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.