2

我的表格和行如下所示;

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 的 case 并产生一个垃圾值(类似于 -2323423)。我怎样才能得到正确的价值?

4

1 回答 1

5

在 SQLite 中,所有整数值都是 64 位值。使用它很常见,sqlite3_column_int因为大多数应用程序已经知道它们的值确实适合 32 位(或者已经从其他一些做出这种假设的应用程序中复制粘贴了该代码)。

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

于 2013-07-25T14:15:39.317 回答