如何在 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?..
我希望你能帮助我,谢谢!