[Xamarin] Import DB File
폰에서 로컬 DB 파일을 가지고 처리를 할때
미리 데이터를 담아놓고 처리를 할 필요가 생기는데요
이때 미리 데이터를 담아둔 DB(SQLite) 파일을
앱 설치시 폰에 위치시키는 방법을 소개합니다.
먼저 TEXT.db SQLite db 파일을 준비합니다.
준비한 DB 파일을 각 기기별로 아래와 같이 파일을 갖다 놓습니다.
Android 는 Asset 폴더에 위치시킵니다.
iOS 는 Resources 폴더에 위치시킵니다.
Android 의 MainActivity.cs 에 아래 처럼 코딩합니다.
| 
 using System.IO; 
 using Android.App; using Android.Content.PM; using Android.OS; using Android.Runtime; 
 namespace ImportDbFile.Droid { [Activity(Label = "ImportDbFile", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity { protected override void OnCreate(Bundle savedInstanceState) { TabLayoutResource = Resource.Layout.Tabbar; ToolbarResource = Resource.Layout.Toolbar; 
 base.OnCreate(savedInstanceState); 
 Xamarin.Essentials.Platform.Init(this, savedInstanceState); 
 string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); string dbPath = Path.Combine(path, "TEST.db"); 
 CopyDatabaseIfNotExists(dbPath); 
 global::Xamarin.Forms.Forms.Init(this, savedInstanceState); LoadApplication(new App()); } 
 public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults) { Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults); 
 base.OnRequestPermissionsResult(requestCode, permissions, grantResults); } 
 /// <summary> /// DB 파일을 복사합니다. /// </summary> /// <param name="dbPath"></param> private static void CopyDatabaseIfNotExists(string dbPath) { if (!File.Exists(dbPath)) { using (var br = new BinaryReader(Application.Context.Assets.Open("TEST.db"))) { using (var bw = new BinaryWriter(new FileStream(dbPath, FileMode.Create))) { byte[] buffer = new byte[2048]; int length = 0; while ((length = br.Read(buffer, 0, buffer.Length)) > 0) { bw.Write(buffer, 0, length); } } } } } } } | 
iOS 의 AppDelegate.cs 에 아래 처럼 코딩합니다.
| using System; using System.IO; 
 using Foundation; 
 using UIKit; 
 namespace ImportDbFile.iOS { // The UIApplicationDelegate for the application. This class is responsible for launching the // User Interface of the application, as well as listening (and optionally responding) to // application events from iOS. [Register("AppDelegate")] public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate { // // This method is invoked when the application has loaded and is ready to run. In this // method you should instantiate the window, load the UI into it and then make the window // visible. // // You have 17 seconds to return from this method, or iOS will terminate your application. // public override bool FinishedLaunching(UIApplication app, NSDictionary options) { global::Xamarin.Forms.Forms.Init(); 
 string docFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal); 
 if (!Directory.Exists(docFolder)) { Directory.CreateDirectory(docFolder); } 
 string dbPath = Path.Combine(docFolder, "TEST.db"); 
 CopyDatabaseIfNotExists(dbPath); 
 LoadApplication(new App()); 
 return base.FinishedLaunching(app, options); } 
 /// <summary> /// 해당 경로에 DB 파일을 복사합니다. /// </summary> /// <param name="dbPath"></param> private void CopyDatabaseIfNotExists(string dbPath) { if (!File.Exists(dbPath)) { var existingDb = NSBundle.MainBundle.PathForResource("TEST", "db"); File.Copy(existingDb, dbPath); } } } } 
 | 
DB 파일 복사하기 전
iOS / Android
  
DB 파일 복사 후
iOS / Android
    
소스
https://github.com/kei-soft/ImportDbFile