0

「sqlite3_open」を使用すると、sqlite ファイルの代わりに新しいデータベースが開かれることを知っています。「sqlite3_open_v2」を使用して修正しました。しかし、今でもファイルからデータを取得できません。誰かがそれをもたらす方法を教えてもらえますか? どうもありがとう!

ここに私のコードがあります:

(void)viewDidLoad
{
[super viewDidLoad];
databaseName=@"freeway.sqlite";

[databasePath stringByExpandingTildeInPath];
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:@"freeway.sqlite"];

[self checkAndCreateDatabase];
[self readDataFromDatabase];
}

-

(void)checkAndCreateDatabase
{

BOOL success;
NSFileManager *fileManager=[NSFileManager defaultManager];
success=[fileManager fileExistsAtPath:databasePath];
if (success) 
{  NSLog(@"success");
return;
}
else {
    NSLog(@"fail");
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];       
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];       
}
}

-

(void)readDataFromDatabase
sqlite3 *database;
fee = [[NSMutableArray alloc]init];

//if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
    if (sqlite3_open_v2([databasePath  UTF8String], &database, SQLITE_OPEN_READWRITE, NULL) == SQLITE_OK)       
    sqlite3_stmt *stmt;
    char *sql = "SELECT * FROM fee";     

    if (sqlite3_prepare_v2(database,sql,-1,&stmt,NULL) == SQLITE_OK){
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            NSInteger pID = sqlite3_column_int(stmt, 0);
            NSString *pName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, 1)];
            NSString *pName2 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, 2)];

            Fee_Station *temp = [[Fee_Station alloc] initwithFCID:pID F_Name:pName Fmaileage:pName2];
            [fee addObject:temp];          
    }
    }
    else {
        NSLog(@"%s Prepare failure '%s' (%1d)", __FUNCTION__, sqlite3_errmsg(database), sqlite3_errcode(database));
    }     
    sqlite3_finalize(stmt);
}
sqlite3_close(database);
}
4

1 に答える 1

0

Application didFinishLaunchingWithOptions メソッド内の checkAndCreateDatabase データベース関数呼び出し、

その後

-(void) checkAndCreateDatabase
{

    BOOL success;


    NSFileManager *fileManager = [NSFileManager defaultManager];

    success = [fileManager fileExistsAtPath:databasePath];

    if(success) return;
    else
        printf("NO File found");

    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];

    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];

    //[fileManager release];
}

-(void)readDataFromDatabase
{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"freeway.sqlite"];
    sqlite3_stmt *compiledStatement;
    sqlite3 *database;
    if(sqlite3_open([path UTF8String], &database) == SQLITE_OK) {

        const char *sqlStatement;

        sqlStatement = "SELECT * FROM fee"; 

        sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL);

        printf("\nError===%s",sqlite3_errmsg(database));


        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {


            while(sqlite3_step(compiledStatement) == SQLITE_ROW ) 

            {

                NSString *str_field1=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];

                NSString *str_field2=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];

                NSString *str_field3=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];

                NSString *str_field4=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];


                // add str_field into array


            }

        }

        sqlite3_finalize(compiledStatement);


    }


    sqlite3_close(database);

}
于 2012-07-02T07:01:15.803 に答える