2016-08-22 60 views
0

本課程將通過SQLite.Net-PCLSQLite.Net-PCL如何插入日期時間爲SQLite的

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

我需要插入這個日期到這個tblPurchase在SQLite的數據庫中創建一個表。

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格式yyyy-mm-dd hh:mm:ss通過SQlite.Net-PCL插入時? 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

這是dt(PurchaseDate = dt)將被轉換爲SQLite格式yyyy-mm-dd hh:mm:ss通過SQlite.Net-PCL插入時?

答案是否定的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的日期時間格式將是這樣的: enter image description here

+0

@Xia:只是爲了避免混淆,是我Edit_2解釋是否正確? – MilkBottle

+0

是的,但您需要使用通過'new SQLiteConnection(new SQLitePlatformWinRT(),DBPath,false)'''獲得的新'db'重新創建'Purchase'表。 –

+0

@Xia:在Edit_3中:是否可以使用新的SQLite.Net.SQLiteConnection(新的SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(),DBPath))沒有錯誤?只想確認一下。 – MilkBottle