Jde*_*one 1 sql sqlite objective-c ios
我从sqlite表获取数据,我想传递变量,以便如果sub_Catgeory等于该变量,那么它应该获取记录.
const char *sql = "select * from category where sub_Category=appDelegate.sub_Category";
Run Code Online (Sandbox Code Playgroud)
同样如果不使用appDelegate.Sub_Category变量那么它工作正常
const char *sql = "select * from category where sub_Category='Glucagon'";
Run Code Online (Sandbox Code Playgroud)
我的完整代码
+ (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.
}
Run Code Online (Sandbox Code Playgroud)
你可以这样做,
假设你已经宣布了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;
Run Code Online (Sandbox Code Playgroud)
它被称为准备语句的绑定值.点击此处查看更多详细信息.
编辑 - 见下面编辑的功能.
+(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);
}
Run Code Online (Sandbox Code Playgroud)
试试这个.
第一路.
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
}
}
Run Code Online (Sandbox Code Playgroud)
第二种方式.
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
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢HarshIT,rmaddy和Jennis.
| 归档时间: |
|
| 查看次数: |
4543 次 |
| 最近记录: |