gpt4 book ai didi

c# - 从 SQLITE PCL 获取特定月份的计数

转载 作者:行者123 更新时间:2023-12-05 04:52:59 25 4
gpt4 key购买 nike

我正在使用 Xamarin.Forms 和 C#。我正在尝试获取一年中每个月的产品数量。

string[] months = new string[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

var montlyCount = new Dictionary<string, int>();
foreach (var month in months)
{
var count = App.Database.GetCount(month);
montlyCount.Add(month, count);
}

数据库.cs:

public int GetCount(string month)
{
return database.ExecuteScalar<int>("SELECT COUNT(*) FROM Products WHERE MONTH(ProductEntryDate) = ?;", month);
}

但是,SQLite PCL 中没有“MONTH”这样的函数。例如,我怎样才能得到 month = "January"的计数?

最佳答案

正如您已经发现的,SQLite 中没有像 MONTH() 这样的函数。
可用于从格式正确的日期 (YYYY-MM-DD) 中提取月份的函数是 strftime()这将返回一个左边的 0 填充字符串,它是月份的数字:

SELECT COUNT(*) 
FROM Products
WHERE strftime('%m', ProductEntryDate) = ?

因此您必须将字符串作为参数传递给 '01''02'、....、'12' 和不是月份的名称。

或者你可以传递一个整数 1, 2, ..., 12 如果你隐式转换 的返回值通过添加 0 将 strftime() 转换为整数:

WHERE strftime('%m', ProductEntryDate) + 0 = ?

关于c# - 从 SQLITE PCL 获取特定月份的计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66292088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com