Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Database in Xamarin.Android Project

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use the database in the Xamarin.Android project. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

The specific steps for using a default database in a Xamarin.Android project are as follows:

(1) create a Xamarin.Android project such as AndroidSQLiteDemo.

(2) create a Raw folder under the Resources folder of the AndroidSQLiteDemo project.

(3) drag the Documents.db database created in the previous section to the Raw folder.

(4) Open the MainActivity.cs file and copy the contents of the Documents.db database to / data/data/ [your packageName/files/ MyDocuments.db, as follows:

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 ("directory of database files: {0}", path) If (! File.Exists (path)) {var s = Resources.OpenRawResource (Resource.Raw.Documents); / / create the write column 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) / / write required bytes while (bytesRead > 0) {writeStream.Write (buffer, 0, bytesRead); bytesRead = readStream.Read (buffer, 0, Length);} readStream.Close (); writeStream.Close ();}

After running the program, the initial state is shown in figure 1.31.

Pat Hello WORLD,CLICK ME! Button, the following is output in the output window:

Directory of database files: / data/user/0/com.company.AndroidSQLiteDemo/files/MyDocuments.db

At this point, the contents of the Documents.db database are copied to the MyDocuments.db file.

This is the end of the article on "how to use the database in the Xamarin.Android project". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report