-->
Before attempting this tutorial, you should have successfully completed the:
- Build your first Xamarin.Forms app quickstart.
- StackLayout tutorial.
- Button tutorial.
- Entry tutorial.
- CollectionView tutorial.
OpenOffice.org is a database management tool that has been designed to function in a way that it replaces the need of Microsoft office for Mac users. Thisfree database software for Macsupports multiple languages and is found to be compatible with most office suites, which makes it possible to alter documents created through Word.
- Available for the Mac, it lets you create and manage a plethora of relational databases from one user interface, including the likes of MySQL, SQLite, Redis, Amazon Redshift and Postgres.
- Another GUI option is DBeaver. DBeaver is a free open source database tool that works with many different database management systems (MySQL, PostgreSQL, MariaDB, SQLite, Oracle, DB2, SQL Server, Sybase, MS Access, Teradata, Firebird, Derby, etc).
- Features not yet available in the new Outlook for Mac. There are several features that are available in the current version of Outlook for Mac for Microsoft 365 that aren't yet available in the new Outlook experience. S/MIME - Now available in Current Channel version 16.45 (21011103.
In this tutorial, you learn how to:
- Use the NuGet Package Manager to add SQLite.NET to a Xamarin.Forms project.
- Create the data access classes.
- Consume the data access classes.
You will use Visual Studio 2019, or Visual Studio for Mac, to create a simple application that demonstrates how to store data in a local SQLite.NET database. The following screenshots show the final application:
Add SQLite.NET
To complete this tutorial you should have Visual Studio 2019 (latest release), with the Mobile development with .NET workload installed. In addition, you will require a paired Mac to build the tutorial application on iOS. For information about installing the Xamarin platform, see Installing Xamarin. For information about connecting Visual Studio 2019 to a Mac build host, see Pair to Mac for Xamarin.iOS development.
Launch Visual Studio, and create a new blank Xamarin.Forms app named LocalDatabaseTutorial.
Important
The C# and XAML snippets in this tutorial requires that the solution is named LocalDatabaseTutorial. Using a different name will result in build errors when you copy code from this tutorial into the solution.
For more information about the .NET Standard library that gets created, see Anatomy of a Xamarin.Forms application in the Xamarin.Forms Quickstart Deep Dive.
In Solution Explorer, select the LocalDatabaseTutorial project, right-click and select Manage NuGet Packages..:
In the NuGet Package Manager, select the Browse tab, search for the sqlite-net-pcl NuGet package, select it, and click the Install button to add it to the project:
Note
There are many NuGet packages with similar names. The correct package has these attributes:
- Authors: SQLite-net
- NuGet link:sqlite-net-pcl
Despite the package name, this NuGet package can be used in .NET Standard projects.
This package will be used to incorporate database operations into the application.
Build the solution to ensure there are no errors.
To complete this tutorial you should have Visual Studio for Mac (latest release), with iOS and Android platform support installed. In addition, you will also require Xcode (latest release). For more information about installing the Xamarin platform, see Installing Xamarin.
Launch Visual Studio for Mac, and create a new blank Xamarin.Forms app named LocalDatabaseTutorial.
Important
The C# and XAML snippets in this tutorial requires that the solution is named LocalDatabaseTutorial. Using a different name will result in build errors when you copy code from this tutorial into the solution.
For more information about the .NET Standard library that gets created, see Anatomy of a Xamarin.Forms application in the Xamarin.Forms Quickstart Deep Dive.
In Solution Pad, select the LocalDatabaseTutorial project, right-click and select Manage NuGet Packages..:
In the Manage NuGet Packages window, search for the sqlite-net-pcl NuGet package, select it, and click the Add Package button to add it to the project:
Note
There are many NuGet packages with similar names. The correct package has these attributes:
- ID: sqlite-net-pcl
- Author: SQLite-net
- NuGet link:sqlite-net-pcl
Despite the package name, this NuGet package can be used in .NET Standard projects. Disk utility icon mac.
This package will be used to incorporate database operations into the application.
Build the solution to ensure there are no errors.
Create data access classes
In this exercise you will add data access classes to the LocalDatabaseTutorial project, which will be used to persist data about people to the database.
In Solution Explorer, in the LocalDatabaseTutorial project, add a new class named
Person
to the project. Then, in Person.cs, remove all of the template code and replace it with the following code:This code defines a
Person
class that will store data about each person in the application. TheID
property is marked withPrimaryKey
andAutoIncrement
attributes to ensure that eachPerson
instance in the database will have a unique id provided by SQLite.NET.In Solution Explorer, in the LocalDatabaseTutorial project, add a new class named
Database
to the project. Then, in Database.cs, remove all of the template code and replace it with the following code:This class contains code to create the database, read data from it, and write data to it. The code uses asynchronous SQLite.NET APIs that move database operations to background threads. In addition, the
Database
constructor takes the path of the database file as an argument. This path will be provided by theApp
class in the next exercise.In Solution Explorer, in the LocalDatabaseTutorial project, expand App.xaml and double-click App.xaml.cs to open it. Then, in App.xaml.cs, remove all of the template code and replace it with the following code:
This code defines a
Database
property that creates a newDatabase
instance as a singleton. A local file path and filename, which represents where to store the database, are passed as the argument to theDatabase
class constructor.Important
The advantage of exposing the database as a singleton is that a single database connection is created that's kept open while the application runs, therefore avoiding the expense of opening and closing the database file each time a database operation is performed.
For more information about the singleton design pattern, see Design Patterns: Singleton.
Build the solution to ensure there are no errors.
Database Options For Mac Computers
Create data access classes
In this exercise you will add data access classes to the LocalDatabaseTutorial project, which will be used to persist data about people to the database.
In Solution Explorer, in the LocalDatabaseTutorial project, add a new class named
Person
to the project. Then, in Person.cs, remove all of the template code and replace it with the following code:This code defines a
Person
class that will store data about each person in the application. TheID
property is marked withPrimaryKey
andAutoIncrement
attributes to ensure that eachPerson
instance in the database will have a unique id provided by SQLite.NET.In Solution Explorer, in the LocalDatabaseTutorial project, add a new class named
Database
to the project. Then, in Database.cs, remove all of the template code and replace it with the following code:This class contains code to create the database, read data from it, and write data to it. The code uses asynchronous SQLite.NET APIs that move database operations to background threads. In addition, the
Database
constructor takes the path of the database file as an argument. This path will be provided by theApp
class in the next exercise.In Solution Explorer, in the LocalDatabaseTutorial project, expand App.xaml and double-click App.xaml.cs to open it. Then, in App.xaml.cs, remove all of the template code and replace it with the following code:
This code defines a
Database
property that creates a newDatabase
instance as a singleton. A local file path and filename, which represents where to store the database, are passed as the argument to theDatabase
class constructor.Important
The advantage of exposing the database as a singleton is that a single database connection is created that's kept open while the application runs, therefore avoiding the expense of opening and closing the database file each time a database operation is performed.
For more information about the singleton design pattern, see Design Patterns: Singleton.
Build the solution to ensure there are no errors.
Database Options For Mac Computers
In Solution Pad, in the LocalDatabaseTutorial project, add a new class named
Person
to the project. Then, in Person.cs, remove all of the template code and replace it with the following code:This code defines a
Person
class that will store data about each person in the application. TheID
property is marked withPrimaryKey
andAutoIncrement
attributes to ensure that eachPerson
instance in the database will have a unique id provided by SQLite.NET.In Solution Pad, in the LocalDatabaseTutorial project, add a new class named
Database
to the project. Then, in Database.cs, remove all of the template code and replace it with the following code:This class contains code to create the database, read data from it, and write data to it. The code uses asynchronous SQLite.NET APIs that move database operations to background threads. In addition, the
Database
constructor takes the path of the database file as an argument. This path will be provided by theApp
class in the next exercise.In Solution Pad, in the LocalDatabaseTutorial project, expand App.xaml and double-click App.xaml.cs to open it. Then, in App.xaml.cs, remove all of the template code and replace it with the following code:
This code defines a
Database
property that creates a newDatabase
instance as a singleton. A local file path and filename, which represents where to store the database, are passed as the argument to theDatabase
class constructor.Important
The advantage of exposing the database as a singleton is that a single database connection is created that's kept open while the application runs, therefore avoiding the expense of opening and closing the database file each time a database operation is performed.
For more information about the singleton design pattern, see Design Patterns: Singleton.
Build the solution to ensure there are no errors.
Consume data access classes
In this exercise you will create a user interface to consume the previously created data access classes.
In Solution Explorer, in the LocalDatabaseTutorial project, double-click MainPage.xaml to open it. Then, in MainPage.xaml, remove all of the template code and replace it with the following code:
This code declaratively defines the user interface for the page, which consists of two
Entry
instances, aButton
, and aCollectionView
in aStackLayout
. EachEntry
has itsPlaceholder
property set, which specifies the placeholder text that's shown prior to user input. TheButton
sets itsClicked
event to an event handler namedOnButtonClicked
that will be created in the next step. TheCollectionView
sets itsItemTemplate
property to aDataTemplate
, which uses aStackLayout
and twoLabel
objects to define the appearance of each row in theCollectionView
. TheLabel
objects bind theirText
properties to theName
andAge
properties of eachPerson
object, respectively.In addition, the
Entry
instances andCollectionView
have names specified with thex:Name
attribute. This enables the code-behind file to access these objects using the assigned names.In Solution Explorer, in the LocalDatabaseTutorial project, expand MainPage.xaml and double-click MainPage.xaml.cs to open it. Then, in MainPage.xaml.cs, add the
OnAppearing
override andOnButtonClicked
event handler to the class:The
OnAppearing
method populates theCollectionView
with any data stored in the database. TheOnButtonClicked
method, which is executed when theButton
is tapped, saves the entered data into the database before clearing bothEntry
instances, and refreshing the data in theCollectionView
.Note Adobe premiere pro update mac.
The
OnAppearing
method override is executed after theContentPage
is laid out, but just before it becomes visible. Therefore, this is a good place to set the content of Xamarin.Forms views.In the Visual Studio toolbar, press the Start button (the triangular button that resembles a Play button) to launch the application inside your chosen remote iOS simulator or Android emulator.
Enter several items of data, tapping the
Button
for each item of data. This will save the data to the database, and repopulate theCollectionView
with all of the database data: How to get sierra on mac.In Visual Studio, stop the application.
For more information local databases in Xamarin.Forms, see Xamarin.Forms Local Databases (guide)
In Solution Pad, in the LocalDatabaseTutorial project, double-click MainPage.xaml to open it. Then, in MainPage.xaml, remove all of the template code and replace it with the following code:
This code declaratively defines the user interface for the page, which consists of two
Entry
instances, aButton
, and aCollectionView
in aStackLayout
. EachEntry
has itsPlaceholder
property set, which specifies the placeholder text that's shown prior to user input. TheButton
sets itsClicked
event to an event handler namedOnButtonClicked
that will be created in the next step. TheCollectionView
sets itsItemTemplate
property to aDataTemplate
, which uses aStackLayout
and twoLabel
objects to define the appearance of each row in theCollectionView
. TheLabel
objects bind theirText
properties to theName
andAge
properties of eachPerson
object, respectively.In addition, the
Entry
instances andListView
have names specified with thex:Name
attribute. This enables the code-behind file to access these objects using the assigned names.In Solution Pad, in the LocalDatabaseTutorial project, expand MainPage.xaml and double-click MainPage.xaml.cs to open it. Then, in MainPage.xaml.cs, add the
OnAppearing
override andOnButtonClicked
event handler to the class:The
OnAppearing
method populates theCollectionView
with any data stored in the database. TheOnButtonClicked
method, which is executed when theButton
is tapped, saves the entered data into the database before clearing bothEntry
instances, and refreshing the data in theCollectionView
.Note
The
OnAppearing
method override is executed after theContentPage
is laid out, but just before it becomes visible. Therefore, this is a good place to set the content of Xamarin.Forms views.In the Visual Studio for Mac toolbar, press the Start button (the triangular button that resembles a Play button) to launch the application inside your chosen iOS simulator or Android emulator.
Enter several items of data, tapping the
Button
for each item of data. This will save the data to the database, and repopulate theCollectionView
with all of the database data:In Visual Studio for Mac, stop the application.
For more information local databases in Xamarin.Forms, see Xamarin.Forms Local Databases (guide)
Congratulations!
Congratulations on completing this tutorial, where you learned how to:
- Use the NuGet Package Manager to add SQLite.NET to a Xamarin.Forms project.
- Create the data access classes.
- Consume the data access classes.
Database For Mac Free
Next steps
To learn more about the basics of creating mobile applications with Xamarin.Forms, continue to the Web Services tutorial.
Related links
Have an issue with this section? If so, please give us some feedback so we can improve this section.