我正在尝试插入值。但是没有保存值。这是我的编码。我已经使用 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