Notice
Recent Posts
Recent Comments
Link
250x250
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- Button
- textfield
- activity
- 계측
- 테스트
- LifeCycle
- Navigation
- Flutter
- livedata
- tabbar
- Kotlin
- TEST
- viewmodel
- scroll
- CustomScrollView
- intent
- ScrollView
- DART
- appbar
- Dialog
- Compose
- drift
- 앱
- 앱바
- data
- textview
- Coroutines
- 안드로이드
- binding
- android
Archives
- Today
- Total
Study Record
[Flutter] 데이터베이스 importing (drift) 본문
728x90
✍ 데이터베이스 importing
drift 에서 새로운 데이터 베이스를 만들지 않고 이미 있는 데이터 베이스를 importing 할 수 있다.
첫 번째로, assets 디렉터리에 데이터 베이스 파일을 넣고 pubspec.yaml 에 데이터 베이스 파일을 추가한다.
두 번째로, 데이터 베이스 클래스에 미리 생성된 데이터 베이스가 없으면 assets 에 저장한 데이터 베이스를 저장하는 걸로 importing 을 할 수 있다.
import 'package:drift/drift.dart';
import 'package:flutter/services.dart' show rootBundle;
import 'package:path/path.dart' as p;
LazyDatabase _openConnection() {
return LazyDatabase(() async {
// put the database file, called db.sqlite here, into the documents folder
// for your app.
final dbFolder = await getApplicationDocumentsDirectory();
final file = File(p.join(dbFolder.path, 'app.db'));
if (!await file.exists()) {
// Extract the pre-populated database file from assets
final blob = await rootBundle.load('assets/my_database.db');
final buffer = blob.buffer;
await file.writeAsBytes(buffer.asUint8List(blob.offsetInBytes, blob.lengthInBytes));
}
return NativeDatabase.createInBackground(file);
});
}
728x90
'Flutter > 라이브러리' 카테고리의 다른 글
[Flutter] 간단한 데이터 저장 (flutter_secure_storage) (0) | 2023.04.07 |
---|---|
[Flutter] drift 에서 Future 와 Stream 차이점 (2) | 2023.03.26 |
[Flutter] drift 에서 테이블 데이터 가져오기 (insert, select, delete, update etc) (0) | 2023.03.25 |
[Flutter] 데이터베이스 table 만들기 (drift) (0) | 2023.03.24 |
[Flutter] drift 시작하기 (Sql 기반 데이터베이스와 테이블 만들기) (0) | 2023.03.23 |