【问题标题】:SQLite not creating DateTime fieldSQLite 没有创建 DateTime 字段
【发布时间】:2016-02-02 20:24:53
【问题描述】:

在我的 C# VS2015 Windows 10 通用应用程序中,我无法在数据库架构中创建 DateTime 字段。它会创建一个 bigint。我有其他表创建了一个没有问题的 DateTime 字段。

这是一个示例:(请参阅 GameDate 字段)

表格 [结果] 领域:17 [季节]:nvarchar(100) [Id]:整数 [主队名称]:nvarchar(100) [OppTeamName]:nvarchar(100) [游戏日期]:日期时间

Foreign Keys: 0
Indexes: 1
    [] PRIMARY
        [Id] AUTOINCREMENT 
Triggers: 0
Unique constraints: 0
Check constraints: 0



    private void AddUserButton_Click(object sender, RoutedEventArgs e)
    {

        User user = new User();
        user.Name = "User1";
        user.GameDate = Convert.ToDateTime("2015-09-02");
        var path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "BaseBallOU.db");
        using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path))
        {
            conn.Insert(user);
        }
    }

    private void CreateTablesButton_Click(object sender, RoutedEventArgs e)
    {
        var path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "BaseBallOU.db");
        using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path))
        {
            conn.CreateTable<User>();
            conn.CreateTable<TeamX>();
        }
    }
}
public class User
{

    [SQLite.AutoIncrement, SQLite.PrimaryKey]
    public int id { get; set; }
    public DateTime GameDate { get; set; }
    public string Name { get; set; }

}

public class TeamX
{
    [SQLite.AutoIncrement, SQLite.PrimaryKey]
    public int id { get; set; }
    public string TeamName { get; set; }
    public DateTime GameDate { get; set; }
}

以下是在表格中创建的内容:

Table [TeamX]
Fields: 3
    [id]: integer
    [TeamName]: varchar
    [GameDate]: bigint
Foreign Keys: 0
Indexes: 0
Triggers: 0
Unique constraints: 0
Check constraints: 0

我正在使用以下参考资料:

sqlite-net-pcl SQLite.Net-PCL 通用应用平台的 SQLite

【问题讨论】:

  • Miiite,我看到 SQLite 没有 DateTime 数据类型的其他引用,但我有其他表,当评估架构时它显示“数据时间”类型。请参阅下面另一个表的架构并查看“GameDate”字段。表[结果]字段:17 [季节]:nvarchar(100)[OverUnderTotal]:双精度[OverUnderAlpha]:nvarchar(100)[GameTotal]:int [Id]: INTEGER [HomeTeamName]: nvarchar(100) [OppTeamName]: nvarchar(100) [GameDate]: datetime

标签: c# sqlite win-universal-app


【解决方案1】:

如果我没记错的话,SQLite 数据库中没有日期时间类型。 保存新对象时将日期转换为字符串,读取对象时将其转换回 DateTime。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-02
  • 2013-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多