SQLite没有提取数据

时间:2011-07-18 22:51:27

标签: iphone objective-c xcode

我正在尝试从SQLite文件中提取数据,我创建了许多基于视图的应用程序,只有一个视图来测试此代码,并且每次运行都很完美。

但是有一些原因,当有多个视图不能完全运行或发出任何错误时,代码是:

TorD_Questions.h

    #import <UIKit/UIKit.h>
    #import <sqlite3.h>

@interface TorD_Questions : UIViewController 
{

    sqlite3         *database;

}

- (void) MakeDBCopy;
- (void) GetQuestion;


@end

TorD_Questions.m

#import "I_Dare_YouViewController.h"
#import "About_Page.h"
#import "Settings_Page.h"
#import "Game_Info.h"
#import "Game_TDoR.h"
#import "Settings_Menu.h"
#import "TorD_Questions.h"


@implementation TorD_Questions

- (void)viewDidLoad 
{
    [super viewDidLoad];
    [self MakeDBCopy];
    [self GetQuestion];
}

NSString *fileName = @"Questions.sqlite";


- (void) MakeDBCopy
{
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:fileName];
    success = [fileManager fileExistsAtPath:writableDBPath];
    if (success) 
    {
        return;
    }
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
    if (!success) 
    {
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }
}

- (void) GetQuestion
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:fileName];
    if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) 
    {

        const char *sql = "select * from Truth";

        sqlite3_stmt *searchStatement;

        if (sqlite3_prepare_v2(database, sql, -1, &searchStatement, NULL) == SQLITE_OK) 
        {
            while (sqlite3_step(searchStatement) == SQLITE_ROW) 
            {
                NSString *TheID = [NSString stringWithUTF8String:(char *)sqlite3_column_text(searchStatement, 0)];
                NSString *TheQuestion = [NSString stringWithUTF8String:(char *)sqlite3_column_text(searchStatement, 1)];
                NSLog(@"ID: %@ Question: %@", TheID, TheQuestion);
            }
        }
        sqlite3_finalize(searchStatement);
    }
}

@end

如果有人知道sqlite和多个视图的问题,因为这是我能想到的唯一不同的东西,在这个时代我只用一个视图测试它。

提前谢谢你。 利

1 个答案:

答案 0 :(得分:0)

问题解决了,我通过重命名SQlite数据库修复了它。