1

我正在尝试使用将数据存储在表中sqlite manager。但是这些值没有存储在数据库中。如果我单击保存按钮,错误消息将像这样显示Failed to open/create database。我不明白如何解决这个问题。任何人请给我一个如何解决这个问题的想法。在此先感谢。

DBManager.m

-(BOOL)createDB {
    NSString *docsDir;
    NSArray *dirPaths;

    // Get the document directory
    dirPaths=NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask,YES);
    docsDir=dirPaths[0];

    // Build the path to the database file
    databasePath=[[NSString alloc]initWithString:[docsDir stringByAppendingPathComponent:@"Feedback.db"]];
    BOOL isSuccess=YES;
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if([fileManager fileExistsAtPath:databasePath]==0) {
        const char *dbpath=[databasePath UTF8String];
        if(sqlite3_open(dbpath, &database)==SQLITE_OK) {
            char *errMsg;
            const char *sql_stmt= "create table if not exists Feeback details (Traineeid integer, Trainername text,Traineename text,Rating float)";
            if(sqlite3_exec(database, sql_stmt, NULL, NULL, &errMsg)!=SQLITE_OK) {
                isSuccess=NO;
                NSLog(@"Failed to create table");
            }
            sqlite3_close(database);
            return isSuccess;
        }
        else {
            isSuccess=NO;
            NSLog(@"Failed to open/Create database");
        }
    }
    return isSuccess;
}

// save data in the Database

-(BOOL) saveData:(NSString *)Traineeid Trainername:(NSString *)Trainername Traineename:(NSString *)Traineename Rating:(NSString *)Rating; {
    const char *dbpath=[databasePath UTF8String];
    if(sqlite3_open(dbpath, &database)==SQLITE_OK) {
        NSString *insertSQL=[NSString stringWithFormat:@"insert into Feedbackdetails(Traineeid,Trainername,Traineename,Rating) values(\"%d\",\"%@\", \"%@\", \"%@\")",[Traineeid integerValue],Trainername,Traineename,Rating];
        const char *insert_stmt=[insertSQL UTF8String];
        sqlite3_prepare_v2(database, insert_stmt, -1, &statement, NULL);
        if(sqlite3_step(statement)==SQLITE_DONE) {
            return YES;
        }       
        else {
            return NO;
        }
        sqlite3_reset(statement);
    }
    return NO;
}

Ratingviewcontroller.m

-(IBAction)saveData:(id)sender {
    BOOL success=NO;
    NSString *alertString = @"Data Insertion failed";
    if (Traineeid.text.length>0 &&Trainername.text.length>0 &&Traineename.text.length>0 &&Rating.text.length>0 ) {
        success=[[DBManager getSharedInstance]saveData:Traineeid.text Trainername:Trainername.text Traineename:Traineename.text Rating:Rating.text];
    }
    else {
        alertString = @"Enter all fields";
    }
    if (success == NO) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle: alertString message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }
}
4

1 回答 1

0

您使用的不是文档目录,而是文档目录。使用它NSDocumentsDirectory而不是NSDocumentationDirectory. 或者更好的是,使用缓存目录NSCachesDirectory,因为文档目录与 iCloud 同步,Apple 可能不喜欢它,因为 Documents 目录应该只包含用户下载的文件。

于 2013-07-10T08:11:45.493 回答