嵌套Sql语句

时间:2013-03-11 15:16:34

标签: ios sqlite

您好我已经在我的app委托中创建了一个函数来删除我的数据库的冗余,我不确定我是否编码正确,因为我必须执行SQL语句的嵌套以找出DB中的错误。

任何人都可以告诉我错误的地方,因为应用程序在模拟器中运行良好并且在设备中崩溃。

我甚至不确定在哪里放置finalize和sqlite3_close。请帮助

 -(void) removeRedundancy2
    {
        NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        dbPathString = [[docPaths objectAtIndex:0] stringByAppendingPathComponent:@"turfnutritiontool_ver_99.db"];
        sqlite3_stmt *selectStmt;
        sqlite3_stmt *selectStmt1;
        BOOL isMyFileThere = [[NSFileManager defaultManager] fileExistsAtPath:dbPathString];

        if (isMyFileThere)
        {
            if (sqlite3_open([dbPathString UTF8String], &database1)==SQLITE_OK)
            {
                // TO REMOVE FROM from tnt_scenario_product when NO ProductID Found

                NSString *querySql2= [NSString stringWithFormat:@"SELECT productid from tnt_scenarioproduct"];
                const char* query_sql2 = [querySql2 UTF8String];

                if(sqlite3_prepare_v2(database1, query_sql2, -1, &selectStmt, NULL) == SQLITE_OK)
            {
                while (sqlite3_step(selectStmt) == SQLITE_ROW)
                {
                    int productid =  sqlite3_column_int(selectStmt, 0);
                    // NSLog(@"ProductId1 =%d",productid);

                    NSString *querySql21= [NSString stringWithFormat:@"SELECT productid from tnt_productcontent WHERE productid = %d",productid];
                    const char* query_sql21 = [querySql21 UTF8String];

                    if(sqlite3_prepare_v2(database1, query_sql21, -1, &selectStmt1, NULL) == SQLITE_OK)
                    {
                        if (sqlite3_step(selectStmt1) == SQLITE_ROW)
                        {
                            // DO NOTHING
                        }
                        else
                        {   // to delete scenario without product id
                            NSLog(@"Delete this Product from TPC 2 %d",productid);
                            NSString *querydelete2= [NSString stringWithFormat:@"DELETE from tnt_scenarioproduct WHERE productid = %d",productid];

                            const char* query_delete2 = [querydelete2 UTF8String];
                            char *error;
                            sqlite3_exec(database1, query_delete2, NULL, NULL, &error);
                            NSLog(@"error=%s ",error);
                            sqlite3_finalize(selectStmt1);
                        }
                    }
                    sqlite3_finalize(selectStmt1);
                    sqlite3_close(database1);

                }
                sqlite3_finalize(selectStmt);
            }
            sqlite3_close(database1);
        }
        sqlite3_close(database1);

    }
}

1 个答案:

答案 0 :(得分:0)

致电sqlite3_open后,您必须仅为该连接拨打sqlite3_close一次。 致电sqlite3_prepare_v2后,您必须仅为该声明致电sqlite3_finalize一次。

if (sqlite3_open([dbPathString UTF8String], &database1)==SQLITE_OK)
{
    ...
    if(sqlite3_prepare_v2(database1, query_sql21, -1, &selectStmt1, NULL) == SQLITE_OK)
    {
        ...
    }
    sqlite3_finalize(selectStmt);
    ....
}
sqlite3_close(database1);

此外,您不应该为两个不同的查询重用相同的变量(selectStmt),以防止混淆其生命周期。