如何连接sqlite db flutter

rpppsulh  于 2023-02-05  发布在  Flutter
关注(0)|答案(1)|浏览(178)

我需要修复的问题时,我把所有信息,并单击保存〉〉有什么数据库
0
我需要修复问题时,我把所有信息,并单击保存有什么数据库
错误:
E/扑动(12919):[错误:flutter/运行时/dart_vm_initializer. cc(41)]未处理的异常:DatabaseException(表PERSON没有名为salary的列(代码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?????
z0qdvdin

z0qdvdin1#

您需要将“salary”列添加到SQLite数据库的“PERSON”表中,或者更新Flutter代码以引用不同的列名。
您可以向现有表“PERSON”添加新列“salary”

Future<void> addSalaryColumn() async {
  final db = await database;
  await db.execute(
    "ALTER TABLE PERSON ADD COLUMN salary INTEGER DEFAULT 0"
  );
}

相关问题