退出后重置Flutter sqflite更新数据

时间:2018-11-13 20:49:00

标签: android sqlite flutter

我正在使用本教程来使用sqflite:

https://grokonez.com/flutter/flutter-sqlite-example-crud-sqflite-example

该数据库已预加载到我的应用程序中,但是在运行更新查询并退出该应用程序后,我的数据已重置!

退出应用程序后,如何确保对数据库所做的更改仍然存在。

数据库助手:

class DatabaseHelper {
  static final DatabaseHelper _instance = new DatabaseHelper.internal();

  factory DatabaseHelper() => _instance;

  Database _db;

  DatabaseHelper.internal();

  Future<Database> get db async {
    if (_db != null) {
      print('---------------Open DB Exist-------------');
      return _db;
    }
    print('---------------Create Copy DB-------------');
    _db = await initDb();

    return _db;
  }

  initDb() async {
    String databasesPath = await getDatabasesPath();
    String path = join(databasesPath, 'mezajxdbs.db'); //On Device

    ByteData data =
        await rootBundle.load(join("assets", "dbs.db")); //In Assets folder
    List<int> bytes =
        data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
    await File(path).writeAsBytes(bytes);

    // await deleteDatabase(path); // just for testing

    _db = await openDatabase(path, version: 1);
    return _db;
  }
Future<List> getAllCategories() async {
    var dbClient = await db;
    // var result = await dbClient.query(tabelCategory, columns: [columnId, columnImage, columnName]);
    var result = await dbClient.rawQuery('SELECT * FROM category');
    return result.toList();
  }

  Future<int> getCount() async {
    var dbClient = await db;
    return Sqflite.firstIntValue(
        await dbClient.rawQuery('SELECT COUNT(*) FROM category'));
  }

  Future<Category> getCategory(int id) async {
    var dbClient = await db;
    var result = await dbClient
        .rawQuery('SELECT * FROM category WHERE ID_Category = $id');
    if (result.length == 0) return null;
    return new Category.fromMap(result.first);
  }

Future<int> updateFavorite(Food food) async {
      print('updated fav food');
    var dbClient = await db;
    return await dbClient.rawUpdate(
        'UPDATE food SET Favorite = \'${food.favotite}\' WHERE ID_Food = ${food.id}');
  }  



Future<int> saveFood(Food food) async {
    var dbClient = await db;
   var result = await dbClient.rawInsert(
       'INSERT INTO food (FK_Category, FK_Mezaj, Name, Value, Description, ImageName, Favorite) VALUES (\'${food.cateory}\', \'${food.mezaj}\', \'${food.name}\', \'${food.value}\', \'${food.description}\', \'${food.image}\', \'${food.favotite}\'  )');

    return result;
  }
Future close() async {
    var dbClient = await db;
    return dbClient.close();
  }
}

食物模型:

class Food {
  int _id, _mezaj, _favorite, _category;
  String _name, _value, _description, _image;

  Food(this._category, this._mezaj, this._name, this._image, this._value, this._description, this._favorite);

  Food.map(dynamic obj) {
    // obj[Database Column Name] --> sample obj["ID_Food"] \\
    this._id = obj["ID_Food"];
    this._category = obj["FK_Category"];
    this._mezaj = obj["FK_Mezaj"];
    this._name = obj["Name"];
    this._value = obj["Value"];
    this._description = obj["Description"];
    this._image = obj["ImageName"];
    this._favorite = obj["Favorite"];
  }

  int get id => _id;
  int get cateory => _category;
  int get mezaj => _mezaj;
  String get name => _name;
  String get value => _value;
  String get description => _description;
  String get image => _image;
  int get favotite => _favorite;


  Map<String, dynamic> toMap() {
    var map = new Map<String, dynamic>();
    if (_id != null) {
      map['ID_Food'] = _id;
    }
    map["FK_Category"] = _category;
    map["FK_Mezaj"] = _mezaj;
    map["Name"] = _name;
    map["Value"] = _value;
    map["Description"] = _description;
    map["ImageName"] = _image;
    map["Favorite"] = _favorite;

    return map;
  }

  Food.fromMap(Map<String, dynamic> map){
    this._id = map["ID_Food"];
    this._category = map["FK_Category"];
    this._mezaj = map["FK_Mezaj"];
    this._name = map["Name"];
    this._value = map["Value"];
    this._description = map["Description"];
    this._image = map["ImageName"];
    this._favorite = map["Favorite"];
  }
}

插入代码:

await db.saveFood(new Food(1, 2, 'Test2', 'ahoo', '2-2', "Create SQLite Tutorial", 1));

在停止调试后,再次运行到调试中,所有插入的数据均被重置!

0 个答案:

没有答案
相关问题