在flutter中将数据保存到sqflite数据库时出错

2lpgd968  于 2023-03-03  发布在  Flutter
关注(0)|答案(1)|浏览(184)

大家好,我正在做一个flutter项目,我试图在一个sqflite数据库中保存数据。因为我是新来的flutter,所以我不明白我做错了什么,或者我犯了什么错误。
我得到的错误是

I/flutter (28200): *** WARNING ***
I/flutter (28200): 
I/flutter (28200): Invalid argument {recipeName: Butter Chicken, recipeCategory: Indian Cuisine} with type _InternalLinkedHashMap<String, dynamic>.
I/flutter (28200): Only num, String and Uint8List are supported. See https://github.com/tekartik/sqflite/blob/master/sqflite/doc/supported_types.md for details
I/flutter (28200): 
I/flutter (28200): This will throw an exception in the future. For now it is displayed once per type.
I/flutter (28200): 
I/flutter (28200):     
E/SQLiteLog(28200): (1) near "101": syntax error in "INSERT INTO recipeTable (101, 102, 103, 104, 105, 106, 107, 109, 110) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"
E/flutter (28200): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: DatabaseException(near "101": syntax error (code 1 SQLITE_ERROR): , while compiling: INSERT INTO recipeTable (101, 102, 103, 104, 105, 106, 107, 109, 110) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)) sql 'INSERT INTO recipeTable (101, 102, 103, 104, 105, 106, 107, 109, 110) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)' args [{recipeName: Butter Chicken, recipeCategory: India..., {recipeName: Tikka, recipeCategory: Indian Cuisine..., {recipeName: Pizza, recipeCategory: Italian Cuisin..., {recipeName: Burger, recipeCategory: Fast Food}, {recipeName: Karai, recipeCategory: Pakistani Cuis..., {recipeName: Handi, recipeCategory: Indian Cuisine..., {recipeName: Soup, recipeCategory: Starter}, {recipeName: Shawarma, recipeCategory: Eastern Cui..., {recipeName: French Fires, recipeCategory: Fast Fo...]
E/flutter (28200): #0      wrapDatabaseException (package:sqflite/src/exception_impl.dart:11:7)
E/flutter (28200): <asynchronous suspension>
E/flutter (28200): #1      SqfliteDatabaseMixin.txnRawInsert.<anonymous closure> (package:sqflite_common/src/database_mixin.dart:548:14)
E/flutter (28200): <asynchronous suspension>
E/flutter (28200): #2      BasicLock.synchronized (package:synchronized/src/basic_lock.dart:33:16)
E/flutter (28200): <asynchronous suspension>
E/flutter (28200): #3      SqfliteDatabaseMixin.txnSynchronized (package:sqflite_common/src/database_mixin.dart:489:14)
E/flutter (28200): <asynchronous suspension>
E/flutter (28200): #4      DatabaseHelper.saveRecipe (package:recipedia/Database/databaseHelper.dart:46:15)
E/flutter (28200): <asynchronous suspension>
E/flutter (28200):

下面是我的代码从firestore数据库获取数据并保存到本地数据库(sqflite)

Future<void> getRecipeData() async {
    recipes = await RecipeModel().getRecipeDataList();
    count = await RecipeModel().getRecipeCount();

    //Converting List into Map
    for (int i=0; i<count;i++) {
      recipeMap[recipes[i].recipeID] = {
        'recipeName': recipes[i].recipeName,
        'recipeCategory': recipes[i].recipeCategory,
      };
    }
    //Storing data into database
    DatabaseHelper().saveRecipe(recipeMap);
  }

这是我的数据库类

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

  factory DatabaseHelper() => _instance;

  final String recipeTable = 'recipeTable';
  final String recipeId = 'recipeId';
  final String recipeName = 'recipeName';
  final String recipeCategory = 'recipe_category';
  final String columnSynced = 'synced';

  static Database? _db;

  Future<Database?> get db async {
    if (_db != null) {
      return _db;
    }
    _db = await initDb();
    return _db;
  }

  DatabaseHelper.internal();

  initDb() async {
    String databasesPath = await getDatabasesPath();
    String path = join(databasesPath, 'recipe.db');

    var db = await openDatabase(path, version: 1, onCreate: _onCreate);
    return db;
  }

  void _onCreate(Database db, int newVersion) async {
    await db.execute(
        'CREATE TABLE $recipeTable($recipeId INTEGER PRIMARY KEY, $recipeName TEXT, $recipeCategory TEXT, $columnSynced INTEGER)');
  }

  Future<int> saveRecipe(Map<String, dynamic> recipe) async {
    var dbClient = await db;
    int res = await dbClient!.insert(recipeTable, recipe);
    return res;
  }
}

请帮助我解决这个问题。所有的努力都将受到赞赏。谢谢

g6ll5ycj

g6ll5ycj1#

您正在尝试使用方法saveRecipeMap数据插入SQLite数据库。
正如Save Hashmap Data Into SQLiteHow to Convert Hashmap to String to Save in SQLite Database post所解释的那样;您需要将recipe Map转换为字符串/ json以存储在SQLite数据库中。
您可以将saveRecipe方法更改为:

Future<int> saveRecipe(Map<String, dynamic> recipe) async {
  var dbClient = await db;
  String recipeJson = jsonEncode(recipe); 
  int res = await dbClient!.insert(recipeTable, {'recipe': recipeJson});
  return res;
}

相关问题