数据类型的处理(提取自FMDB)

本文介绍了如何使用Objective-C与SQLite进行数据绑定的方法,详细解释了不同数据类型的处理方式,包括空值、二进制数据、日期、数值类型等。

if ((!obj) || ((NSNull *)obj == [NSNull null])) {

        sqlite3_bind_null(pStmt, idx);

    }

    

    // FIXME - someday check the return codes on these binds.

    else if ([obj isKindOfClass:[NSData class]]) {

        const void *bytes = [obj bytes];

        if (!bytes) {

            // it's an empty NSData object, aka [NSData data].

            // Don't pass a NULL pointer, or sqlite will bind a SQL null instead of a blob.

            bytes = "";

        }

        sqlite3_bind_blob(pStmt, idx, bytes, (int)[obj length], SQLITE_STATIC);

    }

    else if ([obj isKindOfClass:[NSDate class]]) {

        if (self.hasDateFormatter)

            sqlite3_bind_text(pStmt, idx, [[self stringFromDate:obj] UTF8String], -1, SQLITE_STATIC);

        else

            sqlite3_bind_double(pStmt, idx, [obj timeIntervalSince1970]);

    }

    else if ([obj isKindOfClass:[NSNumber class]]) {

        

        if (strcmp([obj objCType], @encode(BOOL)) == 0) {

            sqlite3_bind_int(pStmt, idx, ([obj boolValue] ? 1 : 0));

        }

        else if (strcmp([obj objCType], @encode(int)) == 0) {

            sqlite3_bind_int64(pStmt, idx, [obj longValue]);

        }

        else if (strcmp([obj objCType], @encode(long)) == 0) {

            sqlite3_bind_int64(pStmt, idx, [obj longValue]);

        }

        else if (strcmp([obj objCType], @encode(long long)) == 0) {

            sqlite3_bind_int64(pStmt, idx, [obj longLongValue]);

        }

        else if (strcmp([obj objCType], @encode(unsigned long long)) == 0) {

            sqlite3_bind_int64(pStmt, idx, (longlong)[obj unsignedLongLongValue]);

        }

        else if (strcmp([obj objCType], @encode(float)) == 0) {

            sqlite3_bind_double(pStmt, idx, [obj floatValue]);

        }

        else if (strcmp([obj objCType], @encode(double)) == 0) {

            sqlite3_bind_double(pStmt, idx, [obj doubleValue]);

        }

        else {

            sqlite3_bind_text(pStmt, idx, [[obj description] UTF8String], -1, SQLITE_STATIC);

        }

    }

    else {

        sqlite3_bind_text(pStmt, idx, [[obj description] UTF8String], -1, SQLITE_STATIC);

    }

转载于:https://www.cnblogs.com/liyufeng2013/p/3421618.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值