如何在 SQLite flutter 数据库中保存日期?
How to save date in SQLite flutter database?
我正在制作一个应用程序,用户可以在其中填写表格并保存他们进行的每笔交易的财务交易详细信息,为此,我还想添加一个日期,我还想使用日期也是如此。我正在创建我的数据库如下:
await database.execute(
"CREATE TABLE $TABLE_EXPENSES ("
"$COLUMN_ID INTEGER PRIMARY KEY,"
"$COLUMN_NAME TEXT,"
"$COLUMN_AMOUNT INTEGER,"
"$COLUMN_UNNECESSARYEXPENSES INTEGER,"
"$COLUMN_CATEGORY TEXT,"
"$COLUMN_DATETIME TEXT"
")",
);
我映射如下:
DateTime dateTime;
String pickedDate;
expense({this.id, this.name, this.amount, this.isUnnecessaryExpenses, this.category, this.dateTime, this.pickedDate});
Map toMap() {
var map = {
DatabaseProvider.COLUMN_NAME: name,
DatabaseProvider.COLUMN_AMOUNT: amount,
DatabaseProvider.COLUMN_UNNECESSARYEXPENSES: isUnnecessaryExpenses ? 1 : 0,
DatabaseProvider.COLUMN_CATEGORY: category,
DatabaseProvider.COLUMN_DATETIME: pickedDate,
};
if (id != null) {
map[DatabaseProvider.COLUMN_ID] = id;
}
return map;
}
expense.fromMap(Map map) {
id = map[DatabaseProvider.COLUMN_ID];
name = map[DatabaseProvider.COLUMN_NAME];
amount = map[DatabaseProvider.COLUMN_AMOUNT];
isUnnecessaryExpenses = map[DatabaseProvider.COLUMN_UNNECESSARYEXPENSES] == 1;
category= map[DatabaseProvider.COLUMN_CATEGORY];
pickedDate = map[DatabaseProvider.COLUMN_DATETIME];
}
我还创建了用户可以保存数据的表单,日期选择器如下:
DateTime _dateTime;
String pickedDate;
Widget DatePicker() {
showDatePicker(context: context, initialDate: DateTime.now(), firstDate: DateTime.parse("2020-01-01 00:00:01Z"), lastDate: DateTime.now()
).then((value){
if (value == null){
return;
}
_dateTime = value;
pickedDate = DateFormat('yyyy-MM-dd').format(_dateTime);
});
}
现在我想知道在 SQLite flutter 数据库中保存日期的方法是否正确,因为我一直在尝试获取查询 运行 但我无法做到。请按照这个 link 到我的另一个问题
请帮忙。
感谢您的回复。
这是我使用的正确方法,一切都是正确的,事实证明问题出在日期中添加单引号,这就是它不起作用的原因。现在一切正常。
我正在制作一个应用程序,用户可以在其中填写表格并保存他们进行的每笔交易的财务交易详细信息,为此,我还想添加一个日期,我还想使用日期也是如此。我正在创建我的数据库如下:
await database.execute(
"CREATE TABLE $TABLE_EXPENSES ("
"$COLUMN_ID INTEGER PRIMARY KEY,"
"$COLUMN_NAME TEXT,"
"$COLUMN_AMOUNT INTEGER,"
"$COLUMN_UNNECESSARYEXPENSES INTEGER,"
"$COLUMN_CATEGORY TEXT,"
"$COLUMN_DATETIME TEXT"
")",
);
我映射如下:
DateTime dateTime;
String pickedDate;
expense({this.id, this.name, this.amount, this.isUnnecessaryExpenses, this.category, this.dateTime, this.pickedDate});
Map toMap() {
var map = {
DatabaseProvider.COLUMN_NAME: name,
DatabaseProvider.COLUMN_AMOUNT: amount,
DatabaseProvider.COLUMN_UNNECESSARYEXPENSES: isUnnecessaryExpenses ? 1 : 0,
DatabaseProvider.COLUMN_CATEGORY: category,
DatabaseProvider.COLUMN_DATETIME: pickedDate,
};
if (id != null) {
map[DatabaseProvider.COLUMN_ID] = id;
}
return map;
}
expense.fromMap(Map map) {
id = map[DatabaseProvider.COLUMN_ID];
name = map[DatabaseProvider.COLUMN_NAME];
amount = map[DatabaseProvider.COLUMN_AMOUNT];
isUnnecessaryExpenses = map[DatabaseProvider.COLUMN_UNNECESSARYEXPENSES] == 1;
category= map[DatabaseProvider.COLUMN_CATEGORY];
pickedDate = map[DatabaseProvider.COLUMN_DATETIME];
}
我还创建了用户可以保存数据的表单,日期选择器如下:
DateTime _dateTime;
String pickedDate;
Widget DatePicker() {
showDatePicker(context: context, initialDate: DateTime.now(), firstDate: DateTime.parse("2020-01-01 00:00:01Z"), lastDate: DateTime.now()
).then((value){
if (value == null){
return;
}
_dateTime = value;
pickedDate = DateFormat('yyyy-MM-dd').format(_dateTime);
});
}
现在我想知道在 SQLite flutter 数据库中保存日期的方法是否正确,因为我一直在尝试获取查询 运行 但我无法做到。请按照这个 link 到我的另一个问题
请帮忙。 感谢您的回复。
这是我使用的正确方法,一切都是正确的,事实证明问题出在日期中添加单引号,这就是它不起作用的原因。现在一切正常。