溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Flutter怎么使用sqflite處理數(shù)據(jù)表變更

發(fā)布時(shí)間:2023-05-09 15:43:50 來(lái)源:億速云 閱讀:104 作者:iii 欄目:開(kāi)發(fā)技術(shù)

本篇內(nèi)容主要講解“Flutter怎么使用sqflite處理數(shù)據(jù)表變更”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“Flutter怎么使用sqflite處理數(shù)據(jù)表變更”吧!

sqflite 版本管理

我們使用 sqflite 創(chuàng)建數(shù)據(jù)庫(kù)的時(shí)候,有個(gè) version 參數(shù),就是用于定義當(dāng)前的數(shù)據(jù)庫(kù)版本。

Future<Database> _initDB(String filePath) async {
    final dbPath = await getDatabasesPath();
    final path = join(dbPath, filePath);
    return await openDatabase(path, version: 1, onCreate: _createDB);
}

同時(shí) sqflite 提供了一個(gè)方法來(lái)獲取當(dāng)前數(shù)據(jù)庫(kù)的版本。

var version = await db.getVersion();

有了這個(gè),我們就可以判斷當(dāng)前本地的數(shù)據(jù)庫(kù)版本是不是最新版本了。在 sqflite 中,提供了版本升級(jí)的處理方法,當(dāng)檢測(cè)到 version 參數(shù)比當(dāng)前的數(shù)據(jù)庫(kù)版本高時(shí),就可以執(zhí)行 onUpgrade 方法,在這個(gè)方法里可以處理數(shù)據(jù)表升級(jí)操作,比如增加/刪除字段,更改字段默認(rèn)值等。

var db = await openDatabase(path,
        version: latestVersion, onCreate: _createDB, onUpgrade: _updateDB);

這個(gè)時(shí)候我們就需要更改備忘錄數(shù)據(jù)表,增加一個(gè)標(biāo)簽字段。由于 SQLite 無(wú)法存儲(chǔ)數(shù)組,我們可以將多個(gè)標(biāo)簽轉(zhuǎn)換為特殊符號(hào)分隔的字符串,比如這里使用的是豎線“|”。增加數(shù)據(jù)表的字段的數(shù)據(jù)庫(kù)版本升級(jí)代碼如下。

Future<void> _updateDB(Database db, int oldVersion, int newVersion) async {
    try {
      await db.execute('ALTER TABLE memo ADD COLUMN tags TEXT');
    } on DatabaseException catch (e) {
      if (e.isDuplicateColumnError()) {
        // 忽略該異常,該列已經(jīng)存在
      } else {
        rethrow;
      }
    }
    await db.setVersion(newVersion);
  }

這里之所以捕獲異常,是因?yàn)樘砑幼侄尾僮鞑辉试S添加已有的字段,如果發(fā)生了重復(fù)字段異常處理我們忽略這個(gè)異常,已讓程序能夠正常運(yùn)行。 這里需要注意,在創(chuàng)建數(shù)據(jù)表的時(shí)候也需要更改SQL,增加 tags 字段(upgrade方法只對(duì)舊版本的做升級(jí)處理,不會(huì)更改現(xiàn)有版本的數(shù)據(jù)表)。

Future<void> _createDB(Database db, int version) async {
    await db.execute('''
      CREATE TABLE memo (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        title TEXT,
        content TEXT,
        tags TEXT,
        created_time INTEGER,
        modified_time INTEGER
      )
    ''');
  }

接下來(lái)是備忘錄的代碼實(shí)現(xiàn),我們用 InputChip定義了一個(gè)標(biāo)簽組件,以支持能夠通過(guò)刪除圖標(biāo)刪掉標(biāo)簽。

class Tag extends StatelessWidget {
  final String text;
  final VoidCallback onDeleted;

  const Tag({Key? key, required this.text, required this.onDeleted})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return InputChip(
      label: Text(text),
      onDeleted: onDeleted,
    );
  }
}

在添加備忘錄的頁(yè)面中,在表單區(qū)增加了如下內(nèi)容,一個(gè) Wrap 組件來(lái)顯示多個(gè)標(biāo)簽,然后一個(gè)輸入框來(lái)添加標(biāo)簽。

Column(
    crossAxisAlignment: CrossAxisAlignment.start,
    children: [
      Wrap(
        alignment: WrapAlignment.start,
        spacing: 4.0,
        runSpacing: 4.0,
        children: [
          for (final tag in _tags)
            Tag(
              text: tag,
              onDeleted: () => _removeTag(tag),
            )
        ],
      ),
      TextField(
        controller: _tagController,
        decoration: InputDecoration(
          labelText: '添加標(biāo)簽',
          suffixIcon: IconButton(
            icon: const Icon(Icons.add),
            onPressed: _addTag,
          ),
        ),
        onSubmitted: (_) => _addTag(),
      ),
    ],
  ),

當(dāng)添加標(biāo)簽時(shí),就往標(biāo)簽數(shù)組里增加標(biāo)簽字符串元素,刪除標(biāo)簽時(shí)就移除該標(biāo)簽。這里注意,為了避免標(biāo)簽重復(fù),添加標(biāo)簽時(shí)需要檢查當(dāng)前標(biāo)簽是否已經(jīng)存在。

void _addTag() {
  final newTag = _tagController.text.trim();
  if (newTag.isNotEmpty && !_tags.contains(newTag)) {
    setState(() {
      _tags.add(newTag);
      _tagController.clear();
    });
  }
}

之后就是更改我們的保存?zhèn)渫浀姆椒ā?/p>

Future<int> _saveMemo(BuildContext context) async {
  var createdTimestamp = DateTime.now().millisecondsSinceEpoch ~/ 1000;
  var modifiedTimestamp = createdTimestamp;
  var memoMap = {
    'title': _title,
    'content': _content,
    'created_time': createdTimestamp,
    'modified_time': modifiedTimestamp,
    'tags': _tags.isNotEmpty ? _tags.join('|') : '',
  };
  // 保存?zhèn)渫?
  var id = await insertMemo(memoMap);
  return id;
}

到此,相信大家對(duì)“Flutter怎么使用sqflite處理數(shù)據(jù)表變更”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI