Sqlite данные не получают выборку

1

Я пытаюсь извлечь значение из базы данных. Но он не получается. Я пытаюсь использовать этот код, но не получил значение. Когда я использую точку прерывания в программе, у меня есть долгота, широта его типа данных двойна в базе данных

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
    Я не понимаю - какой тип данных долгота и широта в базе данных? Если это реальный или двойной, почему вы используете sqlite3_column_text вместо двойной долготы = sqlite3_column_double (compiledStatement, 3); ???
  • 0
    Я хочу сохранить эти данные в массиве [местоположения addObject: [NSString stringWithFormat: @"%@ "% @"%@,% @"%@", dLongitude, dLatitude]];
Показать ещё 4 комментария
Теги:
iphone

2 ответа

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);
                }
  • 0
    Он показывает мне ошибку [TLocationManager init] ': /Users/HarishYadav/Desktop/JourneyMapper/Journey/TLocationManager.m:88: ошибка: ожидается'] 'до появления строковой константы, метод' -initWithFormat 'не найден
  • 0
    отредактировано, попробуйте еще раз
Показать ещё 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);
  • 0
    Это работает, но когда дело доходит до строки NSString *ordins = [[[NSString alloc] initWithFormat: @"%f "% f,% f", долгота, широта] autorelease]; происходит сбой, и база данных My iPhone Simulator / 4.2 / Applications получает ошибку, а база данных не открывает проблему. Но я не понимаю, почему моя база данных получает error.it не открывается
  • 0
    Попробуйте это: NSString *ordins = [NSString stringWithFormat: @"%f "% f,% f", долгота, широта];

Ещё вопросы

Сообщество Overcoder
Наверх
Меню