IOS中数据持久化的方法之一就是利用数据库来实现了,而且使用起来很便捷,尤其是当程序中需要对数据进行筛选查询的时候使用起来尤其的方便。下面我就为大家分享一个程序就是利用数据库知识在数据库中新建一个学生信息表。,首先在Xcode5里面新建一个单视图工程,然后快速拖拽控件,并进行必要的关联。
如下图所示:
另外还要给大家分享一个数据库管理工具sqlite manager
另外还要在工程中导入框架,如下图所示:
白色的那个。
具体的代码如下:
HHLAppDelegate.h和HHLAppDelegate.m没有做任何变化
HHLViewVController.h
#import <UIKit/UIKit.h> #import <sqlite3.h> @interface HHLViewController : UIViewController { sqlite3 *_dataBase; } @property (strong,nonatomic) IBOutlet UITextField *nameField; @property (strong,nonatomic) IBOutlet UITextField *ageField; @property (strong,nonatomic) IBOutlet UITextField *adField; @property (strong,nonatomic) IBOutlet UITextField *sexField; @property (strong,nonatomic) IBOutlet UITextView *myTextView; - (IBAction)saveBtn:(id)sender; - (IBAction)readBtn:(id)sender; //接口说明 //返回值:数据库创建是否成功 // - (BOOL)initDatabase:(NSString *)dbName; //创建表 - (BOOL) createChannelsTable; //接口说明: //返回值:插入数据是否成功 - (BOOL)insertOneChannel:(NSString *)name cage:(int)age caddress:(NSString *)addr csex:(NSString *)sex;//接口说明: //返回值:可变数组(数组内存放多个字典,每个字典存储表中一行的数据) - (NSMutableArray *)getAllDatas; @end
HHLViewController.m
#import "HHLViewController.h" @interface HHLViewController () @end @implementation HHLViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view,typically from a nib. [self initDatabase:@"hanhailong.db"]; self.myTextView.editable = NO; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)saveBtn:(id)sender { NSString *pName = self.nameField.text; NSInteger nAge = [self.ageField.text intValue]; NSString *pAddr = self.adField.text; NSString *pSex = self.sexField.text; //插入数据 [self insertOneChannel:pName cage:nAge caddress:pAddr csex:pSex]; self.nameField.text = @""; self.ageField.text = @""; self.adField.text = @""; self.sexField.text = @""; } - (IBAction)readBtn:(id)sender { NSMutableString *pResultStr = [NSMutableString string]; NSMutableArray *pArr = [self getAllDatas]; for (int i = 0; i < [pArr count]; i++) { //每一行对应的字典 NSMutableDictionary *pTempDic = [pArr objectAtIndex:i]; [pResultStr appendFormat:@"%@",[pTempDic objectForKey:@"name"]]; [pResultStr appendString:@"\t"]; [pResultStr appendFormat:@"%@",[NSString stringWithFormat:@"%@",[pTempDic objectForKey:@"age"]]]; [pResultStr appendString:@"\t"]; [pResultStr appendFormat:@"%@",[pTempDic objectForKey:@"add"]]; [pResultStr appendString:@"\t"]; [pResultStr appendFormat:@"%@",[pTempDic objectForKey:@"sex"]]; [pResultStr appendString:@"\n"]; } self.myTextView.text = pResultStr; } - (BOOL)initDatabase:(NSString *)dbName { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString *filePath = [paths objectAtIndex:0]; NSLog(@"%@",filePath); NSString *path = [filePath stringByAppendingPathComponent:dbName]; if (sqlite3_open([path UTF8String],&_dataBase) != sqlITE_OK) { NSAssert(NO,@"打开数据库失败"); return NO; } else{ NSLog(@"打开数据库成功"); //创建表 // [self createChannelsTable]; return YES; } } - (BOOL)createChannelsTable { char *sql = "CREATE TABLE IF NOT EXISTS member(id integer primary key autoincrement,name text,age integer,addr text,sex text)"; sqlite3_stmt *stmt; if(sqlite3_prepare_v2(_dataBase,sql,-1,&stmt,nil)!=sqlITE_OK) { NSLog(@"编译失败"); return NO; } NSLog(@"创建表的sql语句执行成功"); int success = sqlite3_step(stmt); if (success != sqlITE_DONE) { NSLog(@"创建表失败"); return NO; } NSLog(@"创建成功"); return YES; } - (BOOL)insertOneChannel:(NSString *)name cage:(int)age caddress:(NSString *)addr csex:(NSString *)sex { sqlite3_stmt *stmt; //插入数据sql语句 char *sql = "INSERT INTO member(name,age,addr,sex) VALUES(?,?,?)"; int success = sqlite3_prepare_v2(_dataBase,nil); if (success != sqlITE_OK) { NSLog(@"插入数据的sql语句编译失败"); return NO; } NSLog(@"插入数据的sql语句编译成功"); // sqlite3_bind_text(stmt,1,[name UTF8String],nil); sqlite3_bind_int(stmt,2,age); sqlite3_bind_text(stmt,3,[addr UTF8String],nil); sqlite3_bind_text(stmt,4,[sex UTF8String],nil); //执行sql语句 success = sqlite3_step(stmt); //释放句柄 sqlite3_finalize(stmt); if (success == sqlITE_ERROR) { NSLog(@"数据加载失败"); return NO; } NSLog(@"插入数据成功"); return YES; } - (NSMutableArray *)getAllDatas { //可变数组,存放表种数据 NSMutableArray *allDatas = [[[NSMutableArray alloc]init]autorelease]; sqlite3_stmt *stmt= nil; char *sql = "SELECT *FROM member"; if (sqlite3_prepare_v2(_dataBase,nil) !=sqlITE_OK) { NSLog(@"读取表中数据sql语句编译失败"); return nil; } NSLog(@"读取表中数据sql语句编译成功"); while (sqlite3_step(stmt) == sqlITE_ROW) { NSMutableDictionary *pTempDic = [[NSMutableDictionary alloc]init]; //获取第一列数据 char *pName = (char *)sqlite3_column_text(stmt,1); //容错保护,如果获取到的数据为空,就不添加到字典中 if (pName) { [pTempDic setObject:[NSString stringWithUTF8String:pName] forKey:@"name"]; } //获取第二列数据 NSInteger nAge = (int)sqlite3_column_int(stmt,2); if (nAge) { [pTempDic setObject:[NSNumber numberWithInt:nAge] forKey:@"age"]; } //第三列数据 char *pAddr = (char *)sqlite3_column_text(stmt,3); if (pAddr) { [pTempDic setObject:[NSString stringWithUTF8String:pAddr] forKey:@"add"]; } //第四列数据 char *pSex = (char *)sqlite3_column_text(stmt,4); if (pSex) { [pTempDic setObject:[NSString stringWithUTF8String:pSex] forKey:@"sex"]; } //将存放每行数据的字典加入到数组中 [allDatas addObject:pTempDic]; [pTempDic release]; } sqlite3_finalize(stmt); return allDatas; } //弹回键盘的方法 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.view endEditing:YES]; } @end
运行后的效果如下图所示: