2015-04-01 52 views
0

我的问题是,我不能从选择查询中获取值,因为它执行101错误。SQLite查询语句 - 客观C

我认为我的问题是,它必须是在SELECT语句

的sqlite3 *数据库中的报价;

NSString * path = [[[ NSBundle mainBundle ]resourcePath ] stringByAppendingPathComponent : @"cars.sqlite"]; 
int returnCode = sqlite3_open ([ path cStringUsingEncoding : NSUTF8StringEncoding], &database) ; 

sqlite3_stmt * statement ; 
char * st ; 
NSString *querySQL = [NSString stringWithFormat: @"select brand,model from cars where brand=%@;", tmpbrand]; 

const char *query_stmt = [querySQL UTF8String]; 
st = sqlite3_mprintf (query_stmt); 

if(sqlite3_prepare_v2 (database , query_stmt , -1 ,& statement ,NULL)==SQLITE_OK){ 
returnCode = sqlite3_step (statement) ; // this statement returns a record 

while (returnCode == SQLITE_ROW) // while there are rows 
{ 
    models*tmp=[[models alloc]init]; 

    char* n=(char*)sqlite3_column_text(statement,0); 
    tmp=[NSString stringWithCString:n encoding:NSUTF8StringEncoding]; 
    char* model=(char*)sqlite3_column_text(statement, 1); 
    tmp.model=[NSString stringWithCString:model encoding:NSUTF8StringEncoding]; 


    [data addObject:tmp]; 

    returnCode = sqlite3_step (statement) ; // this statement returns a record 

} 
    sqlite3_finalize(statement); 
} 

}

回答

1

快速浏览一下the documentation显示错误101的意思是 “完成”。

SQLITE_DONE结果代码指示操作已完成。

如果你没有得到你期望的行,你是对的,这可能是你的查询。为了使它工作,你可能需要在'%@'附近放置单引号。

但是,这不是正确的做法。你应该使用准备好的语句。也就是说,您的查询看起来像select brand,model from cars where brand=?,并使用绑定函数填充值。

+0

我试图做到这一点与单引号,我couldnt设法使它的工作..如果有可能向我展示如何使用预处理语句来做到这一点..我相当一个begginer在这.. – 2015-04-02 11:45:23

+0

这是第一个链接在Google搜索“objective c sqlite prepared statement”:http://stackoverflow.com/q/1187154/2998 – 2015-04-02 12:04:08