我正在创建一个应用程序并需要一个数据库。该数据库包含位置表和兴趣点表。
这是一对多的关系。
一个位置有多个兴趣点。
现在我尝试用 sqflite 在 flutter 中建立这种关系,但失败了。
我已经尝试添加外键,但没有成功。
这只是代码中最重要的部分。
static final String locationTable = 'location';
static final String columnLocationId = 'id';
static final String columnLocationTitle = 'title';
static final String pointOfInterestTable = 'point_of_interest';
static final String columnPointOfInterestId = 'id';
static final String columnPointOfInterestTitle = 'title';
static final String createTableLocation = 'CREATE TABLE $locationTable('
'$columnLocationId INTEGER PRIMARY KEY AUTOINCREMENT,'
'$columnLocationTitle TEXT NOT NULL)';
static final String createTableLocation = 'CREATE TABLE $pointOfInterestTable('
'$columnPointOfInterestId INTEGER PRIMARY KEY AUTOINCREMENT,'
'$columnPointOfInterestTitle TEXT NOT NULL)';
Future<List> getPointOfInterestsOfLocations(int id) async {
final db = await this.db;
final maps = await db.query(
locationTable,
where: '$columnLocationId = ?',
whereArgs: [id],
);
return maps.toList();
}
这是将其加载到列表中的代码行:
List pointOfViews = await _pointOfViewDb.getPointOfInterestsOfLocations(id: location.id);
看来你需要自己在 sqlite 中启用外键支持。
在 sqflite 中,您可以像这样定义一个函数:
static Future _onConfigure(Database db) async {
await db.execute('PRAGMA foreign_keys = ON');
}
并将其添加到您的 openDatabase 函数中,如下所示:
openDatabase(version: _databaseVersion, onCreate: _onCreate,
onUpdate: _onUpdate, onDelete: _onDelete,
onConfigure: _onConfigure);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)