【问题标题】:How SQLite.Net-PCL insert DateTime into SQLiteSQLite.Net-PCL 如何将 DateTime 插入 SQLite
【发布时间】:2016-12-28 19:35:27
【问题描述】:

这个类将被 SQLite.Net-PCL 创建为一个表

 class Purchase
    {
        [SQLite.Net.Attributes.PrimaryKey, SQLite.Net.Attributes.AutoIncrement]
        public int QId { get; set; }
        public DateTime PurchaseDate { get; set; }
        public int Qty { get; set; }
    }

我需要将此日期插入到 SQLite DB 中的这个 tblPurchase 中。

strDate ="2016/08/10"   
strTime ="10:17:26"

string[] strAr_Date = strDate.Split('/');
string strYear = strAr_Date[0].ToString();
string strMth = strAr_Date[1].ToString();
string strDay = strAr_Date[2].ToString();

//- 根据 DateTime.Now 重新创建日期

 string strDateTime = strDay + "/" + strMth + "/" + strYear + " " + strTime;    
  DateTime dt = DateTime.Parse(strDateTime);

这个 dt 将被插入到下面的 SQLite.Net-PCL 中:

 public static void InsertQueueData(string strContentA, string strContentB)
{
 var db = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), DBPath);

var newItem = new Purchase()
            {  
                PurchaseDate = dt,                
                Qty = 20
            };

            db.Insert(newItem);
}

问题:

1) 这个 dt ( PurchaseDate = dt) 在插入时会被 SQlite.Net-PCL 转换为 SQLite 格式 yyyy-mm-dd hh:mm:ss 吗?

2) 可以使用这个:
var db = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), DBPath);

编辑_2:

to use method(1) to insert a DateTime as below:

DateTime dt = DateTime.Parse(strDateTime);


The method must include :
var db = new SQLiteConnection(new SQLitePlatformWinRT(), DBPath,false);

(1)
public static void InsertQueueData(string strContentA, string strContentB)
{

var db = new SQLiteConnection(new SQLitePlatformWinRT(), DBPath,false);

var newItem = new Purchase()
   {  
     PurchaseDate = dt,                
              Qty = 20
    };

  db.Insert(newItem);
}

2) The purchase Class  must declare in this way :

class Purchase
 {
   [SQLite.Net.Attributes.PrimaryKey, SQLite.Net.Attributes.AutoIncrement]
   public int QId { get; set; }

   public string PurchaseDate { get; set; } // Dont use DateTime PurchaseDate

   public int Qty { get; set; }
 }





   Edit_3 <br/>

    public static string DBPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Purchase.sqlite");

    public static string strDbName = "Purchase.sqlite";

private void CreateDBNow()
{
 var DBPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Purchase.sqlite");
using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), DBPath))
  {
     conn.CreateTable<Purchase>();
 }

 }


        Please help. Thanks

InEdit_3

使用: 新 SQLite.Net.SQLiteConnection(新 SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), DBPath))

【问题讨论】:

    标签: sqlite winrt-xaml uwp


    【解决方案1】:

    这个 dt ( PurchaseDate = dt) 在插入时会被 SQlite.Net-PCL 转换为 SQLite 格式 yyyy-mm-dd hh:mm:ss 吗?

    答案是否定的。Datetime 将在 SQLite 中转换为以下格式:

    我正在使用SQLite Toolbox 来检查数据。

    如果要将DateTime 转换为yyyy-mm-dd hh:mm:ss 格式。你需要使用:

    //false stands for 'storeDateTimeAsTicks'
    //And the table need to be recreated if the table aready exists 
    db = new SQLiteConnection(new SQLitePlatformWinRT(), DBPath,false);
    

    那么 SQLite 中的 DateTime 格式会是这样的:

    【讨论】:

    • @Xia:为了避免混淆,我的Edit_2解释正确吗?
    • 是的,但是您需要使用通过new SQLiteConnection(new SQLitePlatformWinRT(), DBPath,false); 获得的新db 重新创建Purchase 表。
    • @Xia: 在 Edit_3 中:是否可以在没有 false 的情况下使用 new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), DBPath)) ?只是为了确保。
    • 不,不行,如果你想让DateTime在SQLite中的格式为yyyy-mm-dd hh:mm:ss。正如我在回答中明确指出的那样,如果您在不使用 false 的情况下使用它,则日期时间在 SQLite 中将是一个大整数,因此在执行 SQL 语句时将不会得到正确的结果。
    • 但是如果你不想DateTime在SQLite中的格式是yyyy-mm-dd hh:mm:ss,并且不想执行依赖于日期时间格式的SQL语句。可以在没有错误的情况下使用它。当您在代码中检索日期时间时,它将转换为 UTC 时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-04
    • 1970-01-01
    • 1970-01-01
    • 2017-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多