[英]Use of undeclared identifier 'sqlStatement'
嗨,我是iOS开发的新手,我在这里使用教程的代码: http : //hubpages.com/hub/IOS-5-How-To-Display-SQLite-Data-in-a-UITableView #但我正在将其更改为应该运行的方式。 但是即使我和本教程使用了相同的代码,也有些错误。 这是教程中的代码:
-(NSMutableArray *) authorList{
theauthors = [[NSMutableArray alloc] initWithCapacity:10];
@try {
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"AuthorsDb.sqlite"];
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if(!success)
{
NSLog(@"Cannot locate database file '%@'.", dbPath);
}
if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
{
NSLog(@"An error has occured: %@", sqlite3_errmsg(db));
}
const char *sql = "SELECT * FROM books";
sqlite3_stmt *sqlStatement;
if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
{
NSLog(@"Problem with prepare statement: %@", sqlite3_errmsg(db));
}else{
while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
Author * author = [[Author alloc] init];
author.name = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,1)];
author.title = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,2)];
author.genre = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 3)];
[theauthors addObject:author];
}
}
}
@catch (NSException *exception) {
NSLog(@"Problem with prepare statement: %@", sqlite3_errmsg(db));
}
@finally {
sqlite3_finalize(sqlStatement);
sqlite3_close(db);
return theauthors;
}
}
这是我的代码:
-(NSMutableArray *) Servicelist{
theservices = [[NSMutableArray alloc] initWithCapacity:10];
@try {
// CHANGE THE NAME OF THE DATABASE
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"DBManicurious.sqlite"];
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if(!success)
{
NSLog(@"Cannot locate database file '%@'.", dbPath);
}
if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
{
NSLog(@"An error has occured: %@", sqlite3_errmsg(db));
}
//ALTER THE SELECTION
const char *sql = "SELECT ServiceName, Price FROM tbl_Services WHERE TitleID=1"; //the Table view will be populated by the type ID
sqlite3_stmt *sqlStatement;
if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
{
NSLog(@"Problem with prepare statement: %@", sqlite3_errmsg(db));
}else{
while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
//This one must be altered .. :)
/*must recode this as soon as you get this -->>*/
services1 *services = [[services1 alloc] init];
services.title = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,0)];
// This one is an integer so find a way and recode this as you get this
services.price = sqlite3_column_int(sqlStatement, 1);
// author.genre = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 3)];
[theservices addObject:services];
}
}
}
@catch (NSException *exception) {
NSLog(@"Problem with prepare statement: %@", sqlite3_errmsg(db));
}
@finally {
sqlite3_finalize(sqlStatement);
sqlite3_close(db);
return theservices;
}
}
我在“ sqlite3_finalize(sqlStatement);”中遇到错误。 它说:使用未声明的标识符“ sqlStatement”
您已经声明
sqlite3_stmt *sqlStatement;
在@try
块中,因此它的作用域在那里。 将声明@try
之外,作为奖励,将其初始化为零,例如
sqlite3_stmt *sqlStatement = 0;
@try { ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.