-1

値を挿入しようとしていますが、値が保存されません。これは私のコーディングです。SQLite Manager を使用してデータベースを作成しました。そのデータベース名は " feedback.sqlite" です。コードを実行してもエラーは表示されませんが、データを入力してから保存ボタンをクリックすると、データは保存されません。コードを実行すると、「データベースを開く/作成に失敗しました」というメッセージがシミュレーターに表示されます。エラーがどこにあるのか推測できません。アイデアを教えてください。事前に感謝します。

Ratingviewcontroller.h

@interface RatingViewController : UIViewController <UITextFieldDelegate> {

    sqlite3 *contactDB;

    IBOutlet UITextField *Traineeid;
    IBOutlet UITextField *Trainername;
    IBOutlet UITextField *Traineename;
    IBOutlet UITextField *Rating;
    IBOutlet UILabel *status;

    NSString *dbpath;
}

@property(nonatomic, retain) UITextField *Traineeid;
@property(nonatomic, retain) UITextField *Trainername;
@property(nonatomic, retain) UITextField *Traineename;
@property(nonatomic, retain) UITextField *Rating;
@property(nonatomic, retain) UILabel *status;

- (IBAction)saveData:(id)sender;
- (IBAction)findData:(id)sender;

@end

Ratingviewcontroller.m

@implementation RatingViewController
- (void)viewDidLoad {

    // Do any additional setup after loading the view, typically from a nib.

    NSString *docsDir;
    NSArray *dirPaths;

    // get the document directory

    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory,
                                                   NSUserDomainMask, YES);

    docsDir = [dirPaths objectAtIndex:0];

    // Build the path to the database file

    dbpath = [[NSString alloc] initWithString:
            [docsDir stringByAppendingPathComponent:@"feedback.sqlite"]];
    NSFileManager *filemgr = [NSFileManager defaultManager];

    if ([filemgr fileExistsAtPath:dbpath] == NO) {

        const char *db = [dbpath UTF8String];

        if (sqlite3_open(db, &contactDB) == SQLITE_OK) {

            char *errMsg;
            const char *sql_stmt =
                "CREATE TABLE IF NOT EXISTS CONTACTS (Traineeid INTEGER "
                "PRIMARY KEY AUTOINCREMENT, Trainername TEXT, Traineename "
                "TEXT, Rating float)";

            if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) !=
                SQLITE_OK) {

                status.text = @"Failed to create table";
            }

            sqlite3_close(contactDB);

        } else {

            status.text = @"Failed to open/create database";
        }
    }

    [super viewDidLoad];
}

- (void)saveData:(id)sender {

    sqlite3_stmt *statement;
    const char *database = [dbpath UTF8String];

    if (sqlite3_open(database, &contactDB) == SQLITE_OK) {

        NSString *insertSQL =
            [NSString stringWithFormat:
                    @"INSERT INTO CONTACTS (Trainee id, Trainer name, Trainee "
                     "name,Rating) VALUES (\"%@\",\"%@\", \"%@\", \"%@\")",
                Traineeid, Trainername.text, Trainername.text, Rating.text];

        /*  NSString *insertSQL = [NSString stringWithFormat:@"insert into
                                      CONTACTS
           (Traineeid,Trainername,Traineename,Rating) values
                                      (\"%d\",\"%@\", \"%@\",
           \"%f\")",[Traineeid integerValue],
                                       Trainername, Traineename,[Rating
           float]];*/

        const char *insert_stmt = [insertSQL UTF8String];
        sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);

        if (sqlite3_step(statement) == SQLITE_DONE) {

            status.text = @"Contact added";
            Traineeid.text = @"";
            Trainername.text = @"";
            Traineename.text = @"";
            Rating.text = @"";

        } else {

            status.text = @"Failed to add contact";
        }

        sqlite3_finalize(statement);

        sqlite3_close(contactDB);
    }
}

- (void)findContact {

    const char *datapath = [dbpath UTF8String];
    sqlite3_stmt *statement;

    if (sqlite3_open(datapath, &contactDB) == SQLITE_OK) {

        NSString *querySQL = [NSString stringWithFormat:
                                  @"select Trainer name,Trainee name,Rating "
                                   "from CONTACT where Traineeid=\"%@\"",
                              Traineeid];

        const char *query_stmt = [querySQL UTF8String];

        if (sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL) ==
            SQLITE_OK) {

            if (sqlite3_step(statement) == SQLITE_ROW) {

                NSString *trainid = [[NSString alloc] initWithUTF8String:
                        (const char *)sqlite3_column_text(statement, 0)];

                Traineeid.text = trainid;

                NSString *trainernme = [[NSString alloc] initWithUTF8String:
                        (const char *)sqlite3_column_text(statement, 1)];

                Trainername.text = trainernme;

                NSString *traineenme = [[NSString alloc] initWithUTF8String:
                        (const char *)sqlite3_column_text(statement, 2)];

                Traineename.text = traineenme;

                NSString *rat = [[NSString alloc] initWithUTF8String:
                        (const char *)sqlite3_column_text(statement, 3)];

                Rating.text = rat;
                status.text = @"Match found";

            } else {

                status.text = @"Match not found";
                Traineeid.text = @"";
                Trainername.text = @"";
                Traineename.text = @"";
                Rating.text = @"";
            }

            sqlite3_finalize(statement);
        }

        sqlite3_close(contactDB);
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {

    [textField resignFirstResponder];
    return YES;
}
@end
4

1 に答える 1