如何在ios 7中使用sqlite? [英] How to use sqlite in ios 7?

查看:154
本文介绍了如何在ios 7中使用sqlite?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to use sqlite in ios 7? I'm trying to use code ios 6 and it does not work in ios 7

我将数据库导出到桌面,更改名称并拖放到xcode

添加代码:

I export a database, to desktop, change name and drag and drop to xcode Add the code:

// 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(@出错INSERT:%s,sqlite3_errmsg(数据库));
并且响应一直在进行INSERT错误:文件已加密或不是数据库

我还试图重新创建数据库并导入几次,但仍然有相同的错误..

I've also tried to recreate the DB and import it several times, but still with the same error..

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

In the iOS simulator iOS 6 if it works but in iOS7 is not working ... is rare, someone tried to use SQLite in iOS7? ..

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

I hope you can help me, thanks!

推荐答案

解决了!

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

The main mistake I committed in exporting the database using firefox plugin ..

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

The solution is to store the desired location without export, at the time of the creation of the database ..

我希望如果有人碰巧有相同的,这是解决方案,谢谢感兴趣的是 @Rob @Hot Licks ..

I hope if someone happens to have the same, here is the solution, thanks for your interest for @Rob and @Hot Licks..

这篇关于如何在ios 7中使用sqlite?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆