• <tfoot id='VaTHJ'></tfoot>
    • <bdo id='VaTHJ'></bdo><ul id='VaTHJ'></ul>
    <i id='VaTHJ'><tr id='VaTHJ'><dt id='VaTHJ'><q id='VaTHJ'><span id='VaTHJ'><b id='VaTHJ'><form id='VaTHJ'><ins id='VaTHJ'></ins><ul id='VaTHJ'></ul><sub id='VaTHJ'></sub></form><legend id='VaTHJ'></legend><bdo id='VaTHJ'><pre id='VaTHJ'><center id='VaTHJ'></center></pre></bdo></b><th id='VaTHJ'></th></span></q></dt></tr></i><div id='VaTHJ'><tfoot id='VaTHJ'></tfoot><dl id='VaTHJ'><fieldset id='VaTHJ'></fieldset></dl></div>
      <legend id='VaTHJ'><style id='VaTHJ'><dir id='VaTHJ'><q id='VaTHJ'></q></dir></style></legend>

      <small id='VaTHJ'></small><noframes id='VaTHJ'>

      1. Flutter 中的 Sqlite,数据库资产如何工作

        时间:2023-09-18

          <small id='GsjWl'></small><noframes id='GsjWl'>

        • <tfoot id='GsjWl'></tfoot>
            <tbody id='GsjWl'></tbody>
          • <i id='GsjWl'><tr id='GsjWl'><dt id='GsjWl'><q id='GsjWl'><span id='GsjWl'><b id='GsjWl'><form id='GsjWl'><ins id='GsjWl'></ins><ul id='GsjWl'></ul><sub id='GsjWl'></sub></form><legend id='GsjWl'></legend><bdo id='GsjWl'><pre id='GsjWl'><center id='GsjWl'></center></pre></bdo></b><th id='GsjWl'></th></span></q></dt></tr></i><div id='GsjWl'><tfoot id='GsjWl'></tfoot><dl id='GsjWl'><fieldset id='GsjWl'></fieldset></dl></div>
          • <legend id='GsjWl'><style id='GsjWl'><dir id='GsjWl'><q id='GsjWl'></q></dir></style></legend>
              • <bdo id='GsjWl'></bdo><ul id='GsjWl'></ul>

                  本文介绍了Flutter 中的 Sqlite,数据库资产如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在看这个(https://github.com/tekartik/sqflite/blob/master/doc/opening_asset_db.md) 用于填充已格式化且应用需要的数据,仅用于读取功能.

                  I am looking at this (https://github.com/tekartik/sqflite/blob/master/doc/opening_asset_db.md) for populating data that is already formatted and need for the app, for read functionality only.

                  因此,当我们已经拥有外部 CSV 文件中的所有信息时,我对创建 SQLite 数据库的理解是,在我的应用程序的 .dart 文件中创建类模型,例如

                  So my understanding of creating an SQLite database when we already have all the information in an outside CSV file is to, create the class models in a .dart file in my app, such as

                  class User {
                  
                    int id;
                    String _firstName;
                    String _lastName;
                    String _dob;
                  
                    User(this._firstName, this._lastName, this._dob);
                  
                    User.map(dynamic obj) {
                      this._firstName = obj["firstname"];
                      this._lastName = obj["lastname"];
                      this._dob = obj["dob"];
                    }
                  
                    String get firstName => _firstName;
                  
                    String get lastName => _lastName;
                  
                    String get dob => _dob;
                  
                    Map<String, dynamic> toMap() {
                      var map = new Map<String, dynamic>();
                      map["firstname"] = _firstName;
                      map["lastname"] = _lastName;
                      map["dob"] = _dob;
                      return map;
                    }
                    void setUserId(int id) {
                      this.id = id;
                    }
                  }
                  

                  然后,如果我有一个包含所有用户信息的 CSV 文件(具有对应于用户类的值),我是否可以使用数据库资产来填充信息,然后在内部调用它颤振应用程序?我意识到可能有很多方法可以解决这个问题,但是 .db 文件究竟存储了什么,它是如何格式化的?我可以在这个 .db 文件中实现一个 .csv 文件吗?

                  then if I have a CSV file with all the user information inside of it (with values that correspond to the user class), could I be using the database asset to have that fill out the information and then call to it inside of the flutter app? I realize there are probably many ways to go about this, but What exactly is the .db file storing, and how is it formatted? Can i implement a .csv file into this .db file?

                  推荐答案

                  首先,您需要从 csv 构建一个 sqlite 数据库.这可以通过以下方式完成:

                  First off, you will need to construct a sqlite database from your csv. This can be done in the following way:

                  1. 创建必要的表(users.sql)

                  1. Create the necessary table (users.sql)

                  CREATE TABLE users(
                     firstname TEXT NOT NULL,
                     lastname TEXT NOT NULL,
                     dob TEXT NOT NULL
                  );
                  

                1. 创建sqlite数据库

                2. Create the sqlite database

                  sqlite3 database.db < users.sql
                  

                3. 插入 csv 数据

                4. Insert the csv data

                  sqlite3 database.db
                  .mode csv
                  .import data.csv users
                  

                5. 将 database.db 放入您的资产并将其添加到 pubspec.yaml 中.

                6. Put database.db into your assets and add that in pubspec.yaml.

                  flutter:
                    # ...
                    assets:
                      - assets/database.db
                  

                7. 在您的应用中,您必须将资产文件复制到文档"中.这有点复杂.

                8. In your app, you'll have to copy the asset file into "documents". This is slightly complicated.

                  // Construct a file path to copy database to
                  Directory documentsDirectory = await getApplicationDocumentsDirectory();
                  String path = join(documentsDirectory.path, "asset_database.db");
                  
                  // Only copy if the database doesn't exist
                  if (FileSystemEntity.typeSync(path) == FileSystemEntityType.notFound){
                    // Load database from asset and copy
                    ByteData data = await rootBundle.load(join('assets', 'database.db'));
                    List<int> bytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
                  
                    // Save copied asset to documents
                    await new File(path).writeAsBytes(bytes);
                  }
                  

                9. 最后,您可以像这样访问数据库.

                10. Lastly, you can access the database like so.

                  Directory appDocDir = await getApplicationDocumentsDirectory();
                  String databasePath = join(appDocDir.path, 'asset_database.db');
                  this.db = await openDatabase(databasePath);
                  initialized = true;
                  

                11. 示例查询(this._initialize() 是第 6 步)

                12. Example query (this._initialize() is step 6)

                  Future<List<Page>> search(String word, int parentId) async {
                      if (!initialized) await this._initialize();
                      String query = '''
                        SELECT * FROM users
                        LIMIT 25''';
                      return await this.db.rawQuery(query);
                  }
                  

                13. 这篇关于Flutter 中的 Sqlite,数据库资产如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:fetch API 总是返回 {“_U":0,“_V":0,“_W":null,“_X&q 下一篇:仅使用 SQL 将图片插入 SQL Server 2005 图像字段

                  相关文章

                      <bdo id='iMpwL'></bdo><ul id='iMpwL'></ul>
                  1. <i id='iMpwL'><tr id='iMpwL'><dt id='iMpwL'><q id='iMpwL'><span id='iMpwL'><b id='iMpwL'><form id='iMpwL'><ins id='iMpwL'></ins><ul id='iMpwL'></ul><sub id='iMpwL'></sub></form><legend id='iMpwL'></legend><bdo id='iMpwL'><pre id='iMpwL'><center id='iMpwL'></center></pre></bdo></b><th id='iMpwL'></th></span></q></dt></tr></i><div id='iMpwL'><tfoot id='iMpwL'></tfoot><dl id='iMpwL'><fieldset id='iMpwL'></fieldset></dl></div>
                  2. <legend id='iMpwL'><style id='iMpwL'><dir id='iMpwL'><q id='iMpwL'></q></dir></style></legend>
                    1. <tfoot id='iMpwL'></tfoot>

                      <small id='iMpwL'></small><noframes id='iMpwL'>