【问题标题】:How to get the number of rows in a database table with Flutter SQFlite如何使用 Flutter SQFlite 获取数据库表中的行数
【发布时间】:2019-06-03 12:03:48
【问题描述】:

如何在 Flutter 中获取数据库表的行数。我正在使用 SQFlite 插件。

我假设它类似于 Android,但 Android 有DatabaseUtils.queryNumEntries(db, TABLE_NAME)。 SQFlite 有类似的东西吗?

我正在尽我所知在下面回答这个问题,但如果有更好的答案,我会很高兴。

【问题讨论】:

    标签: dart flutter sqflite


    【解决方案1】:

    我相信在 DatabaseHelper/Service class 内部有一个更好的方法

    class DbService {
      static Database _db; //static = only a single copy of _db is shared among all the instances of this class (Database)
      static int _count;
    
      static int get count => _count;
      ...
    

    这样每次query 被执行时count 更新,这也使得可以使用过滤器更新count 查询,而无需为其编写新的function ?:

      List<Map> result = await db.query('myDatabase');
      _count = result.length;
      return result;
    }
    

    然后简单地使用DbService.count 来获取计数

    【讨论】:

      【解决方案2】:

      不再需要硬函数调用。试试.length 如果您使用的是数据模型,则可以在 MODEL_NAME 处使用其名称。

      Future<int> getCount() async {
      
      Database db = await this.database;
      var result = await db.query(MODEL_NAME.TABLE_NAME);
      int count = result.length;
      return count;
      

      }

      【讨论】:

        【解决方案3】:

        试试这个代码:

        class DatabaseHelper() {
        
          Future<List<Map<String, dynamic>>> getDataInMap() async {
              Database database = await this.database;
              return database.query("ahkam_fiqhia");
          }
        
          Future<List<BooksModel>> getModelsFromMapList() async {
            List<Map<String, dynamic>> mapList = await getDataInMap();
        
            List<DataModel> dataModel = List();
        
            for (int i = 0; i < mapList.length; i++) {
              dataModel(DataModel(mapList[i]));
            }
            print(mapList.length);
            return dataModel;
          }
        
        }
        
        

        在你的视图中添加initState()函数然后调用getModelsFromMapList函数:

        class _MainView extends State<MainView> {
        
          DatabaseHelper dbHelper = databaseHelper();  
        
          @override
          void initState() {
            super.initState();
            databaseHelper.getModelsFromMapList();
          }
        }
        
        

        【讨论】:

          【解决方案4】:

          试试这个功能:

          Future<int> getCount() async {
              //database connection
              Database db = await this.database;
              var x = await db.rawQuery('SELECT COUNT (*) from 
              $Table');
              int count = Sqflite.firstIntValue(x);
              return count;
          }
          

          【讨论】:

            【解决方案5】:

            你也可以使用

            List<Map> list = await db.rawQuery('SELECT * FROM table_name');
            int count = list.length;
            

            【讨论】:

            • 不建议为了检查记录数而加载列表中的所有记录。始终使用count(*) 函数。
            • 你的函数会减慢你的应用程序,因为它会加载所有的记录数据并且它需要很长时间来执行而不是使用计数
            【解决方案6】:

            你可以使用

            int count = Sqflite.firstIntValue(await db.rawQuery('SELECT COUNT(*) FROM table_name'));
            

            其中dbSQFlite Database

            来源:我在heresource code 中找到了这个。

            【讨论】:

              猜你喜欢
              • 2021-10-12
              • 2019-06-03
              • 1970-01-01
              • 1970-01-01
              • 2021-05-04
              • 1970-01-01
              • 1970-01-01
              • 2020-09-06
              • 2019-08-04
              相关资源
              最近更新 更多