【问题标题】:how to sort data by current time in sqlite in ios如何在ios的sqlite中按当前时间对数据进行排序
【发布时间】:2014-01-03 12:55:18
【问题描述】:

我正在尝试解决问题 我将数据保存在 sqlite 中。 日期格式为

NSDateFormatter *df= [[NSDateFormatter alloc]init];
df.dateStyle = NSDateFormatterMediumStyle;
df.dateFormate = @"dd-MM-yyyy";
DateTextField.text = [NSString stringWithFormat:@"%@",[df stringFromDate:MyPicker.date]];

Sqlite 表中保存的值。

我在表 currentdate,pastdate,futuredate 中有三个按钮。

当我按下当前日期按钮时,当前日期值显示在表格中,

当我按下过去日期按钮时,过去日期值显示在表格中,

当我按下futuredate按钮时,futuredate值显示在表格中。

在过去的日期是按 sqlite 查询排序数据

NSString *Querysql = [NSString stringWithFormate:@"SELECT *FROM Table_Name where Date > '16-12-2013' order by date DESC"];

如何在当前日期显示?

获取当前日期、sqlite 表中的值的查询是什么?

【问题讨论】:

  • 使用“dd-MM-yyyy”会使您的代码过于复杂,并阻止您使用许多优化,例如INDEXes。使用“yyyy-MM-dd”将日期存储在数据库中,并根据需要在应用程序中显示格式。检查SQLite date/time

标签: ios sql objective-c sqlite


【解决方案1】:

先获取当前日期

NSDate *date = [NSDate date];

// format it
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"dd-MM-yyyy"];

// convert it to a string
NSString *dateString = [dateFormat stringFromDate:date];

然后使用查询

String Querysql = "SELECT *FROM Table_Name where Date = \""+dateString+"\" ";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-20
    • 2011-09-20
    • 1970-01-01
    • 1970-01-01
    • 2021-04-22
    相关资源
    最近更新 更多