1

プロジェクトでsqliteデータベースmathFactsを使用しています。これは、Sqlite Database Browserで作成し、Xcodeに追加します。以下はView Controllerクラスからの私のコードです

- (void)viewDidLoad
{   [super viewDidLoad];

    [self copyDatabaseIfNeeded];
    [self getInitialDataToDisplay:[self getDBPath]];
}


- (void) copyDatabaseIfNeeded {
    //Using NSFileManager we can perform many file system operations.
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;

    NSString *dbPth = [self getDBPath];
    BOOL success = [fileManager fileExistsAtPath:dbPth];

    if(!success) {

        NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"mathFActs"];
        success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

        if (!success)
            NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }
}
- (NSString *) getDBPath {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    return [documentsDir stringByAppendingPathComponent:@"mathFActs"];

}

-(void) getInitialDataToDisplay:(NSString *)databasePath{

    if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
      NSLog(@"open");
    const char *sql = "select Question  from math ";
        sqlite3_stmt *selectstmt;
        if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
            NSLog(@"prepare");
            while(sqlite3_step(selectstmt) == SQLITE_ROW) {

                NSString *addressField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(selectstmt, 0)];
                //address.text = addressField;

                qstn.text=addressField;         
               sqlite3_finalize(selectstmt); 

            }}
        else
            sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
    }

}

プロジェクトを実行すると、開いて印刷されますが、準備されていないということは、クエリステートメントを実行していないことを意味します..私の問題を解決するのを手伝ってください.

4

1 に答える 1

2

これを試して :-

-(void) getInitialDataToDisplay:(NSString *)databasePath
{
    if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
     {
      NSLog(@"open");
      const char *sql = "select Question  from math ";
      sqlite3_stmt *selectstmt;
      if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
            NSLog(@"prepare");
            while(sqlite3_step(selectstmt) == SQLITE_ROW) {

                NSString *addressField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(selectstmt, 0)];
                //address.text = addressField;

                qstn.text=addressField;         


            }
            sqlite3_reset(selectstmt);
       }
       else
       {
          NSLog(@"Error: failed to select details with message '%s'.", sqlite3_errmsg(database));
       }
       sqlite3_finalize(selectstmt);
       sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
    }

}

編集 :-

-(void)copyDatabaseIfNeeded
{
    @try
    {
        NSFileManager *fmgr=[NSFileManager defaultManager];
        NSError *error;
        NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
        NSString *path=[paths objectAtIndex:0];
        dbPath=[path stringByAppendingPathComponent:@"mathFActs.sqlite"];
        if(![fmgr fileExistsAtPath:dbPath]){
            NSString *defaultDBPath=[[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@"Addict.sqlite"];
            if(![fmgr copyItemAtPath:defaultDBPath toPath:dbPath error:&error])
                NSLog(@"failure message----%@",[error localizedDescription]);
        }

    }
    @catch (NSException *exception)
    {
        NSLog(@"Exception: %@", exception);
    }

}

-(NSString *)getDBPath
{
    //Search for standard documents using NSSearchPathForDirectoriesInDomains
    //First Param = Searching the documents directory
    //Second Param = Searching the Users directory and not the System
    //Expand any tildes and identify home directories.
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    return [documentsDir stringByAppendingPathComponent:@"mathFActs.sqlite"];
}

-(void)openDatabase
{
    [self copyDatabaseIfNeeded];

    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
    {
        //Database Opened
        NSLog(@"Database opened");
    }
    else
    {
        NSLog(@"Database cannot be opened");
    }
}

-(void)closeDatabase
{
    sqlite3_close(database);
}

それがあなたを助けることを願っています

于 2013-01-05T08:35:18.263 に答える