简体   繁体   中英

failing to insert data into sqlite database via test case iOS

I am trying to insert data into one of my tables in a sqlite database via a test case.

Following is the test method:

-(void)testAddContact2
{
    [ContactsModel getInstance].mContactName = @"Arvind";
    [ContactsModel getInstance].mContactNo = @"556";
    [ContactsModel getInstance].mContactDob = @"July 10, 1988";
    [ContactsModel getInstance].mContactGender = @"Male";
    [ContactsModel getInstance].mContactEmail = @"arv@bnsad.co";
    [ContactsModel getInstance].mContactLatitude = @"33";
    [ContactsModel getInstance].mContactLongitude = @"44";

    STAssertFalse(([[ContactsModel getInstance] addNewContact] == TRUE),@"passed");
}

This is the method I'm trying to test:

-(BOOL)addNewContact
{
    BOOL tempFlag = NO;

    sqlite3_stmt    *statement;
    sqlite3 *mDiaryTemp = nil;

    const char *dbpath = [[UseDb getInstance].mDatabasePathDb UTF8String];

    if (sqlite3_open(dbpath, &mDiaryTemp) == SQLITE_OK)
    {
        NSString *insertSQL1=[NSString stringWithFormat:@"INSERT INTO CONTACTS VALUES(\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\")",
                              [ContactsModel getInstance].mContactName,
                              [ContactsModel getInstance].mContactNo,
                              [ContactsModel getInstance].mContactGender,
                              [ContactsModel getInstance].mContactDob,
                              [ContactsModel getInstance].mContactEmail,
                              [ContactsModel getInstance].mContactLatitude,
                              [ContactsModel getInstance].mContactLongitude];

        const char *insert_stmt1 = [insertSQL1 UTF8String];

        sqlite3_prepare_v2(mDiaryTemp, insert_stmt1, -1, &statement, NULL);

        NSString *tempName = [NSString stringWithFormat:@"%@",[ContactsModel getInstance].mContactName];
        tempName = [tempName stringByAppendingString:@"     "];

        NSString *tempNo = [NSString stringWithFormat:@"%@",[ContactsModel getInstance].mContactNo];
        tempName = [tempName stringByAppendingString:tempNo];

        [[ShowContactsViewController getInstance].phoneContacts addObject:tempName];

        NSLog(@"%d",[[ShowContactsViewController getInstance].phoneContacts count]);

        //  NSLog(@"%d",sqlite3_step(statement));
        if (sqlite3_step(statement) == SQLITE_DONE)
        {
            sqlite3_finalize(statement);
            sqlite3_close(mDiaryTemp);
            tempFlag = YES;
        }
        else
        {
            sqlite3_finalize(statement);
            sqlite3_close(mDiaryTemp);
            tempFlag = NO;
        }
    }
    if(tempFlag)
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

The following condition always fails: if (sqlite3_step(statement) == SQLITE_DONE) I had posted a similar question earlier, but the issue was resolved when I used shared instances instead of new objects. But here I'm failing to insert even after using shared instances.

On iOS, you should consider using CoreData rather than using SQLite directly. CoreData, by default, persists to SQLite and it has much more friendly documentation. Also, since CoreData is not a C API, it will interoperate more nicely with the rest of your Objective-C code. There's also nice Xcode integration when using CoreData, which will let you define schemas easily. You'll also avoid, in many cases, writing SQL queries.

For comparisons between the two see this related question.

Try adding a semi-colon to the very end of your insertSQL1 , and see where that gets you. Also be sure that there are no escape characters substituted into this statement.

Edit:

@"INSERT INTO CONTACTS VALUES(\\"%@\\",\\"%@\\",\\"%@\\",\\"%@\\",\\"%@\\",\\"%@\\",\\"%@\\")>>>;<<<"...

http://www.sqlite.org/syntaxdiagrams.html#sql-stmt-list

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM