SQLITE INSERT problem when app starts

0

I'm doing Insert in my SQLite, however, I'm getting it!

NSLog returns: 2014-06-01 23: 15: 35.664 goTask [25379: 60b] open Bank: open bank successfully 2014-06-01 23: 15: 35.666 goTask [25379: 60b] createTable: Table created successfully! 2014-06-01 23: 15: 41.250 goTask [25379: 60b] insertTest: 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
    
asked by anonymous 01.06.2014 / 21:33

1 answer

4

If you are really interested in learning how to use SQLite to control your iOS App data, I recommend that you follow the tutorial below, it is a great guide:

IOS -5-SDK-Database-Insert-Update-Delete-with-SQLite-and-Objective-CC-How-To iphone-sdk -living-data-from-a-database-sqlite

Or see the example below of how to do an insert.

static sqlite3_stmt *insertStmt = nil;

if(insertStmt == nil) 
{
    insertSql = "INSERT INTO Loginchk (uname,password) VALUES(?,?)";
    if(sqlite3_prepare_v2(database, insertSql, -1, &insertStmt, NULL) != SQLITE_OK)
        NSAssert1(0, @"Error while creating insert statement. '%s'", sqlite3_errmsg(database));
}

sqlite3_bind_text(insertStmt, 1, [Gunameq UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insertStmt, 2, [Gpassq UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(insertStmt))
    NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
else
    NSLog("Inserted");

//Reset the add statement.
sqlite3_reset(insertStmt);
insertStmt = nil; 

I hope it helps solve your problem.

    
02.06.2014 / 22:53