0
I’m performing a Insert
in my Sqlite, however, I’m getting!
The Nslog returns : 2014-06-01 23:15:35.664 goTask[25379:60b] open Bank: Bank opened successfully 2014-06-01 23:15:35.666 goTask[25379:60b] createdTable: Table successfully created! 2014-06-01 23:15:41.250 goTask[25379:60b] inTarefa: Could not insert task
#import "BDManager.h"
@implementation BDManager
-(NSString *)filePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [[paths objectAtIndex:0]stringByAppendingString:@"tarefas.sql"];
}
-(void)abrirBanco {
if (sqlite3_open([[self filePath]UTF8String], &_tarefasDB)!=SQLITE_OK) {
sqlite3_close(_tarefasDB);
NSLog(@"abrirBanco: Erro ao abrir o Banco");
}
NSLog(@"abrirBanco: Banco aberto com sucesso");
}
-(void)criarTabela {
char *err;
NSString *sql = @"CREATE TABLE IF NOT EXISTS TAREFASLIST (ID SERIAL, TITULO TEXT, RESPONSAVEL TEXT, DESCRICAO TEXT, ENTREGA TEXT, LEMBRETE INTEGER);";
if (sqlite3_exec(_tarefasDB, [sql UTF8String], NULL, NULL, &err)!=SQLITE_OK) {
sqlite3_close(_tarefasDB);
NSLog(@"criarTabela: Não foi possivel criar a tabela");
}
NSLog(@"criarTabela: Tabela criada com sucesso!");
}
-(void)inserirTarefa:(Tarefas *)taf {
NSDateFormatter *dtf = [[NSDateFormatter alloc]init];
NSString *date = [dtf stringFromDate:taf.entrega];
NSString *sql = [NSString stringWithFormat:@"INSERT INTO TAREFASLIST (titulo, responsavel, descricao, entrega, lembrete) VALUES (\"%@\",\"%@\",\"%@\",\"%@\",\"%i\")", taf.titulo, taf.responsavel, taf.descricao, date, taf.lembrete];
const char *insert_stmt = [sql UTF8String];
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(_tarefasDB, insert_stmt, -1, &statement, NULL)!=SQLITE_OK) {
NSLog(@"inserirTarefa: Não foi possível inserir a tarefa");
sqlite3_reset(statement);
} else {
NSLog(@"inserirTarefa: Tarefa inserida com sucesso");
sqlite3_reset(statement);
}
}
-(NSMutableArray *)resgataListanaTarefa {
NSMutableArray *lista = [[NSMutableArray alloc]init];
NSString *sql = [NSString stringWithFormat:@"SELECT titulo FROM TAREFASLIST"];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(_tarefasDB, [sql UTF8String], -1, &statement, nil)==SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
char *ti = (char *) sqlite3_column_text(statement, 0);
NSString *titulo = [NSString stringWithUTF8String:ti];
[lista addObject:titulo];
return lista;}}
else {
NSLog(@"Não encontrado!");
return nil;
sqlite3_finalize(statement);
}
return lista;
}
@end
Is this code correct? Because at first there is dead code after if/they there. Is there any way to check? And what kind of error occurs?
– Wakim
Apparently it is, the problem happens that it returns the error Nslog (NO) and in the array containing the items does not update... only that apparently it is correct the codes... before I was able to run the Insert however, it only ran at the first execution of the file.. if I delete the app and run again it works... I don’t know this strange!!
– user9133
Is it not a competition problem? In what order are you calling these functions? Could you try to generate a more detailed error? Take a look at this link: http://stackoverflow.com/questions/6171056/sqlite3-error-ios, and log complete error messages to see what might be.
– Wakim
That’s right @Wakim the problem was in order, opening the bank mainly!! thank you so much for the help!!!
– user9133