SQLite table when working with .Net MAUI

salilsingh-9961 346 Reputation points
2023-12-31T14:56:37.9633333+00:00

Hi Team,

I am working on a .Net MAUI app (in Visual Studio 2022), Android emulator used is Pixel 5 API 33.

I need to perform below using SQLite database, please note currently there is no table created for this in SQLite.

There are 2 date fields on my screen, in corresponding SQLite table, I need to create a field as CreatedDate and when this date falls between other 2 dates, table data regarding CreatedDate should get selected. Please note I need to create a Class (Model) in C# to achieve this. I am not sure If I can select date as column type of a SQLite table.

Please provide a link if possible.

Thanks,

Salil

Azure SQL Database
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,597 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Amira Bedhiafi 26,186 Reputation points
    2023-12-31T22:57:56.8333333+00:00

    You can create a table in SQLite with date fields :

    
       CREATE TABLE ExampleTable (
    
           Id INTEGER PRIMARY KEY AUTOINCREMENT,
    
           CreatedDate DATE,
    
           StartDate DATE,
    
           EndDate DATE
    
       );
    
    

    You will need a C# class that represents this table, which you can use in your .Net MAUI application. Here's an example class:

    
       using System;
    
       public class ExampleModel
    
       {
    
           public int Id { get; set; }
    
           public DateTime CreatedDate { get; set; }
    
           public DateTime StartDate { get; set; }
    
           public DateTime EndDate { get; set; }
    
       }
    
    
    
    

    To select data where CreatedDate falls between StartDate and EndDate, you can write a SQL query like:

    
       SELECT * FROM ExampleTable WHERE CreatedDate BETWEEN StartDate AND EndDate;
    
    

    You can execute this query from your .Net MAUI application using SQLite libraries available for .NET.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.