1. 程式人生 > >XamarinSQLite教程在Xamarin.Android專案中使用資料庫

XamarinSQLite教程在Xamarin.Android專案中使用資料庫

XamarinSQLite教程在Xamarin.Android專案中使用資料庫

在Xamarin.Android專案中使用預設資料庫的具體操作步驟如下:

(1)建立一個Xamarin.Android專案,如AndroidSQLiteDemo。

(2)在AndroidSQLiteDemo專案的Resources資料夾下建立一個Raw資料夾。

(3)將上一節中建立的Documents.db資料庫拖動到Raw資料夾中。

(4)開啟MainActivity.cs檔案,將Documents.db資料庫的內容複製到/data/data/[your packageName/files/ MyDocuments.db中,程式碼如下:

using System;

using Android.App;

using Android.Content;

using Android.Runtime;

using Android.Views;

using Android.Widget;

using Android.OS;

using Android.Support.V7.App;

using System.IO;

using System.Text;

namespace AndroidSQLiteDemo

{

    [Activity(Label = "@string/app_name", MainLauncher = true, LaunchMode = Android.Content.PM.LaunchMode.SingleTop, Icon = "@drawable/icon")]

    public class MainActivity : AppCompatActivity

    {

        protected override void OnCreate(Bundle bundle)

        {

            base.OnCreate(bundle);

            SetContentView(Resource.Layout.main);

            var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);

            if (toolbar != null)

            {

                SetSupportActionBar(toolbar);

                SupportActionBar.SetDisplayHomeAsUpEnabled(false);

                SupportActionBar.SetHomeButtonEnabled(false);

            }

            // Get our button from the layout resource,

            // and attach an event to it

            var clickButton = FindViewById<Button>(Resource.Id.my_button);

            clickButton.Click += (sender, args) =>

              {

                  var sqliteFilename = "MyDocuments.db";

                  string documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); // Documents folder

                  var path = Path.Combine(documentsPath, sqliteFilename);

                  Console.WriteLine("資料庫檔案的目錄:{0}",path);

                  if (!File.Exists(path))

                  {

                      var s = Resources.OpenRawResource(Resource.Raw.Documents);

                      //建立寫入列

                      FileStream writeStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);

                      ReadWriteStream(s, writeStream);

                  }

              };

        }

        void ReadWriteStream(Stream readStream, Stream writeStream)

        {

            int Length = 256;

            Byte[] buffer = new Byte[Length];

            int bytesRead = readStream.Read(buffer, 0, Length);

            // 寫入所需位元組

            while (bytesRead > 0)

            {

                writeStream.Write(buffer, 0, bytesRead);

                bytesRead = readStream.Read(buffer, 0, Length);

            }

            readStream.Close();

            writeStream.Close();

        }

    }

}

執行程式後,初始狀態如圖1.31所示。

輕拍HELLO WORLD,CLICK ME!按鈕後,會在輸出視窗輸出以下的內容:

資料庫檔案的目錄:/data/user/0/com.company.AndroidSQLiteDemo/files/MyDocuments.db

此時Documents.db資料庫中的內容就會複製到MyDocuments.db檔案中。