专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-683-0016      微信咨询  |  联系我们

XamarinSQLite教程在Xamarin.Android项目中使用数据库

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/2/14 0:56:19       共计:3642 浏览

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 文件中。

版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:Xamarin Forms MVVM实现效果说明 | ·下一条:Android开发_如何设置按钮背景透明与半透明_图片背景透明

Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有