问题
This class will be created as a table by 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; }
}
I need to insert this date into this tblPurchase in SQLite DB.
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();
//- Recreate Date base on DateTime.Now
string strDateTime = strDay + "/" + strMth + "/" + strYear + " " + strTime;
DateTime dt = DateTime.Parse(strDateTime);
This dt will be inserted with below 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);
}
Questions:
1) Is this dt ( PurchaseDate = dt) will be convert to SQLite Format yyyy-mm-dd hh:mm:ss by SQlite.Net-PCL when insert?
2) Can use this:
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
use : new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), DBPath))
回答1:
Is this dt ( PurchaseDate = dt) will be convert to SQLite Format yyyy-mm-dd hh:mm:ss by SQlite.Net-PCL when insert?
The answer is No. The Datetime
will be convert to following format in SQLite:
I'm using SQLite Toolbox to inspect the data.
If you want to convert the DateTime
in format of yyyy-mm-dd hh:mm:ss
. You need to use:
//false stands for 'storeDateTimeAsTicks'
//And the table need to be recreated if the table aready exists
db = new SQLiteConnection(new SQLitePlatformWinRT(), DBPath,false);
Then the DateTime format in SQLite will be like this:
来源:https://stackoverflow.com/questions/39076337/how-sqlite-net-pcl-insert-datetime-into-sqlite