您的位置:首页 > 移动开发 > IOS开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: