我的文档文件夹中有一个sqlite文件(我已经打开了,它就在那里)。问题是openDatabase函数总是打印Yes,即使我给它错误的路径,所以这很奇怪。 第二件事是
if (sqlite3_prepare_v2(localDB,query_stmt, -1, &statement, NULL) == SQLITE_OK)
它总是转到else并打印“SQLITE_OK上的错误”。你能帮我解决一下我的代码:
@interface SQLReader ()
{
sqlite3 *localDB;
}
@end
@implementation SQLReader
- (void)viewDidLoad {
[super viewDidLoad];
[self openDatabase];
sqlite3_stmt *statement;
NSString *querySQL = @"SELECT * FROM Animations";
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(localDB,query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *addressField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
NSString *phoneField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
NSLog(@"%@",addressField);
NSLog(@"%@",phoneField);
}
else
{
NSLog(@"%s","Match not found");
}
sqlite3_finalize(statement);
}
else
{
NSLog(@"%s","Error on SQLITE_OK");
}
}
- (void)openDatabase
{
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
char *dbpath = (char *)[[documentsPath stringByAppendingPathComponent: @"Bemad/IR_World/DB/IRRMobile.sqlite"] UTF8String];
BOOL openDatabaseResult = sqlite3_open(dbpath, &localDB);
NSLog(openDatabaseResult ? @"DB: Yes" : @"DB: No");
}