Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use sqlite in ios 7?
    text
    copied!<p>How to use sqlite in ios 7? I'm trying to use code ios 6 and it does not work in ios 7</p> <h2><strong>UPDATE!</strong></h2> <p>I export a database, to desktop, change name and drag and drop to xcode <img src="https://i.stack.imgur.com/U3Zl9.jpg" alt="enter image description here"> Add the code:</p> <pre><code>// SQLite // Conexion DB NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); NSString *documentDirectory = [paths objectAtIndex:0]; _databasePath = [documentDirectory stringByAppendingPathComponent:@"ambisi_test.sqlite"]; [self loadDB]; // --&gt; 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:&amp;error]; if(!exit) NSLog(@"%@",[error localizedDescription]); </code></pre> <p>}</p> <pre><code>- (void) clickFavorites{ sqlite3 *database = NULL; sqlite3_stmt *sentencia = NULL; // Si la BD se ha abierto bien if(sqlite3_open([appDelegate.databasePath UTF8String], &amp;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, &amp;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, &amp;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); </code></pre> <p>}</p> <pre><code>- (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, &amp;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; </code></pre> <p>}</p> <p>The error is triggered in the NSLog (@ "Error making INSERT:% s", sqlite3_errmsg (database)); and the response has been making INSERT Error: <strong>file is encrypted or is not a database</strong></p> <p>I've also tried to recreate the DB and import it several times, but still with the same error..</p> <p>In the iOS simulator iOS 6 if it works but in iOS7 is not working ... is rare, someone tried to use SQLite in iOS7? ..</p> <p>I hope you can help me, thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload