SQLite.Net-PCL 如何将 DateTime 插入 SQLite [英] How SQLite.Net-PCL insert DateTime into SQLite
问题描述
这个类将被 SQLite.Net-PCL 创建为一个表
类购买{[SQLite.Net.Attributes.PrimaryKey, SQLite.Net.Attributes.AutoIncrement]公共 int QId { 获取;设置;}公共日期时间购买日期{获取;设置;}公共整数数量{得到;设置;}}
我需要将此日期插入到 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(){购买日期 = dt,数量 = 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:
使用方法(1)插入一个DateTime如下:DateTime dt = DateTime.Parse(strDateTime);该方法必须包括: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(){购买日期 = dt,数量 = 20};db.Insert(newItem);}2)采购类必须这样声明:课时购买{[SQLite.Net.Attributes.PrimaryKey, SQLite.Net.Attributes.AutoIncrement]公共 int QId { 获取;设置;}公共字符串 PurchaseDate { 获取;设置;}//不要使用 DateTime PurchaseDate公共整数数量{得到;设置;}}Edit_3 <br/>公共静态字符串 DBPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Purchase.sqlite");public static string strDbName = "Purchase.sqlite";私有无效 CreateDBNow(){var DBPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Purchase.sqlite");使用 (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), DBPath)){conn.CreateTable();}}请帮忙.谢谢
InEdit_3
使用:new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), DBPath))
这个 dt (PurchaseDate = dt) 会在插入时被 SQlite.Net-PCL 转换为 SQLite 格式 yyyy-mm-dd hh:mm:ss 吗?
答案是否定的.Datetime
将在 SQLite 中转换为以下格式:
我正在使用
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))
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:
这篇关于SQLite.Net-PCL 如何将 DateTime 插入 SQLite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!