0
votes

I am working on SQLite based Windows phone 8 app. I am not getting how to provide default value for a column.

This is my code in app.xaml.cs

public App()
    {
        // Global handler for uncaught exceptions.
        UnhandledException += Application_UnhandledException;

        // Standard XAML initialization
        InitializeComponent();

        // Phone-specific initialization
        InitializePhoneApplication();

        // Language display initialization
        InitializeLanguage();

        // Show graphics profiling information while debugging.
        if (Debugger.IsAttached)
        {

            Application.Current.Host.Settings.EnableFrameRateCounter = true;

            PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;
        }
        string dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db1.sqlite");
        if (!FileExists("db1.sqlite").Result)
        {
            using (var db = new SQLiteConnection(dbPath))
            {
                db.CreateTable<Person>();
            }
        }
    }
    private async Task<bool> FileExists(string fileName)
    {
        var result = false;
        try
        {
            var store = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
            result = true;
        }
        catch
        {
        }

        return result;

    }

My Person class:

public class Person
{
    [SQLite.PrimaryKey, SQLite.AutoIncrement]
    public int Id { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string status;

}

My Insertion data code is

string dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db1.sqlite");
    private void BtnInsert_OnClick(object sender, RoutedEventArgs e)
    {
        using (var db = new SQLiteConnection(dbPath))
        {

            db.RunInTransaction(() =>
            {
                db.Insert(new Person() { FirstName = "Ken", LastName="Thompson"});
            });
        }
    }

Whenever i'm inserting data into table it has to insert status field as true along with firstname and lastname. How to achieve it??????

1

1 Answers

0
votes

This can be done by creating default constructor of the Person class

public class Person
{
   [SQLite.PrimaryKey, SQLite.AutoIncrement]
   public int Id { get; set; }

   public string FirstName { get; set; }

   public string LastName { get; set; }

   public string Status {get;set;}

   public Person()
   {
      this.Status = true;  //this will set it to true whenever Person class is initialized and will insert true always.
   }

}

Hope this helps