Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在WP8 SQLite应用程序中,如何为表中的指定列提供默认值?_C#_Sqlite_Windows Phone 8 - Fatal编程技术网

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

C# 在WP8 SQLite应用程序中,如何为表中的指定列提供默认值?,c#,sqlite,windows-phone-8,C#,Sqlite,Windows Phone 8,我正在开发基于SQLite的Windows phone 8应用程序。我不知道如何为列提供默认值 这是我在app.xaml.cs中的代码 public App() { // Global handler for uncaught exceptions. UnhandledException += Application_UnhandledException; // Standard XAML initialization I

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

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

    }
我的插入数据代码是

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

每当我向表中插入数据时,它必须将状态字段连同firstname和lastname一起插入为true。如何实现它????

这可以通过创建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.
   }

}
希望这有帮助

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

}