我正在尝试通过测试用例将数据插入到 sqlite 数据库中的一个表中。
以下是测试方法:
-(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");
}
这是我正在尝试测试的方法:
-(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;
}
}
以下情况总是失败:if (sqlite3_step(statement) == SQLITE_DONE)
我之前发布了一个类似的问题,但是当我使用共享实例而不是新对象时问题得到了解决。但是,即使在使用共享实例之后,我也无法插入。