我正在尝试从数据库中获取值但是它没有得到 我正在尝试这个代码,但没有得到值,当我在程序上使用断点 我有经度,纬度他的数据类型是数据库的两倍
if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
NSString *sqlStr = [NSString stringWithFormat:@"select Longitude,Latitude from Location"];
const char *sqlStatement = [sqlStr UTF8String];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
locations = [NSMutableArray array];
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
int i = sqlite3_step(compiledStatement);
NSLog(@"%i",i); **//over here i am getting the 101 value in console** And my pointer getting out from here
NSString *dLongitude = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
NSString *dLatitude = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 4)];
[locations addObject:[NSString stringWithFormat:@"%@ ,%@",dLongitude,dLatitude]];
NSLog(@"%@",locations);
}
result = [locations componentsJoinedByString:@" "]; // same as `fake_location`
// Get file path here
NSError *error;
if ( [result writeToFile:dbPath atomically:YES encoding:NSUTF8StringEncoding error:&error] ) {
NSLog(@"%@", [error localizedDescription]);
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
答案 0 :(得分:0)
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
int i = sqlite3_step(compiledStatement);
NSLog(@"%i",i); **//over here i am getting the 101 value in console** And my pointer getting out from here
double longitude = sqlite3_column_double(compiledStatement, 3);
double latitude = sqlite3_column_double(compiledStatement, 4);
NSString *coords = [[[NSString alloc] initWithFormat:@"%f,%f",longitude,latitude] autorelease];
[locations addObject:coords];
NSLog(@"%@",locations);
}
答案 1 :(得分:0)
将代码更改为以下内容:
if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
NSString *sqlStr = [NSString stringWithFormat:@"select Longitude,Latitude from UserJourneyLocation"];
const char *sqlStatement = [sqlStr UTF8String];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
locations = [NSMutableArray array];
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
int i = sqlite3_step(compiledStatement);
// NSLog(@"%i",i); / comment this line or use NSLog(@"%d",i);
// Make sure your longitude is double in database. else change the fetching value declaration
// We use 0 for dLongitude instead of 3 because in the select statement, it is the first value to be fetched irrespective of your table structure
// and same for dLatitude.
double dLongitude = sqlite3_column_double(compiledStatement, 0);
double dLatitude = sqlite3_column_double(compiledStatement, 1);
[locations addObject:[NSString stringWithFormat:@"%d ,%d",dLongitude,dLatitude]];
NSLog(@"%@",locations);
}
result = [locations componentsJoinedByString:@" "]; // same as `fake_location`
// Get file path here
NSError *error;
if ( [result writeToFile:dbPath atomically:YES encoding:NSUTF8StringEncoding error:&error] ) {
NSLog(@"%@", [error localizedDescription]);
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);