之前在安卓中做过项目通过本地的SQLITE数据库的项目,现在有了iphone,于是也想把它转到ios上
主要是对项目中的sqlite数据库文件的使用,其中对数据库中的操作用的是FMDBQueue,还有就是对网络图片的浏览,对网络图片的浏览用的是SDWebImage框架。
经过一天的学习与操作,终于完成了。主要步骤如下:
FMDBQueue库的加入:先去网上搜一下FMDBQueue的下载地址,先把它下载下来,然后将代码放到项目中就要以了。
由于其FMDBQueue依赖于系统自带的libsqlite3.dylib,所以也得要把libsqlite3.dylib添加到项目中来,不然会出问题的。
- (void)viewDidLoad { [super viewDidLoad]; // // 0.获得沙盒中的数据库文件名 NSString *filename = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"stu.db"]; NSError *error; NSBundle *bundle = [NSBundle mainBundle]; NSString *filenameAgo = [bundle pathForResource:@"stu" ofType:@"db"]; NSFileManager *fileManager = [NSFileManager defaultManager]; [fileManager copyItemAtPath:filenameAgo toPath:filename error:&error]; NSLog(@"filenameAgo>>>>%@",filenameAgo); NSLog(@"filename%@",filename); // 1.创建数据库队列 self.queue = [FMDatabaseQueue databaseQueueWithPath:filename]; // 2.创表 [self.queue inDatabase:^(FMDatabase *db) { BOOL result = [db executeUpdate:@"create table if not exists person (id integer primary key autoincrement, name text, age integer);"]; if (result) { NSLog(@"创表成功"); } else { NSLog(@"创表失败"); } }]; }上面还写了建表语句,其实它是用不到的。
#import <Foundation/Foundation.h> @interface IWStudent : NSObject @property (nonatomic,copy) NSString *stunum; @property (nonatomic,copy) NSString *name; @property (nonatomic,copy) NSString *tel; @property (nonatomic,copy) NSString *classname; @property (nonatomic,copy) NSString *qq; @property (nonatomic,copy) NSString *address; @property (nonatomic,copy) NSString *photo; @end
#import <Foundation/Foundation.h> @class IWStudent; @interface IWStudentTool : NSObject /** * 添加学生 * * @param student 需要添加的学生 */ + (BOOL)addStudent:(IWStudent *)student; /** * 获得所有的学生 * * @return 数组中装着都是IWStudent模型 */ + (NSArray *)students; /** * 根据搜索条件获得对应的学生 * * @param condition 搜索条件 */ + (NSArray *)studentsWithCondition:(NSString *)condition; @end
- (IBAction)query { self.myStudents = [NSMutableArray array]; [self.queue inDatabase:^(FMDatabase *db) { NSString *inputText = [self.inputTf text]; NSLog(@"query %@",inputText); NSMutableArray *students = [NSMutableArray array]; // 1.查询数据 FMResultSet *rs = [db executeQuery:@"select * from person where name=?;",@"王磊"]; // 2.遍历结果集 while (rs.next) { IWStudent *student = [[IWStudent alloc] init]; student.name = [rs stringForColumn:@"name"]; student.photo = [rs stringForColumn:@"photourl"]; student.classname = [rs stringForColumn:@"classname"]; student.qq = [rs stringForColumn:@"qq"]; student.tel = [rs stringForColumn:@"tel"]; student.stunum = [rs stringForColumn:@"stunum"]; student.address = [rs stringForColumn:@"address"]; NSString *name = [rs stringForColumn:@"name"]; NSLog(@" %@ ", student.tel); [self goImage:student.photo]; [students addObject: student]; self.myStudents = students; } }]; [self performSegueWithIdentifier:@"second" sender:self]; } -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ NSString *msg = self.inputTf.text; UIViewController *send = segue.destinationViewController; if([send respondsToSelector:@selector(setData:)]){ [send setValue:msg forKey:@"data"]; [send setValue:self.myStudents forKey:@"myStudents"]; } }
#import <UIKit/UIKit.h> #import "IWStudent.h" @class IWStudent; @interface IWSecondViewController : UITableViewController @property NSString *data; @property NSMutableArray *myStudents; @end
#import <UIKit/UIKit.h> @class IWStudent; @interface IWStudentCell : UITableViewCell @property(nonatomic,strong) IWStudent *student; @end
#import "IWStudentCell.h" #import "IWStudent.h" @interface IWStudentCell() @property (weak, nonatomic) IBOutlet UILabel *nameLb; @property (weak, nonatomic) IBOutlet UILabel *qqLb; @property (weak, nonatomic) IBOutlet UILabel *classNameLb; @property (weak, nonatomic) IBOutlet UILabel *telLb; @property (weak, nonatomic) IBOutlet UILabel *stuNumLb; @property (weak, nonatomic) IBOutlet UILabel *addressLb; @end @implementation IWStudentCell -(void)setStudent:(IWStudent *)student { _student = student; self.nameLb.text = [NSString stringWithFormat:@"姓名:%@",student.name]; self.qqLb.text = [NSString stringWithFormat:@"QQ:%@",student.qq]; self.classNameLb.text =[NSString stringWithFormat:@"班级:%@",student.classname]; self.telLb.text = [NSString stringWithFormat:@"电话:%@",student.tel]; self.stuNumLb.text = [NSString stringWithFormat:@"学号:%@",student.stunum]; self.addressLb.text = [NSString stringWithFormat:@"地址:%@",student.address]; } @end
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.myStudents.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { IWStudentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"student"]; cell.student = self.myStudents[indexPath.row]; return cell; }
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ IWStudent *stu = [IWStudent alloc]; stu = self.myStudents[self.tableView.indexPathForSelectedRow.row]; NSString *photoUrl = stu.photo; NSString *url; url = @"http://xxx.xxx.xxx.xxx"; url = [url stringByAppendingString:[NSString stringWithFormat:@"%@",photoUrl]]; NSLog(url); NSURL *urlPath = [NSURL URLWithString:url];<pre name="code" class="objc">#import "IWViewController.h" @interface IWThirdViewController : UIViewController @property (weak, nonatomic) IBOutlet UIImageView *photoIv; @property NSURL *photoUrl; @property NSString *photoUrlPath; @property (weak, nonatomic) IBOutlet UILabel *testLb; @end
#import "IWViewController.h" @interface IWThirdViewController : UIViewController @property (weak, nonatomic) IBOutlet UIImageView *photoIv; @property NSURL *photoUrl; @property NSString *photoUrlPath; @property (weak, nonatomic) IBOutlet UILabel *testLb; @end
#import "IWThirdViewController.h" #import "UIImageView+WebCache.h" @implementation IWThirdViewController @synthesize photoUrl; @synthesize photoUrlPath; -(void)viewDidLoad { [super viewDidLoad]; //self.testLb.text = photoUrlPath; [self.photoIv sd_setImageWithURL:photoUrl]; } @end