How to use sqlite in ios 7?

本小妞迷上赌 提交于 2019-12-16 18:04:31

问题


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

UPDATE!

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;

}

The error is triggered in the NSLog (@ "Error making INSERT:% s", sqlite3_errmsg (database)); and the response has been making INSERT Error: file is encrypted or is not a database

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

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!


回答1:


You should look at error codes.

Thus, the line that says:

NSLog(@"Error making INSERT");

should say:

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

Likewise the line that says:

NSLog(@"Error la SELECT");

should say:

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

Only by looking at those error messages can you effectively diagnose the problem.


You report that it says: "file is encrypted or is not a database"

That suggests that your database has gotten corrupted somehow (assuming you never used encryption). You'll want to recreate it.


Unrelated to your broader problem, your code for the INSERT statement bears a line that says:

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

That should be:

if (sqlite3_step(sentencia) != SQLITE_DONE)
    NSLog(@"Error in INSERT step: %s", sqlite3_errmsg(database));



回答2:


Solved!

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 ..

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



来源:https://stackoverflow.com/questions/18993375/how-to-use-sqlite-in-ios-7

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!