gpt4 book ai didi

ios - 如何在 iPhone 应用程序中获取数据时在 sqlite 查询中传递变量

转载 作者:行者123 更新时间:2023-12-03 00:55:53 25 4
gpt4 key购买 nike

我正在从 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.
}

最佳答案

你可以这样做,

假设您已声明 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;

它被称为将值绑定(bind)到准备好的语句。 Check here了解更多详细信息。

编辑 - 请参阅下面编辑的函数。

+(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);
}

关于ios - 如何在 iPhone 应用程序中获取数据时在 sqlite 查询中传递变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16051494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com