【问题标题】:How to connect sqlite db flutter如何连接sqlite db flutter
【发布时间】:2023-02-01 13:24:22
【问题描述】:

当我输入所有信息并单击保存时,我需要解决问题 > > 数据库中没有任何内容


0

我需要解决问题,当我输入所有信息并单击保存时,数据库中没有任何内容

错误:

E/flutter (12919): [ERROR:flutter/runtime/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?????

【问题讨论】:

  • 因为它说你的表缺少薪水列。检查您的表创建代码。

标签: flutter


【解决方案1】:

您需要将“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"
  );
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-28
    • 2011-09-23
    • 2019-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多