我使用此代码从DB获取值,它适用于iPhone 4s和iPhone 5.但是当它在iPhone 5s及以后返回不同的值时。我不知道为什么这样工作。
NSMutableArray *array = [[NSMutableArray alloc] init];
// Setup the database object
sqlite3 *database;
if(!databasePath)
{
[self getDbPath:_dbName];
}
char const *dbPath = [databasePath UTF8String];
// Open the database from the users filessytem
if(sqlite3_open(dbPath, &database) == SQLITE_OK)
{// Setup the SQL Statement and compile it for faster access
//SQLIte Statement
NSString *sqlStatement_userInfo =[[NSString stringWithFormat:@"Select * from "]stringByAppendingString:tablename];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, [sqlStatement_userInfo UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK)
{
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
if((screenWidth==320 && screenHeight==480) || (screenWidth==320 && screenHeight==568))
{
// Init the Data Dictionary
NSMutableDictionary *_dataDictionary=[[NSMutableDictionary alloc] init];
NSString *colId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
// NSLog(@"_userName = %@",_userName);
NSString *authcode = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 5)];
// NSLog(@"_emailID = %@",_emailID);
NSString *title = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
// NSLog(@"_contactNumber = %@",_contactNumber);
NSString *subtitle = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 4)];
NSString *hours = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
NSString *notificationtype = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
[_dataDictionary setObject:[NSString stringWithFormat:@"%@",colId] forKey:@"id"];
[_dataDictionary setObject:[NSString stringWithFormat:@"%@",authcode] forKey:@"authcode"];
[_dataDictionary setObject:[NSString stringWithFormat:@"%@",title] forKey:@"title"];
[_dataDictionary setObject:[NSString stringWithFormat:@"%@",subtitle] forKey:@"subtitle"];
[_dataDictionary setObject:[NSString stringWithFormat:@"%@",hours] forKey:@"hours"];
[_dataDictionary setObject:[NSString stringWithFormat:@"%@",notificationtype] forKey:@"notificationtype"];
[array addObject:_dataDictionary];
}
else
{
// Init the Data Dictionary
NSMutableDictionary *_dataDictionary=[[NSMutableDictionary alloc] init];
NSString *colId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
// NSLog(@"_userName = %@",_userName);
NSString *authcode = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 5)];
// NSLog(@"_emailID = %@",_emailID);
NSString *title = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
// NSLog(@"_contactNumber = %@",_contactNumber);
NSString *subtitle = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 4)];
NSString *hours = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
NSString *notificationtype = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
[_dataDictionary setObject:[NSString stringWithFormat:@"%@",colId] forKey:@"id"];
[_dataDictionary setObject:[NSString stringWithFormat:@"%@",authcode] forKey:@"authcode"];
[_dataDictionary setObject:[NSString stringWithFormat:@"%@",title] forKey:@"title"];
[_dataDictionary setObject:[NSString stringWithFormat:@"%@",subtitle] forKey:@"subtitle"];
[_dataDictionary setObject:[NSString stringWithFormat:@"%@",hours] forKey:@"hours"];
[_dataDictionary setObject:[NSString stringWithFormat:@"%@",notificationtype] forKey:@"notificationtype"];
// [_dataDictionary setObject:[NSString stringWithFormat:@"%@",hour] forKey:@"ZIPCode"];
[array addObject:_dataDictionary];
}
}
}
else
{
NSLog(@"No Data Found");
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
return array;
在上面的查询中,以下代码适用于4s和5,但它在所有其他设备上返回不同的值:
NSString *colId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
// NSLog(@"_userName = %@",_userName);
NSString *authcode = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 5)];
// NSLog(@"_emailID = %@",_emailID);
NSString *title = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
// NSLog(@"_contactNumber = %@",_contactNumber);
NSString *subtitle = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 4)];
NSString *hours = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
NSString *notificationtype = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
例如:
NSString *title = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
这里它将在iPhone 5上返回标题但在iPhone 5s上它返回我的authcode。
答案 0 :(得分:1)
切勿在查询中使用select * from ...
。您不知道哪些列以及它们将以何种顺序返回。
使用明确的字段名称列表编写查询:
select userName, contactNumber, notificationType, ... from ...
这将确保您的索引引用一致,并且您确切知道将获得的内容。