【问题标题】:Iphone: How to read return value of strftime SQLite functionIphone:如何读取 strftime SQLite 函数的返回值
【发布时间】:2011-02-02 01:55:37
【问题描述】:

我需要读取以下查询的列值

SELECT strtime('%Y-%m', created_at) as field FROM table GROUP BY field

列字段的类型是3(我假设是博客),但我需要字符串

我应该怎么做?

更新

const char* sql = "SELECT CAST(strftime('%Y', created_at) as INTEGER) as year FROM table GROUP BY year

if (SQLITE_OK == sqlite3_prepare_v2([AppDelegate db], sql, -1, &queryhandle, NULL)){ while(SQLITE_ROW == sqlite3_step(queryhandle)){

   NSSTring* year  = [NSString stringWithFormat:@"%d",sqlite3_column_int(queryhandle, 0)];

这段代码给了我 3460 作为年份

(已检查强制转换为整数,并强制转换为 varchar 和 sqlite3_column_text 函数)

【问题讨论】:

  • 我已经通过为一年创建单独的字段来解决它,并不优雅,但上面的解决方案在任何变体中都不起作用

标签: iphone sqlite strftime


【解决方案1】:

只要created_at 具有sqlite docs 中指定的时间字符串格式,在特定元组中使用什么存储类型都没有关系(尽管您可以使用typeof 找到)函数。 strftime 函数(您将其别名为 field)的结果是文本或 null。

CREATE TABLE t (x BLOB);
INSERT INTO t VALUES ("2010-03-29\x0"), ("2010-03-28");
SELECT strftime("%Y-%m", x), typeof(strftime("%Y-%m", x)), typeof(x) FROM t;
|null|text
2010-03|text|text

【讨论】:

  • 在 SQLite 管理器中结果相同,但无法取回有效数据
猜你喜欢
  • 1970-01-01
  • 2020-09-14
  • 1970-01-01
  • 2017-01-01
  • 1970-01-01
  • 2021-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多