genuinepaster.blogg.se

Create a sqlite database
Create a sqlite database













create a sqlite database

Views > AddProducts.xaml > : using System Please ignore the error of view model, we will add it shortly. Using this form we will add Products to the SQLite database.Įdit its code-behind like below. Now create Views folder (MVVM structure) and create following ContentLayout files: This is our database class and it is also used it ViewModel. Now create a folder named Models (MVVM structure) and create following model class inside of it. But here I’m using its inbuilt functionality. The SQLite.NET provides us with Object Relational Mapping (ORM) that allows us to store and retrieve objects without writing SQL statements.

create a sqlite database

This class will create SQLite connection and the table of our Model class ( Product.cs) which we are using. Public Task deleteProduct(Products product) Public Task saveProduct(Products product)

create a sqlite database

If (!(m => m.MappedType.Name = typeof(Products).Name))Īwait Database.CreateTablesAsync(CreateFlags.None, typeof(Products)).ConfigureAwait(false) InitializeAsync().SafeFireAndForget(false) Static SQLiteAsyncConnection Database => lazyInitializer.Value Return new SQLiteAsyncConnection(Constants.DatabasePath, Constants.Flags) Static readonly Lazy lazyInitializer = new Lazy(() => This class is being called from the main class below, where we are initializing the SQLite connection object. TaskExtensions class will help in creating a database connection in a asynchronous form. Public static async void SafeFireAndForget(this Task task,Īwait task.ConfigureAwait(returnToCallingContext) Ĭatch (Exception ex) when (onException != null) You can change location of the database by editing DatabasePath string as per your requirements. This class contains the name and file path of our SQLite database. Var basePath = Environment.GetFolderPath() Public const SQLite.SQLiteOpenFlags Flags = | Public const string dbName = "Products.db3" Create a folder in your project with name Database and add following classes inside of this folder.















Create a sqlite database