如何从sqlite数据库中读取Int64值?

时间:2013-07-25 12:55:55

标签: objective-c sqlite

我的表格和行如下所示;

Table : Visit
Column : VisitDate
Value : 634993344000000000 

VisitDate列包含Int64值(例如DateTime.Now.Ticks)

当我从数据库中读取此内容时,SQLite无法检测列是否为Int64值。 我的代码是;

switch (sqlite3_column_type(statement,i))
      {
         case SQLITE_INTEGER:
                intValue  = (int)sqlite3_column_int(statement, i);
                colNameChar = (const char *)sqlite3_column_name(statement, i);
                colName =  [NSString stringWithCString:colNameChar encoding:NSUTF8StringEncoding];
                colValue = [NSNumber numberWithInteger: intValue];
                break;
        case SQLITE_FLOAT:
                dblValue = (double)sqlite3_column_double(statement, i);
                colNameChar = (const char *)sqlite3_column_name(statement, i);
                colName =  [NSString stringWithCString:colNameChar encoding:NSUTF8StringEncoding];
                colValue  = [NSNumber numberWithDouble: dblValue];
                break;
         case SQLITE_TEXT:
                 strValue = (const char *)sqlite3_column_text(statement, i);
                 colNameChar = (const char *)sqlite3_column_name(statement, i);
                 colName =  [NSString stringWithCString:colNameChar encoding:NSUTF8StringEncoding];
                 colValue = [NSString stringWithCString:strValue encoding:NSUTF8StringEncoding];
                        break;
          case SQLITE_BLOB:
                  strValue = (const char *)sqlite3_column_value(statement, i);
                  colNameChar = (const char *)sqlite3_column_name(statement, i);
                  colName =  [NSString stringWithCString:colNameChar encoding:NSUTF8StringEncoding];
                  colValue = [NSString stringWithCString:strValue encoding:NSUTF8StringEncoding];
                  break;
           case SQLITE_NULL:
                  strValue = nil;
                  colNameChar = (const char *)sqlite3_column_name(statement, i);
                  colName =  [NSString stringWithCString:colNameChar encoding:NSUTF8StringEncoding];
                  colValue = [[NSObject alloc] init];
                  break;
            default:
                   strValue = (const char *)sqlite3_column_value(statement, i);
                   colNameChar = (const char *)sqlite3_column_name(statement, i);
                   colName =  [NSString stringWithCString:colNameChar encoding:NSUTF8StringEncoding];
                   colValue = [NSString stringWithCString:strValue encoding:NSUTF8StringEncoding];
                    break;
       }    

调试器输入SQLITE_INTEGER的大小写并产生一个垃圾值(类似于-2323423)。 我怎样才能得到正确的价值?

1 个答案:

答案 0 :(得分:4)

在SQLite中,所有整数值都是64位值。 通常只使用sqlite3_column_int,因为大多数应用程序已经知道它们的值确实适合32位(或者使用来自其他应用程序的代码复制 - 表示这个假设)。

如果要检查SQLITE_INTEGER值是否适合32位或64位,则必须先调用sqlite3_column_int64并检查结果。