无法在sqlite查询中使用

时间:2012-06-04 11:16:07

标签: iphone ios sqlite

我试图在我的代码中使用以下方式:

-(void)getMatchedContacts{
if(sqlite3_open([[self GetDBPath] UTF8String], &database)== SQLITE_OK) {
    NSMutableArray *arrFBContacts = [self getFBContacts];
    if([arrFBContacts count]>0){
        sqlite3_stmt *stmt;
        for (MContact *mc in arrFBContacts){
            const char *sql = "select count(*) from contacts where fname LIKE '?' and lname LIKE '?'";
            if (sqlite3_prepare_v2(database, sql, -1, &stmt, NULL) != SQLITE_OK) {
                NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
            }
            else{
                NSLog(@"%@",[NSString stringWithFormat:@"%@",mc.firstname]);
                NSLog(@"%@",[NSString stringWithFormat:@"%@",mc.lastname]);
                sqlite3_bind_text(stmt, 1, [[NSString stringWithFormat:@"%@",mc.firstname]  UTF8String], -1, SQLITE_TRANSIENT);
                sqlite3_bind_text(stmt, 2, [[NSString stringWithFormat:@"%@",mc.lastname]  UTF8String], -1, SQLITE_TRANSIENT);

                int count =0;
                if(sqlite3_step(stmt) == SQLITE_ROW) {
                    count = sqlite3_column_int(stmt, 0);
                }
                sqlite3_reset(stmt);
                if(count>0){
                    const char *sql1 = "update fbContacts set matched = 1 where id = ?";
                    if (sqlite3_prepare_v2(database, sql1, -1, &stmt, NULL) != SQLITE_OK) {
                        NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
                    }
                    else {
                        sqlite3_bind_int(stmt, 1, mc.fblocalcontactid);
                        sqlite3_step(stmt);
                    }
                    sqlite3_reset(stmt);
                }
            }
        }
    }
}

}

但是我无法从第一个sql中获取count变量的任何值。任何人都可以帮我解决y程序中的错误。

此致 的Pankaj

1 个答案:

答案 0 :(得分:0)

您可以按照以下方式检查我的功能:其中,就像查询工作正常一样。

-(NSMutableArray *)getCategorySearchNoDueDate:(int)catId:(NSString *)searchWord{
searchWord = [@"%" stringByAppendingFormat:@"%@%%",searchWord];

NSMutableArray *tempArray = [[NSMutableArray alloc] init];

oClsDBFunction = [[clsDBFunction alloc] init];

appDelegate = (WFAppDelegate *)[[UIApplication sharedApplication] delegate];
if (appDelegate.result == SQLITE_OK) {
    const char *sql;

    NSString *sqlString = @"SELECT \"Project\" as objectType, \"0\" as tID, projectId as pID, \"0\" as refTID, referenceId as refPID, "
    "title as pTitle, \"\" as tTitle, description as Desc, "
    "startDate as StartDate, startTime as StartTime, "
    "dueDate as DueDate, dueTime as DueTime, compDate as CompDate, compTime as CompTime, "
    "priorityId as PriorityId, categoryId as CategoryId, "
    "isCompleted as IsCompleted, status as Status, isDeleted as IsDeleted, "
    "remark as Remark, repeatId as RepeatId, alertCount as AlertCount, \"0\" as TaskType, "
    "(select count(*) from tblTask, tblReference "
    "where tblTask.referenceId=tblReference.referenceId and tblReference.projectId = tblProject.projectId) as TotalTask, "
    "(select count(*) from tblTask, tblReference "
    "where tblTask.referenceId=tblReference.referenceId and tblReference.projectId = tblProject.projectId and tblTask.isCompleted=1) "
    "as CompletedTask "
    "FROM tblProject "
    "WHERE dueDate='' and "
    "IsCompleted=0 and CategoryId=? and (tblProject.title like ? or tblProject.description like ?) "
    "UNION "

    "SELECT \"Task\" as objectType, tblTask.taskId as tID, \"0\" as pID,  tblTask.referenceId as refTID, \"0\" as refPID, "
    "\"\" as pTitle, title as tTitle, description as Desc, "
    "\"\" as StartDate, \"\" as StartTime, "
    "dueDate as DueDate, dueTime as DueTime, compDate as CompDate, compTime as CompTime, "
    "priorityId as PriorityId, categoryId as CategoryId, "
    "isCompleted as IsCompleted, status as Status, isDeleted as IsDeleted, "
    "remark as Remark, repeatId as RepeatId, alertCount as AlertCount, taskTypeId as TaskType, "
    "\"0\" as TotalTask, "
    "\"0\" as CompletedTask "
    "FROM tblTask, tblReference  "
    "WHERE IsCompleted=0 and "
    "dueDate='' and "
    "CategoryId=? and "
    "tblTask.referenceId = tblReference.referenceId and tblReference.projectId=0 and (tblTask.title like ? or tblTask.description like ?) "
    "UNION "
    "SELECT \"Task\" as objectType, tblTask.taskId as tID, tblProject.projectId as pID,  tblTask.referenceId as refTID, \"0\" as refPID, "
    "tblProject.title as pTitle, tblTask.title as tTitle, tblTask.description as Desc, "
    "\"\" as StartDate, \"\" as StartTime, "
    "tblTask.dueDate as DueDate, tblTask.dueTime as DueTime, tblTask.compDate as CompDate, tblTask.compTime as CompTime, "
    "tblTask.priorityId as PriorityId, tblTask.categoryId as CategoryId, "
    "tblTask.isCompleted as IsCompleted, tblTask.status as Status, tblTask.isDeleted as IsDeleted, "
    "tblTask.remark as Remark, tblTask.repeatId as RepeatId, tblTask.alertCount as AlertCount, taskTypeId as TaskType, "
    "\"0\" as TotalTask, "
    "\"0\" as CompletedTask "
    "FROM tblTask, tblReference, tblProject "
    "WHERE tblTask.IsCompleted=0 and "
    "tblTask.dueDate='' and  "
    "tblTask.CategoryId=? and "
    "tblTask.referenceId = tblReference.referenceId and tblReference.projectId!=0 and "
    "tblProject.projectId = tblReference.projectId and (tblTask.title like ? or tblTask.description like ?) "
    "order by  PriorityId DESC, IsCompleted";

    sql = [sqlString UTF8String];

    sqlite3_stmt *statement;

    if (sqlite3_prepare_v2(appDelegate.database, sql, -1, &statement, NULL) == SQLITE_OK) {

        NSString *strCatId = [NSString stringWithFormat:@"%d", catId];
        sqlite3_bind_text(statement, 1, [strCatId UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(statement, 2, [searchWord UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(statement, 3, [searchWord UTF8String], -1, SQLITE_TRANSIENT);

        sqlite3_bind_text(statement, 4, [strCatId UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(statement, 5, [searchWord UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(statement, 6, [searchWord UTF8String], -1, SQLITE_TRANSIENT);

        sqlite3_bind_text(statement, 7, [strCatId UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(statement, 8, [searchWord UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(statement, 9, [searchWord UTF8String], -1, SQLITE_TRANSIENT);

        while (sqlite3_step(statement) == SQLITE_ROW) {


            NSString *strObjectType = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)];

            NSString *strTaskId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
            NSString *strProjectId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)];
            NSString *strRefTaskId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 3)];             
            NSString *strRefProjectId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 4)];              
            NSString *strProjectTitle = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 5)];              
            NSString *strTaskTitle = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 6)];             
            NSString *strDescription = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 7)];

            NSString *strStartDate = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 8)];
            NSString *strStartTime = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 9)];
            NSString *strDueDate = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 10)];
            NSString *strDueTime = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 11)];              
            NSString *strCompDate = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 12)];
            NSString *strCompTime = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 13)];

            NSString *strPriorityId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 14)];
            NSString *strCategoryId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 15)];
            NSString *strIsCompleted = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 16)];
            NSString *strStatus = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 17)];
            NSString *strIsDeleted = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 18)];

            NSString *strRemark = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 19)];               
            NSString *strRepeatId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 20)];
            NSString *strAlertCount = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 21)];               
            NSString *strTaskType = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 22)];

            NSString *strTotalTask = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 23)];
            NSString *strCompletedTask = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 24)];


            oMainPage = [[MainPage alloc] init];
            oMainPage.objectType = strObjectType;

            oMainPage.tId = strTaskId;
            oMainPage.pId = strProjectId;
            oMainPage.refTId = strRefTaskId;                
            oMainPage.refPId = strRefProjectId;
            oMainPage.pTitle = strProjectTitle;
            oMainPage.tTitle = strTaskTitle;
            oMainPage.strDesc = strDescription;

            oMainPage.strStartDate = strStartDate;
            oMainPage.strStartTime = strStartTime;
            oMainPage.strDueDate = strDueDate;
            oMainPage.strDueTime = strDueTime;
            oMainPage.strCompDate = strCompDate;
            oMainPage.strCompTime = strCompTime;

            oMainPage.strPriority = strPriorityId;
            oMainPage.strCategory = strCategoryId;              
            oMainPage.strIsCompleted = strIsCompleted;
            oMainPage.strStatus = strStatus;
            oMainPage.strIsDeleted = strIsDeleted;

            oMainPage.strRemark = strRemark;                
            oMainPage.strRepeatId = strRepeatId;
            oMainPage.strAlertCount = strAlertCount;                
            oMainPage.taskType = strTaskType;

            oMainPage.totalTask = strTotalTask;
            oMainPage.completedTask = strCompletedTask;

            [tempArray addObject:oMainPage];
            [oMainPage release];
        }
    }
    sqlite3_finalize(statement);
} else {
    NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(appDelegate.database));
}

[oClsDBFunction release];
return tempArray;
}

干杯。