当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - SQLite returning wrong integer value

问题描述:

I am getting strange wrong value from an INTEGER column in SQlite3. Here it is

From terminal I executed select satement

sqlite> select userId, email, firstName, lastName, mobilePhone, passWord, signupDate, profilePicture from SCH_USERS where email = '[email protected]';

8|[email protected]|Sujit|Dalai|**5106932562**|yas|2015-02-22 02:05:55 +0000|

The mobilePhone value is 5106932562

Now when I am executing same select statement in IOS I am getting different value

Here it is

NSString * sql = [NSString stringWithFormat:@"select userId, email, firstName, lastName, mobilePhone, passWord, signupDate, profilePicture from SCH_USERS where email = '%@';", email];

NSLog(@"sql: %@", sql);

sqlite3_stmt * selectStatement;

if (sqlite3_prepare_v2(sqlite.database, [sql UTF8String], -1, &selectStatement, NULL) == SQLITE_OK)

{

while (sqlite3_step(selectStatement) == SQLITE_ROW)

{

NSNumber *mobilePhone = [NSNumber numberWithInt:sqlite3_column_int(selectStatement, 4)];

NSLog(@"mobile phone %@", mobilePhone);

}

}

The result in log is 2015-02-21 18:05:55.171 Appointment Scheduler[40331:7035909] phone: 811965266

It's quite strange. Any help?

网友答案:

Not that strange at all in fact … 5,106,932,562 minus 811,965,266 is 4,294,967,296, and that just happens to be 2^32 – so you’re simply experiencing the effects of an integer overflow here.

And that’s were your mistake lies to begin with – storing phone numbers as integers, which they are of course not.

Store phone numbers as what they are – strings.

分享给朋友:
您可能感兴趣的文章:
随机阅读: