这是我想要做的事情,我在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正确连接。请帮助谢谢