数据库FMDB
1.数据库
存储大量的,结构复杂的数据
常用数据库
(1)Microsoft SQL Server 2000/2008 ,中小企业使用居多
(2)Oracle 较复杂,大企业
(3)Myaql 数据库,网站使用多
(4)sqlite 本地数据库,访问快,简单 ,但功能不齐,小(系统不超过1M),移动端使用
2.MesaSQlite使用
(1)新建数据库
(2)新建数据表
(3)添加数据
(4)常用操作
3.SQL语句
SQL -->Structured Query Language
常用
create table if not exists ClassInfo(id integer,name varchar(20),age integer,score varchar(20)) //创建
insert into ClassInfo(id,name,age,score) values (2,'zhao',19,'100') //添加
select *from ClassInfo ; //查询
select name from ClassInfo;
select *from ClassInfo where name = 'zhang';
select *from ClassInfo where name = 'zhang' and id = '1'
select *from ClassInfo order by score desc //选择后排序(降序)
select count(*) from ClassInfo //数据行数
update ClassInfo set score = '80' where username = 'zhang'; //修改数据
delete from ClassInfo where id = '2' //delete
4.FMDB开源库使用
配置:添加FMDB库文件 --> 添加sqlite3.dylib库 --> 头文件#import "FMDatabase.h"
建立单例类:DatabaseManager
@interface DatabaseManager : NSObject +(id)sharedInstance; -(NSArray *)firstLevel; //获取第一级目录
@end
//init 打开数据库
//init -(id)init{ if(self = [super init]){ [self openDatabase]; //打开数据库 --> data.sqlite } return self; } //openDatabase -(void)openDatabase{ NSString *path = [[NSBundle mainBundle] pathForResource:@"data.sqlite" ofType:nil]; _database = [[FMDatabase alloc] initWithPath:path]; if(!_database.open){ NSLog(@"fail"); } }
获取数据库第一级目录
查询用executeQuery:
其余用executeUpdate:
//firstLevel -(NSArray *)firstLevel{ NSString *sql = @"select *from firstlevel"; //查询firstlevel FMResultSet *resultSet = [_database executeQuery:sql]; NSMutableArray *muarr = [[NSMutableArray alloc] init]; while ([resultSet next]) { FirstLevelModel *model = [[FirstLevelModel alloc] init]; model.pid = [resultSet intForColumn:@"pid"]; model.pname = [resultSet stringForColumn:@"pname"]; model.pcount = [resultSet intForColumn:@"pcount"]; [muarr addObject:model]; } return muarr; }
ViewController 直接调用单例
#pragma mark - downloadData -(void)downloadData{ DatabaseManager *manager = [DatabaseManager sharedInstance]; for(FirstLevelModel *model in manager.firstLevel){ // [_dataArray addObject:model.pname]; } [_tableView reloadData]; }