ios25-封装一个类---
2013-06-19 19:47
337 查看
1.student.h
//
// Student.h
// ios24-saveObjectToFile
//
// Created by on 13-6-18.
// Copyright 2013年 __MyCompanyName__. All rights reserved.
//
#import
<Foundation/Foundation.h>
//要实现当前类对象,就必须实现nscoding协议
@interface Student :
NSObject<NSCoding>
{
NSString *studentId;
NSString *studentName;
NSString *studentClass;
}
@property (nonatomic,retain)
NSString *studentId;
@property (nonatomic,retain)
NSString *studentName;
@property (nonatomic,retain)
NSString *studentClass;
@end
--------------------------------------------------
//
// Student.m
// ios24-saveObjectToFile
//
// Created by on 13-6-18.
// Copyright 2013年 __MyCompanyName__. All rights reserved.
//
#import
"Student.h"
@implementation Student
@synthesize studentId,studentName,studentClass;
@end
2.studentDao.h
//
// StudentDao.h
// ios24-saveObjectToSqList
//
// Created by on 13-6-18.
// Copyright 2013年 __MyCompanyName__. All rights reserved.
//
#import
<Foundation/Foundation.h>
#import
"Student.h"
#import
"sqlite3.h"
@interface StudentDao :
NSObject{
//用来存储查询的单条结果集
NSMutableDictionary *objDict;
//存储查询返回的集合
NSMutableArray *listData;
//student类
Student *stu;
//进行查询并且创建表
sqlite3 *database;
}
@property(nonatomic,retain)
NSMutableDictionary *objDict;
@property(nonatomic,retain)
NSMutableArray *listData;
@property(nonatomic,retain)
Student *stu;
-(BOOL) doAdd:(id) _obj;
-(BOOL) doDelete:(int ) _nid;
-(BOOL) doUpdate:(id) _obj;
-(NSMutableArray *)doFindALL:(NSString *)_condition;
-(NSMutableDictionary *)findById:(int)_nid;
-(NSString *)getFilePath;
@end
----------------------------------88888888*************
//
// StudentDao.m
// ios24-saveObjectToSqList
//
// Created by on 13-6-18.
// Copyright 2013年 __MyCompanyName__. All rights reserved.
//
#import
"StudentDao.h"
@implementation StudentDao
@synthesize listData,objDict,stu;
//添加
-(BOOL) doAdd:(id) _obj{
//把接收的参数转换为Student
Student *stu2 = (Student *)_obj;
//获取路径insertjiushihuijia kanjannijiushiyge
NSString *dbFilePath = [self
getFilePath];
if (sqlite3_open([dbFilePath
UTF8String], &database)!=SQLITE_OK) {
sqlite3_close(database);
return NO;
}else{
//定义sql
NSString *insertSql =
@"insert into student values(?,?,?);";
//定义预处理命令
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, [insertSql
UTF8String], -1, &statement,
NULL)==SQLITE_OK) {
//绑定参数
sqlite3_bind_int(statement, 1, [stu2.studentId
integerValue]);
sqlite3_bind_text(statement, 2, [stu2.studentName
UTF8String], -1,
NULL);
sqlite3_bind_text(statement, 3, [stu2.studentClass
UTF8String], -1,
NULL);
//执行,并判断是否成功
if (sqlite3_step(statement)!=SQLITE_DONE){
sqlite3_finalize(statement);
sqlite3_close(database);
return NO;
}
return YES;
}else{
//预编译失败,执行代码
sqlite3_finalize(statement);
sqlite3_close(database);
return NO;
}
}
return YES;
}
//删除
-(BOOL) doDelete:(int ) _nid{
//获取路径
NSString *dbFilePath=[self
getFilePath];
//1打开数据库
//2定义sql
//3进行预处理
//4执行sql
//5关闭数据库
if (sqlite3_open([dbFilePath
UTF8String], &database)!=SQLITE_OK) {
sqlite3_close(database);
return NO;
}else{
//定义sql
NSString *insertSql=@"delete from student where sid=?";
//定义预处理命令
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, [insertSql
UTF8String], -1, &statement,
NULL)==SQLITE_OK) {
NSString *xh=[NSString
stringWithFormat:@"%d",_nid];
//开始绑定参数
sqlite3_bind_text(statement, 1, [xh
UTF8String], -1,
NULL);
if (sqlite3_step(statement)!=SQLITE_DONE) {
//释放预处理对象资源
sqlite3_finalize(statement);
sqlite3_close(database);
return NO;
}else
{
sqlite3_finalize(statement);
sqlite3_close(database);
return YES;
}
}else{
sqlite3_finalize(statement);
sqlite3_finalize(statement);
sqlite3_close(database);
return NO;
}
}
return YES;
}
//更新
-(BOOL) doUpdate:(id) _obj{
stu=_obj;
//获取路径
NSString *dbFilePath=[self
getFilePath];
//1打开数据库
//2定义sql
//3进行预处理
//4执行sql
//5关闭数据库
if (sqlite3_open([dbFilePath
UTF8String], &database)!=SQLITE_OK) {
sqlite3_close(database);
return NO;
}else{
//定义sql
NSString *insertSql=@"update student set name=?,class=? where sid=?";
//定义预处理命令
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, [insertSql
UTF8String], -1, &statement,
NULL)==SQLITE_OK) {
//开始绑定参数
sqlite3_bind_text(statement, 1, [stu.studentName
UTF8String],-1,NULL);
sqlite3_bind_text(statement, 2, [stu.studentClass
UTF8String], -1,
NULL);
sqlite3_bind_int(statement, 3, [stu.studentId
integerValue]);
if (sqlite3_step(statement)!=SQLITE_DONE) {
//释放预处理对象资源
sqlite3_finalize(statement);
sqlite3_close(database);
return NO;
}else
{
sqlite3_finalize(statement);
sqlite3_close(database);
return YES;
}
}else{
sqlite3_finalize(statement);
sqlite3_finalize(statement);
sqlite3_close(database);
return NO;
}
}
return YES;
}
//查询返回结果集
-(NSMutableArray *)findAll{
//初始化数组
listData = [[NSMutableArray
alloc] initWithCapacity:1];
//设定路径
NSString *filePath = [self
getFilePath];
//1、打开数据库
if (sqlite3_open([filePath
UTF8String],&database)!=SQLITE_OK) {
sqlite3_close(database);
NSAssert(NO,@"打开数据库");
}else{
//预处理sql语句
NSString *selectSql = [NSString
stringWithFormat:@"select * from student"];
//绑定参数
sqlite3_stmt *statement;
//开始执行查询
if (sqlite3_prepare_v2(database, [selectSql
UTF8String], -1, &statement,
NULL)==SQLITE_OK) {
while (sqlite3_step(statement)==SQLITE_ROW) {
//构建行字典
NSMutableDictionary *row = [[NSMutableDictionary
alloc]
init];
//得到每一列的数据
char *stuid = (char*)sqlite3_column_text(statement,
0);
char *stuname = (char*)sqlite3_column_text(statement,
1);
char *stuclass = (char*)sqlite3_column_text(statement,
2);
//得到每一列oc对象
NSString *strName = [[NSString
alloc] initWithUTF8String:stuname];
NSString *strId = [[NSString
alloc] initWithUTF8String:stuid];
NSString *strClass = [[NSString
alloc] initWithUTF8String:stuclass];
//添加列内容到字典中
[row
setObject:strId forKey:@"id"];
[row
setObject:strName
forKey:@"name"];
[row
setObject:strClass
forKey:@"class"];
//将字典添加到数组中
[listData
addObject:row];
}
}
sqlite3_finalize(statement);
sqlite3_close(database);
}
return
listData;
}
//
-(NSMutableDictionary *)findById:(int)_nid{
//初始化
objDict=[[NSMutableDictionary
alloc]init];
//返回
return
objDict;
}
-(NSString *)getFilePath{
//设置文件保存的路径
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
//获取documents路径
NSString *documentPath = [paths lastObject];
//定义全路径
NSString *savePath = [documentPath stringByAppendingPathComponent:@"student.db"];
return savePath;
}
@end
3.
//
// ios24_saveObjectToFileViewController.h
// ios24-saveObjectToFile
//
// Created by on 13-6-18.
// Copyright 2013年 __MyCompanyName__. All rights reserved.
//
#import
<UIKit/UIKit.h>
#import
"sqlite3.h"
@interface ios24_saveObjectToFileViewController :
UIViewController<UITextFieldDelegate>
{
UITextField *tfId;
UITextField *tfName;
UITextField *tfClass;
sqlite3 *db;
}
/*
文件夹的含义:
bean:表的实体映射
--类名对应表名
--属性名
字段名
dao:对每一个表的曾删改查都放到这里
---doAdd增加
---doDelete删除
---doupdate更新
--findall
查询全部信息
---findbyid根据id查询
*/
@property (nonatomic,retain)
IBOutlet UITextField *tfId;
@property (nonatomic,retain)
IBOutlet UITextField *tfName;
@property (nonatomic,retain)
IBOutlet UITextField *tfClass;
-(IBAction)save;
-(IBAction)read;
-(IBAction)update;
-(IBAction)deleteObjectToFile;
-(NSString *)getFilePath;
-(void)showAlert:(NSString *)_msg;
@end
**********______________-----------
//
// ios24_saveObjectToFileViewController.m
// ios24-saveObjectToFile
//
// Created by on 13-6-18.
// Copyright 2013年 __MyCompanyName__. All rights reserved.
//
#import
"ios24_saveObjectToFileViewController.h"
#import
"StudentDao.h"
@implementation ios24_saveObjectToFileViewController
@synthesize tfId,tfName,tfClass;
- (void)didReceiveMemoryWarning
{
[super
didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
//保存数据
-(IBAction)save{
//定义定义数据库的操作类
StudentDao *stuDao=[[StudentDao
alloc]init];
Student *stu=[[Student
alloc]init];
stu.studentId=tfId.text;
stu.studentName=tfName.text;
stu.studentClass=tfClass.text;
if ([stuDao doAdd:stu]) {
[self
showAlert:@"成功"];
}else{
[self
showAlert:@"失败"];
}
}
-(void)showAlert:(NSString *)_msg{
UIAlertView *alert=[[UIAlertView
alloc]initWithTitle:@"提示"
message:_msg delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:@"确定",
nil];
[alert
show];
}
//读取数据
-(IBAction)read{
StudentDao *dbDao = [[StudentDao
alloc] init];
NSMutableArray *list = [dbDao findAll];
for (NSDictionary *row
in list) {
NSLog(@"name ; %@ ",[row
objectForKey:@"name"]);
}
}
//更新数据
-(IBAction)update{
Student *stu=[[Student
alloc]init];
stu.studentId=tfId.text;
stu.studentName=tfName.text;
stu.studentClass=tfClass.text;
StudentDao *stuDao=[[StudentDao
alloc]init];
if ([stuDao doUpdate:stu]) {
[self
showAlert:@"修改成功"];
}
else {
[self
showAlert:@"修改失败"];
}
}
//删除数据
-(IBAction)deleteObjectToFile{
StudentDao *stuDao=[[StudentDao
alloc]init];
int sid=[tfId.text
intValue] ;
if ([stuDao doDelete:sid]) {
[self
showAlert:@"删除成功"];
}else{
[self
showAlert:@"删除失败"];
}
}
-(NSString *)getFilePath{
//设置文件保存的路径
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
//获取documents路径
NSString *documentPath = [paths lastObject];
//定义全路径
NSString *savePath = [documentPath stringByAppendingPathComponent:@"student.db"];
return savePath;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField
resignFirstResponder];
return YES;
}
- (void)viewDidLoad
{
[super
viewDidLoad];
tfId.delegate=self;
tfName.delegate=self;
tfClass.delegate=self;
//创建数据库
NSString *dbPath=[self
getFilePath];
NSLog(@"%@",dbPath);
//开始创建数据库
if (sqlite3_open([dbPath
UTF8String], &db)!=SQLITE_OK) {
sqlite3_close(db);
//进行假言判断
NSAssert(NO,@"数据库打开失败");
}else{
//创建数据库
char *err;
NSString *createSql=@"create table if not exists student(sid interger(11) primary key,name varchar(50),class varchar(20))";
if (sqlite3_exec(db, [createSql
UTF8String], NULL,
NULL, &err)!=SQLITE_OK) {
sqlite3_close(db);
//
NSAssert(NO,@"数据表打开失败");
}
//
sqlite3_close(db);
}
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[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);
}
@end
//
// Student.h
// ios24-saveObjectToFile
//
// Created by on 13-6-18.
// Copyright 2013年 __MyCompanyName__. All rights reserved.
//
#import
<Foundation/Foundation.h>
//要实现当前类对象,就必须实现nscoding协议
@interface Student :
NSObject<NSCoding>
{
NSString *studentId;
NSString *studentName;
NSString *studentClass;
}
@property (nonatomic,retain)
NSString *studentId;
@property (nonatomic,retain)
NSString *studentName;
@property (nonatomic,retain)
NSString *studentClass;
@end
--------------------------------------------------
//
// Student.m
// ios24-saveObjectToFile
//
// Created by on 13-6-18.
// Copyright 2013年 __MyCompanyName__. All rights reserved.
//
#import
"Student.h"
@implementation Student
@synthesize studentId,studentName,studentClass;
@end
2.studentDao.h
//
// StudentDao.h
// ios24-saveObjectToSqList
//
// Created by on 13-6-18.
// Copyright 2013年 __MyCompanyName__. All rights reserved.
//
#import
<Foundation/Foundation.h>
#import
"Student.h"
#import
"sqlite3.h"
@interface StudentDao :
NSObject{
//用来存储查询的单条结果集
NSMutableDictionary *objDict;
//存储查询返回的集合
NSMutableArray *listData;
//student类
Student *stu;
//进行查询并且创建表
sqlite3 *database;
}
@property(nonatomic,retain)
NSMutableDictionary *objDict;
@property(nonatomic,retain)
NSMutableArray *listData;
@property(nonatomic,retain)
Student *stu;
-(BOOL) doAdd:(id) _obj;
-(BOOL) doDelete:(int ) _nid;
-(BOOL) doUpdate:(id) _obj;
-(NSMutableArray *)doFindALL:(NSString *)_condition;
-(NSMutableDictionary *)findById:(int)_nid;
-(NSString *)getFilePath;
@end
----------------------------------88888888*************
//
// StudentDao.m
// ios24-saveObjectToSqList
//
// Created by on 13-6-18.
// Copyright 2013年 __MyCompanyName__. All rights reserved.
//
#import
"StudentDao.h"
@implementation StudentDao
@synthesize listData,objDict,stu;
//添加
-(BOOL) doAdd:(id) _obj{
//把接收的参数转换为Student
Student *stu2 = (Student *)_obj;
//获取路径insertjiushihuijia kanjannijiushiyge
NSString *dbFilePath = [self
getFilePath];
if (sqlite3_open([dbFilePath
UTF8String], &database)!=SQLITE_OK) {
sqlite3_close(database);
return NO;
}else{
//定义sql
NSString *insertSql =
@"insert into student values(?,?,?);";
//定义预处理命令
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, [insertSql
UTF8String], -1, &statement,
NULL)==SQLITE_OK) {
//绑定参数
sqlite3_bind_int(statement, 1, [stu2.studentId
integerValue]);
sqlite3_bind_text(statement, 2, [stu2.studentName
UTF8String], -1,
NULL);
sqlite3_bind_text(statement, 3, [stu2.studentClass
UTF8String], -1,
NULL);
//执行,并判断是否成功
if (sqlite3_step(statement)!=SQLITE_DONE){
sqlite3_finalize(statement);
sqlite3_close(database);
return NO;
}
return YES;
}else{
//预编译失败,执行代码
sqlite3_finalize(statement);
sqlite3_close(database);
return NO;
}
}
return YES;
}
//删除
-(BOOL) doDelete:(int ) _nid{
//获取路径
NSString *dbFilePath=[self
getFilePath];
//1打开数据库
//2定义sql
//3进行预处理
//4执行sql
//5关闭数据库
if (sqlite3_open([dbFilePath
UTF8String], &database)!=SQLITE_OK) {
sqlite3_close(database);
return NO;
}else{
//定义sql
NSString *insertSql=@"delete from student where sid=?";
//定义预处理命令
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, [insertSql
UTF8String], -1, &statement,
NULL)==SQLITE_OK) {
NSString *xh=[NSString
stringWithFormat:@"%d",_nid];
//开始绑定参数
sqlite3_bind_text(statement, 1, [xh
UTF8String], -1,
NULL);
if (sqlite3_step(statement)!=SQLITE_DONE) {
//释放预处理对象资源
sqlite3_finalize(statement);
sqlite3_close(database);
return NO;
}else
{
sqlite3_finalize(statement);
sqlite3_close(database);
return YES;
}
}else{
sqlite3_finalize(statement);
sqlite3_finalize(statement);
sqlite3_close(database);
return NO;
}
}
return YES;
}
//更新
-(BOOL) doUpdate:(id) _obj{
stu=_obj;
//获取路径
NSString *dbFilePath=[self
getFilePath];
//1打开数据库
//2定义sql
//3进行预处理
//4执行sql
//5关闭数据库
if (sqlite3_open([dbFilePath
UTF8String], &database)!=SQLITE_OK) {
sqlite3_close(database);
return NO;
}else{
//定义sql
NSString *insertSql=@"update student set name=?,class=? where sid=?";
//定义预处理命令
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, [insertSql
UTF8String], -1, &statement,
NULL)==SQLITE_OK) {
//开始绑定参数
sqlite3_bind_text(statement, 1, [stu.studentName
UTF8String],-1,NULL);
sqlite3_bind_text(statement, 2, [stu.studentClass
UTF8String], -1,
NULL);
sqlite3_bind_int(statement, 3, [stu.studentId
integerValue]);
if (sqlite3_step(statement)!=SQLITE_DONE) {
//释放预处理对象资源
sqlite3_finalize(statement);
sqlite3_close(database);
return NO;
}else
{
sqlite3_finalize(statement);
sqlite3_close(database);
return YES;
}
}else{
sqlite3_finalize(statement);
sqlite3_finalize(statement);
sqlite3_close(database);
return NO;
}
}
return YES;
}
//查询返回结果集
-(NSMutableArray *)findAll{
//初始化数组
listData = [[NSMutableArray
alloc] initWithCapacity:1];
//设定路径
NSString *filePath = [self
getFilePath];
//1、打开数据库
if (sqlite3_open([filePath
UTF8String],&database)!=SQLITE_OK) {
sqlite3_close(database);
NSAssert(NO,@"打开数据库");
}else{
//预处理sql语句
NSString *selectSql = [NSString
stringWithFormat:@"select * from student"];
//绑定参数
sqlite3_stmt *statement;
//开始执行查询
if (sqlite3_prepare_v2(database, [selectSql
UTF8String], -1, &statement,
NULL)==SQLITE_OK) {
while (sqlite3_step(statement)==SQLITE_ROW) {
//构建行字典
NSMutableDictionary *row = [[NSMutableDictionary
alloc]
init];
//得到每一列的数据
char *stuid = (char*)sqlite3_column_text(statement,
0);
char *stuname = (char*)sqlite3_column_text(statement,
1);
char *stuclass = (char*)sqlite3_column_text(statement,
2);
//得到每一列oc对象
NSString *strName = [[NSString
alloc] initWithUTF8String:stuname];
NSString *strId = [[NSString
alloc] initWithUTF8String:stuid];
NSString *strClass = [[NSString
alloc] initWithUTF8String:stuclass];
//添加列内容到字典中
[row
setObject:strId forKey:@"id"];
[row
setObject:strName
forKey:@"name"];
[row
setObject:strClass
forKey:@"class"];
//将字典添加到数组中
[listData
addObject:row];
}
}
sqlite3_finalize(statement);
sqlite3_close(database);
}
return
listData;
}
//
-(NSMutableDictionary *)findById:(int)_nid{
//初始化
objDict=[[NSMutableDictionary
alloc]init];
//返回
return
objDict;
}
-(NSString *)getFilePath{
//设置文件保存的路径
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
//获取documents路径
NSString *documentPath = [paths lastObject];
//定义全路径
NSString *savePath = [documentPath stringByAppendingPathComponent:@"student.db"];
return savePath;
}
@end
3.
//
// ios24_saveObjectToFileViewController.h
// ios24-saveObjectToFile
//
// Created by on 13-6-18.
// Copyright 2013年 __MyCompanyName__. All rights reserved.
//
#import
<UIKit/UIKit.h>
#import
"sqlite3.h"
@interface ios24_saveObjectToFileViewController :
UIViewController<UITextFieldDelegate>
{
UITextField *tfId;
UITextField *tfName;
UITextField *tfClass;
sqlite3 *db;
}
/*
文件夹的含义:
bean:表的实体映射
--类名对应表名
--属性名
字段名
dao:对每一个表的曾删改查都放到这里
---doAdd增加
---doDelete删除
---doupdate更新
--findall
查询全部信息
---findbyid根据id查询
*/
@property (nonatomic,retain)
IBOutlet UITextField *tfId;
@property (nonatomic,retain)
IBOutlet UITextField *tfName;
@property (nonatomic,retain)
IBOutlet UITextField *tfClass;
-(IBAction)save;
-(IBAction)read;
-(IBAction)update;
-(IBAction)deleteObjectToFile;
-(NSString *)getFilePath;
-(void)showAlert:(NSString *)_msg;
@end
**********______________-----------
//
// ios24_saveObjectToFileViewController.m
// ios24-saveObjectToFile
//
// Created by on 13-6-18.
// Copyright 2013年 __MyCompanyName__. All rights reserved.
//
#import
"ios24_saveObjectToFileViewController.h"
#import
"StudentDao.h"
@implementation ios24_saveObjectToFileViewController
@synthesize tfId,tfName,tfClass;
- (void)didReceiveMemoryWarning
{
[super
didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
//保存数据
-(IBAction)save{
//定义定义数据库的操作类
StudentDao *stuDao=[[StudentDao
alloc]init];
Student *stu=[[Student
alloc]init];
stu.studentId=tfId.text;
stu.studentName=tfName.text;
stu.studentClass=tfClass.text;
if ([stuDao doAdd:stu]) {
[self
showAlert:@"成功"];
}else{
[self
showAlert:@"失败"];
}
}
-(void)showAlert:(NSString *)_msg{
UIAlertView *alert=[[UIAlertView
alloc]initWithTitle:@"提示"
message:_msg delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:@"确定",
nil];
[alert
show];
}
//读取数据
-(IBAction)read{
StudentDao *dbDao = [[StudentDao
alloc] init];
NSMutableArray *list = [dbDao findAll];
for (NSDictionary *row
in list) {
NSLog(@"name ; %@ ",[row
objectForKey:@"name"]);
}
}
//更新数据
-(IBAction)update{
Student *stu=[[Student
alloc]init];
stu.studentId=tfId.text;
stu.studentName=tfName.text;
stu.studentClass=tfClass.text;
StudentDao *stuDao=[[StudentDao
alloc]init];
if ([stuDao doUpdate:stu]) {
[self
showAlert:@"修改成功"];
}
else {
[self
showAlert:@"修改失败"];
}
}
//删除数据
-(IBAction)deleteObjectToFile{
StudentDao *stuDao=[[StudentDao
alloc]init];
int sid=[tfId.text
intValue] ;
if ([stuDao doDelete:sid]) {
[self
showAlert:@"删除成功"];
}else{
[self
showAlert:@"删除失败"];
}
}
-(NSString *)getFilePath{
//设置文件保存的路径
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
//获取documents路径
NSString *documentPath = [paths lastObject];
//定义全路径
NSString *savePath = [documentPath stringByAppendingPathComponent:@"student.db"];
return savePath;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField
resignFirstResponder];
return YES;
}
- (void)viewDidLoad
{
[super
viewDidLoad];
tfId.delegate=self;
tfName.delegate=self;
tfClass.delegate=self;
//创建数据库
NSString *dbPath=[self
getFilePath];
NSLog(@"%@",dbPath);
//开始创建数据库
if (sqlite3_open([dbPath
UTF8String], &db)!=SQLITE_OK) {
sqlite3_close(db);
//进行假言判断
NSAssert(NO,@"数据库打开失败");
}else{
//创建数据库
char *err;
NSString *createSql=@"create table if not exists student(sid interger(11) primary key,name varchar(50),class varchar(20))";
if (sqlite3_exec(db, [createSql
UTF8String], NULL,
NULL, &err)!=SQLITE_OK) {
sqlite3_close(db);
//
NSAssert(NO,@"数据表打开失败");
}
//
sqlite3_close(db);
}
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[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);
}
@end
相关文章推荐
- iOS开发封装篇-利用CAShapeLayer和绘图封装一个动画按钮
- 对AF的二次封装--ELNetworking(一个轻量级的iOS网络层)
- ios开源框架——如何在iOS SDK中创建一个umbrellaframework(framework库的再封装)
- 自己封装的一个基于ios的一个折叠tableView
- 【iOS 技巧- 4 】如何利用xib封装一个view
- iOS封装一个带placeholder和可限制最大输入字符数的TextView
- iOS回顾笔记(05) -- 手把手教你封装一个广告轮播图框架
- iOS开发之带你5分钟封装一个时间轴
- iOS_25_彩票设置的cell的数据源模型的封装
- iOS开发造轮子 | 优雅的封装一个倒计时button
- 项目中封装了一个弹出框但是点击上面的按钮就崩溃,尽在iOS9.0以下版本出现
- 一个封装好的iOS无限滚动组件HXInfiniteScrollView
- 自己动手写一个 iOS 网络请求库(NSURLSession 初探、封装接口、降低耦合、快速文件上传 )
- iOS开发 - 用AVPlayer封装一个播放器
- iOS封装浅谈-一句代码弹出actionSheet,如何优雅的设计一个ActionSheetManager
- iOS开发 - 用AVPlayer封装一个视频播放器
- iOS开发 - 封装一个自己的按钮
- iOS开发 封装一个可以复用的弹窗父类(TSActionAlertView)
- iOS UIButton文字和图片上下左右偏移封装,一个方法即可实现button上文字和图片不同位置的放置
- [转]ios的一个架构小例子---异步请求的封装