flutter 当使用sqflite包时,我应该为定义为“AUTOINCREMENT”或“TIMESTAMP”的列设置什么属性值?

bq9c1y66  于 2023-01-31  发布在  Flutter
关注(0)|答案(1)|浏览(91)

我实现了Db_helper,如下所示,并将columnId和columnCreatedAt的列分别定义为“AUTOINCREMENT”和“TIMESTAMP”。

import 'package:intl/intl.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sqflite/sqflite.dart';

const String columnId = '_id';
const String columnIconPass = 'iconPass';
const String columnStatus = 'status';
const String columnMemo = 'memo';
const String columnCreatedAt = 'createdAt';

const List<String> columns = [
  columnId,
  columnIconPass,
  columnStatus,
  columnMemo,
  columnCreatedAt,
];

class DbHelper {
  DbHelper._createInstance();
  static DbHelper instance =
      DbHelper._createInstance();
  static Database? _database;

  Future<Database> get database async {
    return _database ??= await _initDB();
  }

  Future<Database> _initDB() async {
    final dbDirectory = await getApplicationSupportDirectory();
    final dbFilePath = dbDirectory.path;
    String path = join(await dbFilePath, 'cards.db');
    return await openDatabase(
      path,
      version: 1,
      onCreate: _onCreate,
    );
  }

  void deleteDB() async{
    final dbDirectory = await getApplicationSupportDirectory();
    final dbFilePath = dbDirectory.path;
    String path = join(await dbFilePath, 'cards.db');
    await deleteDatabase(path);
  }

  Future _onCreate(Database database, int version) async {
    await database.execute('''
      CREATE TABLE icons(
        _id INTEGER PRIMARY KEY AUTOINCREMENT,
        iconPass TEXT,
        status INTEGER,
        memo TEXT,
        createdAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
      )
   ''');
  }

  Future<List<IconList>> selectAllIcons() async {
    final db = await instance.database;
    final iconListData = await db.query('icons');

    return iconListData
        .map((json) => IconList.fromJson(json))
        .toList();
  }

    Future insert(IconList iconList) async {
    final db = await database;
    return await db.insert(
      'icons',//tablename
      iconList.toJson()
      );
  }
}

class IconList {
  final int id;
  final String iconPass;
  final int status;
  final String memo;
  DateTime createdAt;

  IconList(
      {
      required this.id,
      required this.iconPass,
      required this.status,
      required this.memo,
      required this.createdAt
      });

  IconList copy({
    int? id,
    String? iconPass,
    int? status,
    String? memo,
    DateTime? createdAt,
  }) =>
      IconList(
        id: id ?? this.id,
        iconPass: iconPass ?? this.iconPass,
        status: status ?? this.status,
        memo: memo ?? this.memo,
        createdAt: createdAt ?? this.createdAt,
      );

  static IconList fromJson(Map<String, Object?> json) => IconList(
        id: json[columnId] as int,
        iconPass: json[columnIconPass] as String,
        status: json[columnStatus] as int,
        memo: json[columnMemo] as String,
        createdAt: DateTime.parse(json[columnCreatedAt] as String),
      );

  Map<String, Object> toJson() => {
        columnId:id,
        columnIconPass: iconPass,
        columnStatus: status,
        columnMemo: memo,
        columnCreatedAt: DateFormat('yyyy-MM-dd HH:mm:ss').format(createdAt),
      };
}

在使用这个帮助器的时候,我想知道如何设置我定义的IconList中的idcreatedAt属性。我甚至在表中定义了那些列,但是那些列应该自动插入,但是我应该在使用DbHelper.instance.insert()方法时放一些值,或者我不应该在帮助器中定义那些列,我也很困惑。
谢谢你的帮助!

onPressed: () async {
                  final iconsData = IconList(
                  //id: ??
                  iconPass: "images/icons/cheer.png", 
                  status: 1, 
                  memo: "nothing", 
                  //createdAt:??
                  );
                  await DbHelper.instance.insert(iconsData); 
                },
gstyhher

gstyhher1#

很抱歉,我应该按如下方式定义helper类:

class IconList {
  final int? id;//corrected
  final String iconPass;
  final int status;
  final String memo;
  DateTime createdAt;

  IconList(
      {
      this.id,//corrected
      required this.iconPass,
      required this.status,
      required this.memo,
      required this.createdAt
      });

  IconList copy({
    int? id,
    String? iconPass,
    int? status,
    String? memo,
    DateTime? createdAt,
  }) =>
      IconList(
        id: id ?? this.id,
        iconPass: iconPass ?? this.iconPass,
        status: status ?? this.status,
        memo: memo ?? this.memo,
        createdAt: createdAt ?? this.createdAt,
      );

  static IconList fromJson(Map<String, Object?> json) => IconList(
        id: json[columnId] as int,
        iconPass: json[columnIconPass] as String,
        status: json[columnStatus] as int,
        memo: json[columnMemo] as String,
        createdAt: DateTime.parse(json[columnCreatedAt] as String),
      );

  Map<String, Object> toJson() => {
        //columnId:id, //corrected
        columnIconPass: iconPass,
        columnStatus: status,
        columnMemo: memo,
        columnCreatedAt: DateFormat('yyyy-MM-dd HH:mm:ss').format(createdAt),
      };
}
onPressed: () async {
                  final iconsData = IconList(
                  //id: this can be null as defined with ?
                  iconPass: "images/icons/cheer.png", 
                  status: 1, 
                  memo: "nothing", 
                  createdAt: DateTime.now(),//I don't know if this way is correct but it works. I believe should use SQL TIMESTAMP function but this depends on DateTime Object in Dart. 
                  );
                  await DbHelper.instance.insert(iconsData); 
                },

相关问题