在WP8 SQLite应用程序中,我如何为表中的指定列提供默认值

本文关键字:默认值 应用程序 SQLite WP8 | 更新日期: 2023-09-27 18:09:27

我正在基于Windows phone 8应用程序的SQLite工作。我不知道如何为列提供默认值。

这是我在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类:

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;
}

我的插入数据代码是

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"});
            });
        }
    }

每当我插入数据到表中,它必须插入状态字段为真随着姓和名。如何实现??????

在WP8 SQLite应用程序中,我如何为表中的指定列提供默认值

这可以通过创建Person类的默认构造函数来实现

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

希望能有所帮助