SQLITE INSERT problem when app starts

Asked

Viewed 140 times

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?

  • 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!!

  • 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.

  • That’s right @Wakim the problem was in order, opening the bank mainly!! thank you so much for the help!!!

1 answer

4

If you’re really interested in learning how to use Sqlite to control your iOS App data, I recommend you follow the tutorial below, it’s a great guide:

IOS-5-SDK-Database-Insert-Update-Delete-with-Sqlite-and-Objective-C-C-How-To iphone-sdk-reading-data-from-a-database-sqlite

Or see the example below of how to make 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.