我正在研究基于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;
}
我的人类:
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"});
});
}
}
每当我将数据插入表格时,它必须将状态字段与firstname和lastname一起插入true。怎么实现呢??????
答案 0 :(得分:0)
这可以通过创建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.
}
}
希望这有帮助