如何在flf中的sqflite数据库中进行读写

问题描述 投票:0回答:1

大家好,

我正在使用Flutter开发一个应用程序。为了保存一些本地数据,我使用了SQFlite插件。

我有一个数据库帮助程序类。我正在使用提供程序来调用我的数据库方法。

[当我调用在数据库中写一个帐户的方法时,我收到一个自动递增的ID作为响应,要求插入该帐户。

问题是,当我调用从数据库读取这些帐户的方法时,我什么也没收到。响应Map为null,就像我在数据库中什么都没有。

如果在数据库中插入一个帐户时收到一个ID作为响应,为什么当我尝试读取这些帐户时却什么也没收到。

请帮助我。此代码使我发疯。

class DBProvider {
  static const String TABLE_ACCOUNTS = 'Accounts';
  static const String COLUMN_ID = 'accountID';
  static const String COLUMN_LOCAL_ID = 'id';
  static const String COLUMN_CUSTOMER_PICTURE = 'customerPictureBase64';
  static const String COLUMN_DOC_FRONT = 'docFrontPictureBase64';
  static const String COLUMN_DOC_BACK = 'docBackPictureBase64';
  static const String COLUMN_SIGNATURE = 'signatureBase64';
  static const String COLUMN_GENDER = 'gender';
  static const String COLUMN_FIRST_NAME = 'firstName';
  static const String COLUMN_LAST_NAME = 'lastName';
  static const String COLUMN_DOC_TYPE = 'docType';

  DBProvider._();
  static final DBProvider db = DBProvider._();
  Database _database;

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

  Future<Database> createDatabase() async {
    String dbPath = await getDatabasesPath();
    return await openDatabase(
      join(dbPath, 'paperless.db'),
      version: 1,
      onCreate: (Database database, int version) async {
        await database.execute(
          'CREATE TABLE $TABLE_ACCOUNTS ($COLUMN_LOCAL_ID INTEGER PRIMARY KEY, $COLUMN_ID TEXT, '
          '$COLUMN_CUSTOMER_PICTURE TEXT, $COLUMN_DOC_FRONT TEXT, '
          '$COLUMN_DOC_BACK TEXT, $COLUMN_SIGNATURE TEXT, '
          '$COLUMN_GENDER INTEGER, $COLUMN_FIRST_NAME TEXT, '
          '$COLUMN_LAST_NAME TEXT, $COLUMN_DOC_TYPE TEXT)',
        );
      },
    );
  }

  Future<List<LowKYCAccount>> getLocalLowKYCAccount() async {
    final db = await database;

    var accounts = await db.rawQuery('SELECT * FROM $TABLE_ACCOUNTS');

    var accountsList = List<LowKYCAccount>();

    accounts.forEach((account) {
      LowKYCAccount kycAccount = LowKYCAccount.fromMap(account);
      accountsList.add(kycAccount);
    });
    return accountsList;
  }

  Future<LowKYCAccount> saveAccountLocaly(LowKYCAccount account) async {
    final db = await database;
    account.localId = await db.insert(TABLE_ACCOUNTS, account.toMap());
    return account;
  }
}

我正在从我的UI唱歌提供者处调用此方法

class LowKYCProvider with ChangeNotifier {
  LowKYCAccount _selectedAccount;
  List<LowKYCAccount> _lowKycAccounts = [];
  List<LowKYCAccount> _localLowKycAccounts = [];

  LowKYCAccount get selectedAccount {
    return _selectedAccount;
  }

  List<LowKYCAccount> get lowKycAccounts {
    return [..._lowKycAccounts];
  }

  List<LowKYCAccount> get localLowKycAccounts {
    return [..._localLowKycAccounts];
  }

  Future<void> submitNewAccount(LowKYCAccount account) async {}

  Future<void> fetchLowKycAccounts() async {
    if (_lowKycAccounts.isEmpty) {
      notifyListeners();
    }
  }

  Future<void> fetchLocalLowKycAccounts() async {
    print('vamos fetchar os locais');
    await DBProvider.db.getLocalLowKYCAccount().then((accounts) {
      _localLowKycAccounts = accounts;
      //notifyListeners();
    });
    print('Já terminamos');
  }

  Future<void> saveAccountLocaly(LowKYCAccount account) async {
    await DBProvider.db
        .saveAccountLocaly(account)
        .then((account) => _localLowKycAccounts.add(account));
    notifyListeners();
  }

  Future<void> updateLowKycAccount(LowKYCAccount account) async {}

  Future<void> setSelectedAccount(int index) async {
    _selectedAccount = _lowKycAccounts[index];
    notifyListeners();
  }
}

这是模型类

class LowKYCAccount {
  String id;
  int localId;
  String customerPictureBase64;
  String docFrontPictureBase64;
  String docBackPictureBase64;
  int gender;
  String firstName;
  String lastName;
  String docType;

  File signatureFile;
  String signatureUrl;

  LowKYCAccount({
    this.id,
    this.localId,
    this.customerPictureBase64,
    this.docFrontPictureBase64,
    this.docBackPictureBase64,
    this.gender,
    this.firstName,
    this.lastName,
    this.docType,
    this.signatureUrl,
    this.signatureFile,
  });

  Map<String, dynamic> toMap() {
    var map = <String, dynamic>{
      DBProvider.COLUMN_ID: id,
      DBProvider.COLUMN_CUSTOMER_PICTURE: customerPictureBase64,
      DBProvider.COLUMN_DOC_FRONT: docFrontPictureBase64,
      DBProvider.COLUMN_DOC_BACK: docBackPictureBase64,
      DBProvider.COLUMN_GENDER: gender,
      DBProvider.COLUMN_FIRST_NAME: firstName,
      DBProvider.COLUMN_LAST_NAME: lastName,
      DBProvider.COLUMN_DOC_TYPE: docType,
    };

    if (localId != null) {
      map[DBProvider.COLUMN_LOCAL_ID] = localId;
    }
    return map;
  }

  LowKYCAccount.fromMap(Map<String, dynamic> account) {
    id = account[DBProvider.COLUMN_ID];
    localId = account[DBProvider.COLUMN_LOCAL_ID];
    customerPictureBase64 = account[DBProvider.COLUMN_CUSTOMER_PICTURE];
    docFrontPictureBase64 = account[DBProvider.COLUMN_DOC_FRONT];
    docBackPictureBase64 = account[DBProvider.COLUMN_DOC_BACK];
    gender = account[DBProvider.COLUMN_GENDER];
    firstName = account[DBProvider.COLUMN_FIRST_NAME];
    lastName = account[DBProvider.COLUMN_LAST_NAME];
    docType = account[DBProvider.COLUMN_DOC_TYPE];
  }
}
flutter provider sqflite
1个回答
0
投票

没有使用这种类型的插件或数据库的经验,但是您可能应该使用事务!否则,您的数据可能不会提交:

await database.transaction((txn) async {
  var batch = txn.batch();

  // ... insert your data here!

  // commit but the actual commit will happen when the transaction is committed
  // however the data is available in this transaction
  await batch.commit();

  //  ...
});

和插入示例:

// Insert some records in a transaction
await database.transaction((txn) async {
  int id1 = await txn.rawInsert(
      'INSERT INTO Test(name, value, num) VALUES("some name", 1234, 456.789)');
  print('inserted1: $id1');
  int id2 = await txn.rawInsert(
      'INSERT INTO Test(name, value, num) VALUES(?, ?, ?)',
      ['another name', 12345678, 3.1416]);
  print('inserted2: $id2');
});

有关交易的更多信息,请参见https://pub.dev/packages/sqflite

© www.soinside.com 2019 - 2024. All rights reserved.