您好我是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'
答案 0 :(得分:1)
您已宣布
sqlite3_stmt *sqlStatement;
在@try
块内,所以它限定在那里。将声明移到@try
之外并作为奖励,将其初始化为零,例如
sqlite3_stmt *sqlStatement = 0;
@try { ...