question

AbhishekJakabal-9046 avatar image
0 Votes"
AbhishekJakabal-9046 asked AbhishekJakabal-9046 commented

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

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....

windows-uwpdotnet-sqlite
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AgaveJoe avatar image
0 Votes"
AgaveJoe answered AgaveJoe converted comment to answer

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://docs.microsoft.com/en-us/ef/core/modeling/generated-properties?tabs=data-annotations



· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you the first answer is working correctly i.e public string location{get; set;} = "default value"

0 Votes 0 ·

I have a new query regarding the same question, what happens if we don't edit the code like mentioned above and what if we directly edit the structure of the database. If i go to sqlite studio and edit the structure of the database and add a default value for a column as ''default_value". And when i create a new row in UWP app, the column isn't getting updated with the default value, it shows null. Please give a solution for this... I actually don't want to edit the code.

0 Votes 0 ·
AgaveJoe avatar image
0 Votes"
AgaveJoe answered AbhishekJakabal-9046 commented

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/


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for your help.

One thing which i tried was, i changed the structure of the query.

Previously i had written :

 var add1 = conn.Insert(new table1 { col1 = val1, col2 = val2 });

so when i was using this query it was dumping null values.

Then i thought of using another query, which was :

 conn.Execute("INSERT INTO table1(col1 , col2) VALUES(?,?)",val1, val2);

then it wasn't dumping the null values.



0 Votes 0 ·