2013-07-30 35 views
0

我有表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); 
    } 
} 
+0

看到这个答案http://stackoverflow.com/questions/2381555/how-to-write-the-query-for-retrieving-id-from-sqlite-database-table-in-iphone-ap –

+0

@ParashJoshi我看到这但没有与我的案件 – user2240329

回答

-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); 
+0

不工作使用这也 – user2240329

+0

为什么downvoted?和@ user2240329是这个解决方案为你工作? – NightFury