sqlite3_prepare_v2返回21& “无法准备状态:失去记忆”

时间:2012-06-12 17:08:25

标签: iphone database sqlite storing-data

我收到这些错误,一切都在这里。我所做的就是使用Xcode指定的路径创建一个.db文件,然后创建一个显示值为列的表。然后我尝试继续填充表格,但在标题中收到上面的错误(似乎这些是导致我头痛的原因)。如果有人能够对此有所了解,我们将不胜感激。

#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);
    }
}

0 个答案:

没有答案