XamarinSQLite教程在Xamarin.Android項(xiàng)目中使用數(shù)據(jù)庫

XamarinSQLite教程在Xamarin.Android項(xiàng)目中使用數(shù)據(jù)庫

在Xamarin.Android項(xiàng)目中使用預(yù)設(shè)數(shù)據(jù)庫的具體操作步驟如下:

(1)創(chuàng)建一個(gè)Xamarin.Android項(xiàng)目,如AndroidSQLiteDemo。

(2)在AndroidSQLiteDemo項(xiàng)目的Resources文件夾下創(chuàng)建一個(gè)Raw文件夾。

(3)將上一節(jié)中創(chuàng)建的Documents.db數(shù)據(jù)庫拖動(dòng)到Raw文件夾中。

(4)打開MainActivity.cs文件,將Documents.db數(shù)據(jù)庫的內(nèi)容復(fù)制到/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(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(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("數(shù)據(jù)庫文件的目錄:{0}",path);

?if (!File.Exists(path))

?{

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

?//創(chuàng)建寫入列

?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);

?//?寫入所需字節(jié)

?while (bytesRead > 0)

?{

?writeStream.Write(buffer, 0, bytesRead);

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

?}

?readStream.Close();

?writeStream.Close();

?}

?}

}

運(yùn)行程序后,初始狀態(tài)如圖1.31所示。


輕拍HELLO WORLD,CLICK ME!按鈕后,會(huì)在輸出窗口輸出以下的內(nèi)容:

數(shù)據(jù)庫文件的目錄:/data/user/0/com.company.AndroidSQLiteDemo/files/MyDocuments.db

此時(shí)Documents.db數(shù)據(jù)庫中的內(nèi)容就會(huì)復(fù)制到MyDocuments.db文件中。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom閱讀 3,203評(píng)論 0 3
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,045評(píng)論 25 709
  • afinalAfinal是一個(gè)android的ioc,orm框架 https://github.com/yangf...
    wgl0419閱讀 6,590評(píng)論 1 9
  • 雖然這個(gè)時(shí)候的項(xiàng)目中已經(jīng)使用了很多Material Design效果,但是在主頁面上還是一片空白,這個(gè)時(shí)候就用一些...
    何懼l閱讀 1,542評(píng)論 0 13
  • 本期銀行家:Emma ATM 本期玩家:陳晨(經(jīng)理),二姐(小學(xué)教師),Emma(醫(yī)生),代會(huì)會(huì)(工程師),李平(...
    my茹蕓閱讀 254評(píng)論 0 2

友情鏈接更多精彩內(nèi)容