How to assign a default string value to a Sqlite database column in UWP app?

Abhishek Jakabal 86 Reputation points
2022-05-02T12:52:40.487+00:00

So i've created an UWP app which communicates with a Sqlite database, i've a table which has a column named 'location'. I want to assign a default value to that 'location' column every time a row is created.

The way i've created table columns in the database is:

//travel is a table name. id and location is a column in that table

public class travel
{
public int id { get; set; }
public string location{get; set;}
}

Please answer asap....

Universal Windows Platform (UWP)
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,644 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 26,186 Reputation points
    2022-05-02T14:11:30.56+00:00

    Assing a value to the class property.

    public class travel  
    {  
        public int id { get; set; }  
        public string location{get; set;} = "default value"  
    }  
    

    You can also use a class constructor to set a property value. Please see the C# programming guide for more information.

    Sqllite has a DEFAULT constraint which you can set.

    https://www.sqlite.org/lang_createtable.html

    If you've implemented Entity Framework.

    https://learn.microsoft.com/en-us/ef/core/modeling/generated-properties?tabs=data-annotations


1 additional answer

Sort by: Most helpful
  1. AgaveJoe 26,186 Reputation points
    2022-05-07T13:13:09.673+00:00

    Your code is specifically inserting a null. See the following ON CONFLICT REPLACE tutorial.

    https://database.guide/convert-null-values-to-the-columns-default-value-when-inserting-data-in-sqlite/