0

我将 xml 传递给我的 addRowToTheDatabase 函数。在这个函数中,我只是解析 xml 文件并将元素检索到 xml 文件中。现在我正在测试这个应用程序。如何从单元测试用例中测试是否添加到数据库中的行?

4

1 回答 1

1

// 我非常快地处理了我的一个查询。我假设您已经在 SQLite 中连接到一个数据库,所以其中大部分是不需要的。但这是查询的基本结构。只要数据库驻留在您的应用程序本身中。

-(void) basicQuery
{
    @try {  
        NSFileManager *fileManager = [NSFileManager defaultManager]; 
        NSString *theDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"databasename.sqlite"]; 
        BOOL success = [fileManager fileExistsAtPath:theDBPath]; 
        if (!success) {
            NSLog(@"Failed to find database file '%@'.", theDBPath);
        } 
        if (!(sqlite3_open([theDBPath UTF8String], &database) == SQLITE_OK)) { 
            NSLog(@"An error opening database, normally handle error here.");  
        }  

        const char *sql = "SELECT * FROM TABLE"; // your basic query
        sqlite3_stmt *statement; 
        if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) != SQLITE_OK){ 
            NSLog(@"Error, failed to prepare statement, normally handle error here."); 
        } 
        while (sqlite3_step(statement) == SQLITE_ROW) { 
          NSString *value = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]; 
           NSLog(@"value = %@",value);

        } 
        if(sqlite3_finalize(statement) != SQLITE_OK){ 
            NSLog(@"Failed to finalize data statement, normally error handling here."); 
        } 
        if (sqlite3_close(database) != SQLITE_OK) { 
            NSLog(@"Failed to close database, normally error handling here."); 
        } 
    } 
    @catch (NSException *e) { 
        NSLog(@"An exception occurred: %@", [e reason]); 
        return nil; 
    } 
}
于 2012-07-06T12:06:57.143 回答