1

xcode で .xib ファイルを使用してフォームを作成し、データベース テーブルから値を挿入および取得しようとしています。sqlite マネージャーを使用してデータベースとテーブルを作成しました。今、私のことは、xcodeチュートリアルに従って、必要なものを取り入れようとしていますが、機能していません

これはviewcontroller.hの私のコードです

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

@interface ViewController : UIViewController
 {
       NSString        *databasePath;

   sqlite3 *healthrecords;
 }
 @property (strong, nonatomic) IBOutlet UITextField *name;

 @property (strong, nonatomic) IBOutlet UITextField *bloodpressure;
 @property (strong, nonatomic) IBOutlet UITextField *heartrate;
 @property (strong, nonatomic) IBOutlet UITextField *sugarlevel;
 @property (strong, nonatomic) IBOutlet UILabel *status;
 - (IBAction)addinfo:(id)sender;
@end

これはviewcontroller.mの私のコードです

#import "ViewController.h"

@implementation ViewController
@synthesize name;
 @synthesize bloodpressure;
@synthesize heartrate;
 @synthesize sugarlevel;
 @synthesize status;

    - (void)didReceiveMemoryWarning
     {
     [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
   }

    #pragma mark - View lifecycle

     - (void)viewDidLoad
   {
      NSString *docsDir;
      NSArray *dirPaths;

     // Get the documents directory
     dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

     docsDir = [dirPaths objectAtIndex:0];

     // Build the path to the database file
     databasePath = [[NSString alloc] initWithString: [docsDir                                stringByAppendingPathComponent:      @"HealthForms.db"]];

NSFileManager *filemgr = [NSFileManager defaultManager];

if ([filemgr fileExistsAtPath: databasePath ] == NO)
{
    const char *dbpath = [databasePath UTF8String];

    if (sqlite3_open(dbpath, &healthrecords) == SQLITE_OK)
    {
        char *errMsg;
        const char *sql_stmt = 
        "CREATE TABLE IF NOT EXISTS HealthRecordsData (ID INTEGER PRIMARY KEY AUTOINCREMENT NOT  NULL UNIQUE, NAME CHAR NOT NULL, Blood Pressure FLOAT NOT NULL, Heart Rate FLOAT NOT NULL, Sugar Level FLOAT NOT NULL, t TIMESTAMP DEFAULT CURRENT_TIMESTAMP)";



        if (sqlite3_exec(healthrecords, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
        {
            status.text = @"Failed to create table";
        }

        sqlite3_close(healthrecords);

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


[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    }

         - (void)viewDidUnload
       {
         [self setName:nil];
          [self setBloodpressure:nil];
        [self setHeartrate:nil];
      [self setSugarlevel:nil];
     [self setStatus:nil];
    [super viewDidUnload];
      // Release any retained subviews of the main view.
     // e.g. self.myOutlet = nil;
  }

 - (void)viewWillAppear:(BOOL)animated
  {
     [super viewWillAppear:animated];
    }

   - (void)viewDidAppear:(BOOL)animated
    {
     [super viewDidAppear:animated];
    }

      - (void)viewWillDisappear:(BOOL)animated
       {
   [super viewWillDisappear:animated];
        }

       - (void)viewDidDisappear:(BOOL)animated
         {
       [super viewDidDisappear:animated];
            }

           - (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
    {
      // Return YES for supported orientations
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
           }

              - (IBAction)addinfo:(id)sender {
                  sqlite3_stmt    *statement;

              const char *dbpath = [databasePath UTF8String];

               if (sqlite3_open(dbpath, &healthrecords) == SQLITE_OK)
              {
                     NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO HealthRecordsData (name, Blood Pressure, Heart Rate, Sugar Level) VALUES (\"%@\", \"%@\", \"%@\",\"%@\")", name.text,     bloodpressure.text, heartrate.text,sugarlevel.text];

            const char *insert_stmt = [insertSQL UTF8String];

               sqlite3_prepare_v2(healthrecords, insert_stmt, -1, &statement, NULL);
               if (sqlite3_step(statement) == SQLITE_DONE)
               {
               status.text = @"info added";
                   name.text = @"";
                    bloodpressure.text = @"";
                    heartrate.text = @"";
                  sugarlevel.text = @"";
                   } else {
                 status.text = @"Failed to add contact";
                }
               sqlite3_finalize(statement);
               sqlite3_close(healthrecords);
               }
              }
              @end

問題は、ビルドが成功し、アプリがシミュレーターに読み込まれることですが、挿入ボタンを押しても何も起こりません。私は混乱しており、この時点で長い間立ち往生しています。助けを求めるのは素晴らしいことです。DB名、テーブルを確認し、xibファイルをviewcontroller.hに正しく接続しました。助けてくださいありがとう

4

0 に答える 0