-1

如何在 iOS 7 中使用 sqlite?我正在尝试使用代码 ios 6,但它在 ios 7 中不起作用

更新!

我将数据库导出到桌面,更改名称并拖放到 xcode 在此处输入图像描述 添加代码:

// SQLite
// Conexion DB
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];

_databasePath = [documentDirectory stringByAppendingPathComponent:@"ambisi_test.sqlite"];
[self loadDB];
// --> End SQLite



-(void)loadDB{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];

NSString *writableDBPath = [documentDirectory stringByAppendingPathComponent:@"ambisi_test.sqlite"];

BOOL exito = [fileManager fileExistsAtPath:writableDBPath];
if(exito) return;

NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"ambisi_test.sqlite"];

BOOL exit = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];

if(!exit) NSLog(@"%@",[error localizedDescription]);

}

- (void) clickFavorites{

sqlite3 *database = NULL;
sqlite3_stmt *sentencia = NULL;

// Si la BD se ha abierto bien
if(sqlite3_open([appDelegate.databasePath UTF8String], &database) == SQLITE_OK){
    // Genero la query
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO estaciones (\"id_number\", \"name\",\"addres\",\"latitude\",\"longitude\") VALUES (\"%i\",\"%@\",\"%@\",\"%f\",\"%f\")", self.modelAnnotation.number, self.modelAnnotation.name,self.modelAnnotation.address, self.modelAnnotation.lat,self.modelAnnotation.lng];
    NSLog(@"%@",sql);
    // Si esta no contien errores
    if(sqlite3_prepare_v2(database, [sql UTF8String], -1, &sentencia, NULL)==SQLITE_OK){

        // Si la estación no existe ya como favorita, la almacenaré, o en el caso contrario la elimnaré
        if(![self isFavoriteWithIdStation:self.modelAnnotation.number database:database]){
            // Insisto hasta que se inserte
            if (sqlite3_step(sentencia) != SQLITE_DONE){
                 NSLog(@"Error in INSERT step: %s", sqlite3_errmsg(database));
            }else{
                // Ademas de cambiarle la imagen
                UIImage *image = [UIImage imageNamed:@"quitar-favorito"];
                [_button setBackgroundImage:image forState:UIControlStateNormal];
            }

        }else{ // La elimino de favoritas
            // Genero la query
            NSString *sql = [NSString stringWithFormat:@"DELETE FROM estaciones WHERE id_number = \"%i\"",self.modelAnnotation.number];
            // Si esta no contien errores
            if(sqlite3_prepare_v2(database, [sql UTF8String], -1, &sentencia, NULL)==SQLITE_OK){
                // Insisto hasta que se inserte
                if (sqlite3_step(sentencia) != SQLITE_DONE){
                    NSLog(@"Error in DELETE step: %s", sqlite3_errmsg(database));
                }else{
                    // Ademas de cambiarle la imagen
                    UIImage *image = [UIImage imageNamed:@"addfav"];
                    [_button setBackgroundImage:image forState:UIControlStateNormal];
                }
            }
        }
    }else{
        NSLog(@"Error making INSERT: %s",sqlite3_errmsg(database));
    }
    sqlite3_finalize(sentencia);
}else{
    NSLog(@"Doesn't open Database: %s",sqlite3_errmsg(database));
}
sqlite3_close(database);

}

- (BOOL) isFavoriteWithIdStation:(int) idStation database:(sqlite3 *)db{

sqlite3_stmt *sentencia = NULL;
NSString *sql = [NSString stringWithFormat:@"SELECT id_number FROM estaciones"];
NSLog(@"%i",idStation);
NSString * ide = [NSString stringWithFormat:@"%i",idStation];
if(sqlite3_prepare_v2(db, [sql UTF8String], -1, &sentencia, NULL)==SQLITE_OK){
    while(sqlite3_step(sentencia) == SQLITE_ROW){
        NSLog(@"%i",idStation);
        NSString *number = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sentencia, 0)];
        //NSString *id_tutorialString = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
        if([number isEqualToString:ide]){
            NSLog(@"NUMBER:%@",number);
            NSLog(@"NUMBER:%i",idStation);
            sqlite3_finalize(sentencia);
            return YES;
        }else{
            NSLog(@"Error en el condicional");
        }

    }
}else{
    NSLog(@"Error making SELCET: %s",sqlite3_errmsg(db));
}
sqlite3_finalize(sentencia);

return NO;

}

该错误在NSLog中触发(@"Error making INSERT:%s", sqlite3_errmsg(database));并且响应一直在进行 INSERT Error: file is encrypted or is not a database

我也尝试重新创建数据库并多次导入,但仍然出现相同的错误..

在 iOS 模拟器 iOS 6 中,如果它可以工作,但在 iOS7 中不工作……很少见,有人试图在 iOS7 中使用 SQLite?..

我希望你能帮助我,谢谢!

4

2 回答 2

2

您应该查看错误代码。

因此,这行说:

NSLog(@"Error making INSERT");

应该说:

NSLog(@"Error making INSERT: %s", sqlite3_errmsg(database));

同样的行说:

NSLog(@"Error la SELECT");

应该说:

NSLog(@"Error la SELECT: %s", sqlite3_errmsg(database));

只有查看这些错误消息,您才能有效地诊断问题。


您报告它说:“文件已加密或不是数据库”

这表明您的数据库已以某种方式损坏(假设您从未使用过加密)。你会想要重新创建它。


与您的更广泛问题无关,您的INSERT语句代码包含一行:

// Insisto hasta que se inserte
while(sqlite3_step(sentencia) == SQLITE_OK);

那应该是:

if (sqlite3_step(sentencia) != SQLITE_DONE)
    NSLog(@"Error in INSERT step: %s", sqlite3_errmsg(database));
于 2013-09-24T23:14:20.677 回答
1

解决了!

我在使用 firefox 插件导出数据库时犯的主要错误..

解决方案是在创建数据库时存储所需位置而不导出..

我希望如果有人碰巧有同样的情况,这里是解决方案,感谢您对@Rob@Hot Licks的关注..

于 2013-09-25T20:26:48.487 回答