朋友你好 我正在开发应用程序,其中视图有一个textField& 2个按钮。当用户单击“添加”时,文本字段中的文本应存储在datatabse中。当用户单击“显示”时,从数据库中提取的数据应显示在textField中。编写的代码就像这样
//SettingsApplicationViewController.h
#import "FMDatabase.h"
@interface SettingsApplicationViewController : UIViewController {
FMDatabase *db;
IBOutlet UITextField *urlField;
IBOutlet UIButton *insertButton;
IBOutlet UIButton *displayButton;
}
@property(nonatomic, retain) IBOutlet UITextField *urlField;
@property(nonatomic, retain) IBOutlet UIButton *insertButton;
@property(nonatomic, retain) IBOutlet UIButton *displayButton;
-(void)insertButtonClicked :(id)sender;
-(void)displayButtonClicked:(id)sender;
-(id)initApp;
@end
// SettingsApplicationViewController.m
#import "SettingsApplicationViewController.h"
#import "FMDatabase.h"
@implementation SettingsApplicationViewController
@synthesize urlField;
@synthesize insertButton;
@synthesize displayButton;
-(id)initApp
{
if(![super init])
return nil;
//DB stored in application bundle
NSString *DBName = @"SettingDB.sqlite";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingString:DBName];
db = [FMDatabase databaseWithPath:path];
[db setLogsErrors:TRUE];
[db setTraceExecution:TRUE];
if(![db open])
{
NSLog(@"Could not open Database");
return 0;
}
else
{
NSLog(@"Database opened successfully");
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self initApp];
}
-(void)insertButtonClicked :(id)sender
{
[db executeUpdate:@"insert into Settings values (?)",urlField.text];
[self resignFirstResponder];
}
-(void)displayButtonClicked:(id)sender
{
NSString *returnResult = [[NSString alloc]init];
FMResultSet *rs = [db executeQuery:@"select url from Settings"];
while ([rs next])
{
returnResult = [rs stringForColumn:@"url"];
[urlField setText:returnResult];
}
}
// SettingDB.sqlite
CREATE TABLE Settings(url text(50));
我在IB&复制了libsqlite3.dylib& FMDatabase文件。我不知道为什么“#”没有打印在这篇文章中,但我的代码中有#。
编译此代码时,显示如下错误
Command /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 failed with exit code 1
请帮帮我朋友。 感谢
答案 0 :(得分:1)
当您在Xcode中引用文件且该文件不在上述路径中时,通常会显示此错误。 (该文件将在Project Navigator中显示为红色)
答案 1 :(得分:0)
您是否为这些按钮分配了操作?
如果不这样做,
而不是-(void)insertButtonClicked :(id)sender;
使用-(IBAction)insertButtonClicked :(id)sender;
并将它们分配给相应的按钮操作。
或者,
在viewdidLoad中,
[insertButton addTarget:self action:@selector(insertButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[displayButton addTarget:self action:@selector(displaytButtonClicked:) forControlEvents:UIControlEventTouchUpInside];