我如何比较 TIMESTAMP 类型数据库中的 NSDate?
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果它是无符号整数、日期或时间戳,您应该使用 %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.