在查询中传递变量不会显示任何结果

时间:2013-07-30 05:25:57

标签: iphone ios sqlite

我在sqlite中有表格我想获取userNAme和organizatiocode的数据。问题是它没有显示任何行。如果我不传递变量并选择所有数据,则返回行。

这是我的代码

+(void)getInitialData:(NSString *)dbPath   {
    MultipleDetailViewsWithNavigatorAppDelegate *appDelegate =  (MultipleDetailViewsWithNavigatorAppDelegate *)[[UIApplication sharedApplication] delegate];

    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)  {
        NSLog(@"User NAme is %@",appDelegate.userName);
        // const char *sql = "select * from library";
        const char *sql = "select * from library where userName=?";

        sqlite3_stmt *selectstmt;
        if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
            //sqlite3_bind_text(selectStmt, 1, [appDelegate.userName  UTF8String], -1, SQLITE_TRANSIENT);

            //sqlite3_bind_text(selectStmt, 1, [appDelegate.organizationCode  UTF8String], -1, SQLITE_TRANSIENT);

            sqlite3_bind_text(selectStmt , 1, [appDelegate.userName UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(selectStmt , 2, [appDelegate.organizationCode UTF8String], -1, SQLITE_TRANSIENT);

            while(sqlite3_step(selectstmt) == SQLITE_ROW) {
                NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
                Coffee *coffeeObj = [[Coffee alloc] initWithPrimaryKey:primaryKey];

                coffeeObj.userID = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
                coffeeObj.contentAddedDateTime = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,2)];

                coffeeObj.contentType = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,3)];

                coffeeObj.contentTitle = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,4)];

                coffeeObj.contentSource = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,5)];

                coffeeObj.contentDescription = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,6)];

                coffeeObj.categoryTitle = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,7)];

                coffeeObj.subCategoryTitle = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,8)];

                coffeeObj.organizationCode = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,9)];

                coffeeObj.userName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,10)];

                int count=[appDelegate.libraryArrayLocal count];
                NSLog(@"count is beofore getting values %d",count);
                [appDelegate.libraryArrayLocal addObject:coffeeObj];
                int countone=[appDelegate.libraryArrayLocal count];
                NSLog(@"count is after getting values %d",countone);
                [coffeeObj release];
            }
        }
    }
    else
        sqlite3_close(database);
    }
}

1 个答案:

答案 0 :(得分:-1)

尝试使用:

NSString *query = [NSString stringWithFormat:@"select * from library where userName=%@",appDelegate.userName];
const char *sql = [query UTF8String];

而不是:

 const char *sql = "select * from library where userName=?";

也可以在没有约束的情况下尝试,

if(sqlite3_prepare_v2(database, sql, -1, &compiledStatement, NULL) == SQLITE_OK)
            {
                // Loop through the results and add them to the feeds array
                while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
                   //do your stuff here
                }
            }
    // Release the compiled statement from memory
    sqlite3_finalize(compiledStatement);