我如何比较 TIMESTAMP 类型数据库中的 NSDate?

发布于 2024-10-17 09:10:41 字数 1589 浏览 1 评论 0原文

在我的 sqlite 数据库中,我有一个名为 datekey 的 TIMESTAMP 类型的列。

我如何匹配两个时间戳?

我正在使用这个:

NSString *Sql =@"SELECT ZTIMESLOT,ZAGENDA1,ZAGENDA2 FROM tbl_agenda where tbl_agenda.ZDATEKEY=%d";

NSString *sqlFormated = [NSString stringWithFormat:Sql,date];

其中日期是 NSDate 类型。

但正如我在数据库中看到的,2 月 26 日的时间戳值是 1298678400,这是正确的,但是当我尝试与上面的源代码匹配时,日期值变成 78756944,这对于比较来说是完全错误的。

请帮助

我的完整代码

-(NSMutableArray*)getAgendaDetail:(NSDate*)date

{

NSMutableArray *DateArr=[[NSMutableArray alloc]init];
sqlite3 *database;

@try
{
    DBSettings *dbSettings = [[DBSettings alloc]init];
    [dbSettings checkAndCreateDatabase];
    DatabaseName=dbSettings.DBName;
    DatabasePath=dbSettings.DBPath;
    [dbSettings release];

    if(sqlite3_open([DatabasePath UTF8String], &database) == SQLITE_OK) {

        // Setup the SQL Statement and compile it for faster access
        const char *sqlStatement;
        //NSString *Sql = @"SELECT * FROM song_info where id_song=%d" ;
        NSString *Sql =@"SELECT ZTIMESLOT,ZAGENDA1,ZAGENDA2 FROM tbl_agenda where tbl_agenda.ZDATEKEY=%lu";
        NSString *sqlFormated = [NSString stringWithFormat:Sql,date];
        sqlStatement=[sqlFormated UTF8String];
        sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
            while(sqlite3_step(compiledStatement) == SQLITE_ROW) 
            {
    ---------------------------

与数据库值相比,sqlFormatted 的值完全错误

In my sqlite database I have a column named datekey of type TIMESTAMP.

How would i match two timestamps ?

I am using this:

NSString *Sql =@"SELECT ZTIMESLOT,ZAGENDA1,ZAGENDA2 FROM tbl_agenda where tbl_agenda.ZDATEKEY=%d";

NSString *sqlFormated = [NSString stringWithFormat:Sql,date];

where date is of type NSDate.

But as i can see in my database timestamp value for 26 feb is 1298678400 which is correct but when i am trying to match with above source code then date value become 78756944 which is complete wrong for comparison.

Please help

My complete code

-(NSMutableArray*)getAgendaDetail:(NSDate*)date

{

NSMutableArray *DateArr=[[NSMutableArray alloc]init];
sqlite3 *database;

@try
{
    DBSettings *dbSettings = [[DBSettings alloc]init];
    [dbSettings checkAndCreateDatabase];
    DatabaseName=dbSettings.DBName;
    DatabasePath=dbSettings.DBPath;
    [dbSettings release];

    if(sqlite3_open([DatabasePath UTF8String], &database) == SQLITE_OK) {

        // Setup the SQL Statement and compile it for faster access
        const char *sqlStatement;
        //NSString *Sql = @"SELECT * FROM song_info where id_song=%d" ;
        NSString *Sql =@"SELECT ZTIMESLOT,ZAGENDA1,ZAGENDA2 FROM tbl_agenda where tbl_agenda.ZDATEKEY=%lu";
        NSString *sqlFormated = [NSString stringWithFormat:Sql,date];
        sqlStatement=[sqlFormated UTF8String];
        sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
            while(sqlite3_step(compiledStatement) == SQLITE_ROW) 
            {
    ---------------------------

getting complete wrong value for sqlFormatted as compared to database value

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你没皮卡萌 2024-10-24 09:10:41

如果它是无符号整数、日期或时间戳,您应该使用 %lu,如果它是 NSTimeInterval,您应该使用 %f,因为 NSTimeInterval 是双精度型。

If its an unsigned int, the date or the timestamp, you should use %lu, if its an NSTimeInterval you should use %f as an NSTimeInterval is a double.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文