sqlite3_prepare_v2 devolviendo 21 y "no se pudo preparar la declaración: sin memoria"

Recibo estos errores, todo está aquí. Todo lo que estoy haciendo es crear un archivo .db con una ruta especificada por Xcode y luego crear una tabla con los valores mostrados como columnas. Luego trato de seguir adelante y completar la tabla, pero recibo los errores anteriores en el título (parece que estos son los que están causando mis dolores de cabeza). Si alguien pudiera arrojar algo de luz sobre esto, sería muy apreciado.

#import "Email.h"

@implementation Email

@synthesize to, cc, bcc, fromName, fromEmail, headers, datetime, subject, body, folder, state, attachments;

-(void)StoreEmail:(Email *)email{
    NSString *docsDir;
    NSArray *dirPaths;

    // Get the documents directory
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    docsDir = [dirPaths objectAtIndex:0];

    // Build the path to the database file
    databasePath = [[NSString alloc] 
    initWithString: [docsDir stringByAppendingPathComponent: 
    @"contacts2.db"]];

    NSFileManager *filemgr = [NSFileManager defaultManager];

    if ([filemgr fileExistsAtPath: databasePath ] == NO)
    {
        const char *dbpath = [databasePath UTF8String];

        if (sqlite3_open(dbpath, &contactDB2) == SQLITE_OK)
        {
            char *errMsg;
            const char *sql_stmt = "CREATE TABLE IF NOT EXISTS CONTACTS2 (ID INTEGER PRIMARY KEY AUTOINCREMENT, FROM TEXT, NAME TEXT, SUBJECT TEXT, BODY TEXT, ATTACHMENTS TEXT, HEADERS TEXT, DATETIME TEXT, FOLDER TEXT, STATE TEXT)";

            if (sqlite3_exec(contactDB2, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"path" message:@"failed to create table" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert show];
            }
            sqlite3_close(contactDB2);
        } 
        else 
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"path" message:@"failed to open/create database" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }
    }

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"path" message:databasePath delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];

    sqlite3_stmt *statement;
    const char *dbpath = [databasePath UTF8String];

    if (sqlite3_open(dbpath, &contactDB2) == SQLITE_OK)
    {
        NSLog(@"This is what open returns %d", sqlite3_open(dbpath, &contactDB2));
        //NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO CONTACTS2 (from, name, subject, body, attachments, headers, datetime, folder, state) VALUES (\"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\")", email.fromEmail, email.fromName, email.subject, email.body, email.attachments, email.headers, email.datetime, email.folder, email.state];
        NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO CONTACTS2 (from, name) VALUES (\"%@\", \"%@\")", email.fromEmail, email.fromName];
        const char *insert_stmt = [insertSQL UTF8String];
        sqlite3_prepare_v2(contactDB2, insert_stmt, -1, &statement, NULL);
        NSLog(@"could not prepare statemnt: %s\n", sqlite3_errmsg(database));
        //NSLog(@"%d", sqlite3_prepare_v2(contactDB2, insert_stmt, -1, &statement, NULL));
        if (sqlite3_step(statement)== SQLITE_DONE)
        {
            NSLog(@"2");
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"path" message:@"email added" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];

        } 
        else 
        {
            NSLog(@"%d", sqlite3_step(statement));
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"path" message:@"failed to add email" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }
        sqlite3_finalize(statement);
        sqlite3_close(contactDB2);
    }
}

preguntado el 12 de junio de 12 a las 18:06

0 Respuestas

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.