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!
See Question&Answers more detail:
os