SQLite.Net-PCL如何将DateTime插入SQLite

时间:2016-08-22 09:50:47

标签: sqlite winrt-xaml uwp

此类将由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);

Edit_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))

1 个答案:

答案 0 :(得分:1)

  

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

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

我正在使用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格式如下: enter image description here