網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
偏好存儲(chǔ)
shared_preferences
類比iOS中的UserDefaults
,使用方法比較簡(jiǎn)單。 地址戳這里 pub get
之后會(huì)自動(dòng)出現(xiàn)一個(gè)這樣的文件generated_plugin_registrant.dart
數(shù)據(jù)存儲(chǔ):
void _incrementCounter() { //創(chuàng)建對(duì)象,用于操作存儲(chǔ)和讀取。 SharedPreferences.getInstance().then((SharedPreferences prefs) { setState(() { _counter++; }); prefs.setInt('counter', _counter); }); }
數(shù)據(jù)讀取:
SharedPreferences.getInstance().then((SharedPreferences prefs) { setState(() { _counter = prefs.getInt('counter') ?? 0; }); });
sqlite
使用sqlite需要搭配著path一起使用,在使用的過(guò)程中踩了一個(gè)坑, 明明我安裝了CocoaPods
卻一直提示我CocoaPods not installed
Warning: CocoaPods not installed. Skipping pod install.?
CocoaPods is used to retrieve the iOS and macOS platform side's plugin code?
that responds to your plugin usage on the Dart side.?
Without CocoaPods, plugins will not work on iOS or macOS.?
For more info, see https://flutter.dev/platform-plugins To install?
see https://guides.cocoapods.org/using/getting-started.html#installation for instructions.
最后解決辦法 1;打開(kāi)終端 2; 輸入open /Applications/Android\ Studio.app
即可。感覺(jué)挺奇怪的一個(gè)錯(cuò)誤 感謝大佬,問(wèn)題解決鏈接
創(chuàng)建表
1.getDatabasesPath
來(lái)到了Documents
下的目錄 2.join(value, 'test_db.db')
使用的是一個(gè)path
的pub庫(kù)配合使用 3.openDatabase
打開(kāi)數(shù)據(jù)庫(kù),onCreate
建表 // 建表 CREATE TABLE 表名(,,)
late Database _db; @override void initState() { super.initState(); _initDatabase().then((value) => _db = value); } Future<Database> _initDatabase() async { Database db = await getDatabasesPath() .then((value) => join(value, 'test_db.db')) .then((value) => openDatabase(value, version: 1, onCreate: (Database db, int version) async { // 建表 await db.execute( 'CREATE TABLE LK_Text(id INTEGER PRIMARY KEY,name TEXT, age INT)'); })); return db; }
Future<String> getDatabasesPath() => databaseFactory.getDatabasesPath();
是一個(gè)Future
所以需要async
配合著await
來(lái)使用。 執(zhí)行之后發(fā)現(xiàn)已經(jīng)創(chuàng)建成功了,大小8kb, 是一個(gè)空表。
數(shù)據(jù)插入
_db插入數(shù)據(jù)可以使用事務(wù)處理
// 添加數(shù)據(jù) INSERT INTO 表名 VALUES (值1,值2,...)
_db.transaction((txn) async { txn .rawInsert('INSERT INTO LK_Text(name,age) VALUES("zhangsan",16)') .then((value) => print(value)); txn .rawInsert('INSERT INTO LK_Text(name,age) VALUES("lisi",17)') .then((value) => print(value)); });
數(shù)據(jù)查詢
// 數(shù)據(jù)查詢 SELECT 列名稱 FROM 表名稱 *通配符
_db.rawQuery('SELECT * FROM LK_Text').then((value) => print(value));
數(shù)據(jù)修改
// 修改數(shù)據(jù) UPDATE 表名稱 SET 列名稱 = 新值 WHERE 列名稱 = 某值
_db.rawUpdate('UPDATE LK_TEXT SET age = 18 WHERE age = 16');
刪除表
1._db.delete
刪除表 2._db.close()
關(guān)閉數(shù)據(jù)庫(kù)
_db .rawQuery('SELECT * FROM LK_Text') .then((value) => print(value)) .then((value) { // 刪除表 _db.delete('LK_Text').then((value) => _db.close()); });
切記:由于這里是異步的操作,注意執(zhí)行的順序!! 校驗(yàn)的話還是很簡(jiǎn)單,再次寫入數(shù)據(jù)的時(shí)候會(huì)報(bào)錯(cuò)。
刪除數(shù)據(jù)庫(kù)
// 刪除數(shù)據(jù)庫(kù) getDatabasesPath() .then((value) => join(value, 'test_db.db')) .then((value) => deleteDatabase(value));
整體來(lái)說(shuō)還是比較簡(jiǎn)單的,主要是把sqlite
語(yǔ)句寫正確。
原文鏈接:https://juejin.cn/post/7096383768096669710
相關(guān)推薦
- 2022-12-13 OpenCV視頻流C++多線程處理方法詳細(xì)分析_C 語(yǔ)言
- 2022-10-12 Docker部署搭建WebDav服務(wù)的詳細(xì)過(guò)程_docker
- 2022-06-15 C#實(shí)現(xiàn)歸并排序_C#教程
- 2022-09-14 python單鏈路性能測(cè)試實(shí)踐_python
- 2023-03-16 Python庫(kù)functools示例詳解_python
- 2022-06-08 換掉你的@RefreshScope吧
- 2023-03-20 pip安裝python庫(kù)時(shí)報(bào)Failed?building?wheel?for?xxx錯(cuò)誤的解決方法
- 2023-04-06 C語(yǔ)言中單鏈表的基本操作(創(chuàng)建、銷毀、增刪查改等)_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支