如何在 Flutter 中使用 Sqlite 将布尔值转换为 Int 值?
How to convert Boolean Value to Int value using Sqlite in Flutter?
使用flutter
我有这个模型
class Task {
String color;
String env;
String id;
String name;
String description;
bool isBuyIt;
bool isOnBacklog;
}
我正在使用 SwitchListTile
来更改 isBuyIt
和 isOnBacklog
的布尔值
SwitchListTile(
activeColor: HexColor(task.color),
title: Text("Do you have it?"),
value: task.isBuyIt,
onChanged: (bool value) {
setState(() {
task.isBuyIt = value;
});
},
secondary: IconButton(
icon: Icon(Icons.shopping_cart),
onPressed: null,
color: HexColor(task.color),
),
)
我正在使用 sqflite: ^1.3.0
,如您所知,它没有 support bool value。我是这样制作 table 的:
Future _onCreate(Database db, int version) async {
await db.execute('''
CREATE TABLE $table (
$columnId TEXT PRIMARY KEY,
$columnName TEXT NOT NULL,
$columnColor TEXT NOT NULL,
$columnEnv TEXT NOT NULL,
$columnDescription TEXT NOT NULL,
$columnisBuyIt INTEGER NOT NULL,
$columnisOnBacklog INTEGER NOT NULL
)
''');
}
但我不知道如何将 Boolean
值转换为 Integer
值。我不想将模型字段更改为 Integer
,因为 SwitchListTile
不适用于 INT
值
我想 Check Constraint
可以。
SQLite 没有单独的布尔存储 class。相反,布尔值存储为整数 0 (false) 和 1 (true)。
我知道我知道的三种方法
这是一个例子
如果你想将布尔值转换为整数,你可以说。
int flag = (boolValue)? 1 : 0;
如果你想将它转换回布尔值,你应该说
Boolean flag2 = (flag == 1)? true : false;
换一种方式
使用以下 ALTER 语句 -
ALTER TABLE CREATE_DB_TABLE ADD status boolean NOT NULL default 0;
然后你可以使用 Cursor 获取所需的值,然后转换为实际的布尔值 -
flag=cursor.getString(0).equals("1")
或第三个与上面的相似的是
Boolean flag = (cursor.getInt(cursor.getColumnIndex("flag")) == 1);
从 SQLite 3.23.0 literal true/false 开始被识别并可以使用。
Recognize TRUE and FALSE as constants. (For compatibility, if there exist columns named "true" or "false", then the identifiers refer to the columns rather than Boolean constants.)
CREATE TABLE a (id INT, i BOOLEAN);
INSERT INTO a(id,i) VALUES(10,true);
INSERT INTO a(id,i) VALUES(20, false);
SELECT * FROM a;
使用flutter
我有这个模型
class Task {
String color;
String env;
String id;
String name;
String description;
bool isBuyIt;
bool isOnBacklog;
}
我正在使用 SwitchListTile
来更改 isBuyIt
和 isOnBacklog
SwitchListTile(
activeColor: HexColor(task.color),
title: Text("Do you have it?"),
value: task.isBuyIt,
onChanged: (bool value) {
setState(() {
task.isBuyIt = value;
});
},
secondary: IconButton(
icon: Icon(Icons.shopping_cart),
onPressed: null,
color: HexColor(task.color),
),
)
我正在使用 sqflite: ^1.3.0
,如您所知,它没有 support bool value。我是这样制作 table 的:
Future _onCreate(Database db, int version) async {
await db.execute('''
CREATE TABLE $table (
$columnId TEXT PRIMARY KEY,
$columnName TEXT NOT NULL,
$columnColor TEXT NOT NULL,
$columnEnv TEXT NOT NULL,
$columnDescription TEXT NOT NULL,
$columnisBuyIt INTEGER NOT NULL,
$columnisOnBacklog INTEGER NOT NULL
)
''');
}
但我不知道如何将 Boolean
值转换为 Integer
值。我不想将模型字段更改为 Integer
,因为 SwitchListTile
不适用于 INT
值
我想 Check Constraint
可以。
SQLite 没有单独的布尔存储 class。相反,布尔值存储为整数 0 (false) 和 1 (true)。 我知道我知道的三种方法 这是一个例子
如果你想将布尔值转换为整数,你可以说。
int flag = (boolValue)? 1 : 0;
如果你想将它转换回布尔值,你应该说
Boolean flag2 = (flag == 1)? true : false;
换一种方式 使用以下 ALTER 语句 -
ALTER TABLE CREATE_DB_TABLE ADD status boolean NOT NULL default 0;
然后你可以使用 Cursor 获取所需的值,然后转换为实际的布尔值 -
flag=cursor.getString(0).equals("1")
或第三个与上面的相似的是
Boolean flag = (cursor.getInt(cursor.getColumnIndex("flag")) == 1);
从 SQLite 3.23.0 literal true/false 开始被识别并可以使用。
Recognize TRUE and FALSE as constants. (For compatibility, if there exist columns named "true" or "false", then the identifiers refer to the columns rather than Boolean constants.)
CREATE TABLE a (id INT, i BOOLEAN);
INSERT INTO a(id,i) VALUES(10,true);
INSERT INTO a(id,i) VALUES(20, false);
SELECT * FROM a;