1

我从 sqlite 表中获取数据,我想传递变量,以便如果 sub_Catgeory 等于该变量,那么它应该获取记录。

  const char *sql = "select * from category where sub_Category=appDelegate.sub_Category";

同样,如果不使用 appDelegate.Sub_Category 变量,那么它工作正常

  const char *sql = "select * from category where sub_Category='Glucagon'";

我的完整代码

   + (void) getInitialData:(NSString *)dbPath {

     MultipleDetailViewsWithNavigatorAppDelegate *appDelegate = (MultipleDetailViewsWithNavigatorAppDelegate *)[[UIApplication sharedApplication] delegate];

     if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {

        //const char *sql = "select * from category where sub_Category='Glucagon'";

       NSLog(@"Sub Category is %@",appDelegate.subCategory);

       const char *sql = "select * from category where sub_Category='appDelegate. subcategory'";

       sqlite3_stmt *selectstmt;
       if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK {

         while(sqlite3_step(selectstmt) == SQLITE_ROW) {

            NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
            Coffee *coffeeObj = [[Coffee alloc] initWithPrimaryKey:primaryKey];
            coffeeObj.category=sqlite3_column_int(selectstmt,1);
            coffeeObj.sub_Category = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)];
            coffeeObj.content_Type = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 3)];
            coffeeObj.content_Title = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,4)];

            coffeeObj.publisher = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 5)];
            coffeeObj.content_Description = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 6)];
            coffeeObj.content_Id = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 7)];
            coffeeObj.content_Source = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 8)];

            int count=[appDelegate.arrayOneC count];

            NSLog(@"count is beofore getting values %d",count);

            [appDelegate.arrayOneC addObject:coffeeObj];

            int countone=[appDelegate.arrayOneC count];

            NSLog(@"count is after getting values %d",countone);

            [coffeeObj release];
        }
    }
}
else
    sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
}
4

3 回答 3

7

你可以这样做,

假设你已经声明static sqlite3_stmt *selectStmt = nil;了一些地方

const char *sqlStatement = "select * from category where sub_Category=?";

if(sqlite3_prepare_v2(database, sqlStatement, -1, &selectStmt, NULL) == SQLITE_OK) 
{
    sqlite3_bind_text(selectStmt, 1, [appDelegate.sub_Category  UTF8String], -1, SQLITE_TRANSIENT);
    while(sqlite3_step(selectStmt) == SQLITE_ROW) 
    {
            //Process data
    }
}

//Reset the select statement.
sqlite3_reset(selectStmt);
selectStmt = nil;

它被称为绑定值到准备好的语句。在这里查看更多详细信息。

编辑——见下面你编辑的函数。

+(void)getInitialData:(NSString *)dbPath
{
    MultipleDetailViewsWithNavigatorAppDelegate *appDelegate = (MultipleDetailViewsWithNavigatorAppDelegate *)[[UIApplication sharedApplication] delegate];
    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
    {
        NSLog(@"Sub Category is %@",appDelegate.subCategory);
        const char *sql = "select * from category where sub_Category=?";
        sqlite3_stmt *selectstmt;
        if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK)
        {
            sqlite3_bind_text(selectStmt, 1, [appDelegate.sub_Category  UTF8String], -1, SQLITE_TRANSIENT);
            while(sqlite3_step(selectstmt) == SQLITE_ROW)
            {
                NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
                Coffee *coffeeObj = [[Coffee alloc] initWithPrimaryKey:primaryKey];
                coffeeObj.category=sqlite3_column_int(selectstmt,1);
                coffeeObj.sub_Category = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)];
                coffeeObj.content_Type = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 3)];
                coffeeObj.content_Title = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,4)];
                coffeeObj.publisher = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 5)];
                coffeeObj.content_Description = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 6)];
                coffeeObj.content_Id = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 7)];
                coffeeObj.content_Source = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 8)];
                int count=[appDelegate.arrayOneC count];
                NSLog(@"count is beofore getting values %d",count);
                [appDelegate.arrayOneC addObject:coffeeObj];
                int countone=[appDelegate.arrayOneC count];
                NSLog(@"count is after getting values %d",countone);
                [coffeeObj release];
            }
        }
    }
    else
        sqlite3_close(database);
}
于 2013-04-17T04:32:14.233 回答
6

尝试这个。

第一种方式。

NSString *queryString = [NSString stringWithFormat:@"select * from category where sub_Category='%@'", appDelegate.subCategory];
const char *sqlQuery = [queryString UTF8String];

     if(sqlite3_prepare_v2(database, sqlQuery, -1, &selectStatement, NULL) == SQLITE_OK){
                while (sqlite3_step(selectStatement) == SQLITE_ROW){
    //Fetching your data logic
    }
}

第二种方式。

const char *sqlStatement = "select * from category where sub_Category=?";

if(sqlite3_prepare_v2(database, sqlStatement, -1, &selectStmt, NULL) == SQLITE_OK) 
{
    sqlite3_bind_text(selectStmt, 1, [appDelegate.subCategory  UTF8String], -1, SQLITE_TRANSIENT);
    while(sqlite3_step(selectStmt) == SQLITE_ROW) 
    {
            //Process data
    }
}

感谢 HarshIT、rmaddy 和 Jennis。

于 2013-04-17T04:33:06.753 回答
3

尝试这个 ::

NSString *selectSQL = [NSString stringWithFormat:@"select * from category where sub_Category = \"%@\"", appDelegate.subCategory];

const char *sql = [selectSQL UTF8String];
sqlite3_stmt *searchStatement;

if (sqlite3_prepare_v2(dbName, sql, -1, &searchStatement, NULL) == SQLITE_OK)
{
    while (sqlite3_step(searchStatement) == SQLITE_ROW)
    {

    }
}
sqlite3_finalize(searchStatement);

希望,它会帮助你。

谢谢。

于 2013-04-17T04:33:32.243 回答