3、在.h文件中
#import <UIKit/UIKit.h>
#import "sqlite3.h"
@interface studentViewController : UIViewController{
sqlite3 *dataBase;
UITextField *num;
UITextField *classname;
UITextField *name;
UILabel *status;
NSString *databasePath;
}
@property (strong, nonatomic) IBOutlet UITextField *num;
@property (strong, nonatomic) IBOutlet UITextField *classname;
@property (strong, nonatomic) IBOutlet UITextField *name;
@property (strong, nonatomic) IBOutlet UILabel *status;
- (IBAction)textFiledReturnEditing:(id)sender;
- (IBAction)backgroundTap:(id)sender;
@end
4、在.m文件中
#import "studentViewController.h"
@interface studentViewController ()
@end
@implementation studentViewController
@synthesize num;
@synthesize classname;
@synthesize name;
@synthesize status;
//创建数据库
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *docsDir;
NSArray *dirPaths;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
databasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent:@"info.db"]];
NSFileManager *filemanager = [NSFileManager defaultManager];
if ([filemanager fileExistsAtPath:databasePath] == NO) {
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &dataBase)==SQLITE_OK) {
char *errmsg;
const char *createsql = "CREATE TABLE IF NOT EXISTS INFO (ID INTEGER PRIMARY KEY AUTOINCREMENT, NUM TEXT, CLASSNAME TEXT,NAME TEXT)";
if (sqlite3_exec(dataBase, createsql, NULL, NULL, &errmsg)!=SQLITE_OK) {
status.text = @"create table failed.";
}
}
else {
status.text = @"create/open failed.";
}
}
}
//保存
- (IBAction)saveinfo:(id)sender {
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &dataBase)==SQLITE_OK) {
if ([num.text isEqualToString:@""]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"SORRY!" message:@"number cannot be nil!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
else {
NSString *insertSql = [NSString stringWithFormat:@"INSERT INTO INFO (num,classname,name) VALUES(\"%@\",\"%@\",\"%@\")",num.text,classname.text,name.text];
const char *insertsatement = [insertSql UTF8String];
sqlite3_prepare_v2(dataBase, insertsatement, -1, &statement, NULL);
if (sqlite3_step(statement)==SQLITE_DONE) {
status.text = @"save to DB.";
num.text = @"";
classname.text = @"";
name.text = @"";
}
else {
status.text = @"save failed!";
}
sqlite3_finalize(statement);
sqlite3_close(dataBase);
}
}
}
//查询
- (IBAction)searchResult:(id)sender {
const char *dbpath = [databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &dataBase)==SQLITE_OK) {
NSString *querySQL = [NSString stringWithFormat:@"SELECT classname,name from info where num=\"%@\"",num.text];
const char *querystatement = [querySQL UTF8String];
if (sqlite3_prepare_v2(dataBase, querystatement, -1, &statement, NULL)==SQLITE_OK) {
if (sqlite3_step(statement)==SQLITE_ROW) {
NSString *classnameField = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 0)];
classname.text = classnameField;
NSString *nameField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
name.text = nameField;
status.text = @"find~~~";
}
else {
status.text = @"did not find you need.";
}
sqlite3_finalize(statement);
}
sqlite3_close(dataBase);
}
}
//清空文本框
- (IBAction)clearTextField:(id)sender {
num.text = @"";
classname.text = @"";
name.text = @"";
status.text = @"";
}
//点击return收起键盘
-(IBAction)textFiledReturnEditing:(id)sender {
[sender resignFirstResponder];
}
//点击屏幕收起键盘
- (IBAction)backgroundTap:(id)sender {
[num resignFirstResponder];
[classname resignFirstResponder];
[name resignFirstResponder];
}
- (void)viewDidUnload
{
[self setNum:nil];
[self setClassname:nil];
[self setName:nil];
[self setStatus:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end