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

问题是我构建成功并且我的应用程序加载到模拟器上但是当我按下插入按钮时没有任何反应。我很困惑并在这一点上停留了很长时间,现在获得一些帮助会很棒。我已经检查了数据库名称、表,并且我已经将我的 xib 文件与 viewcontroller.h 正确连接。请帮忙谢谢

4

0 回答 0