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

これを試して。

第 1 の方法。

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
    }
}

2番目の方法。

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 に答える