标签:
存储大量的,结构复杂的数据
常用数据库
(1)Microsoft SQL Server 2000/2008 ,中小企业使用居多
(2)Oracle 较复杂,大企业
(3)Myaql 数据库,网站使用多
(4)sqlite:本地数据库, 访问数据足够快, 直接访问文件,足够简单, 功能相对其他数据库软件不是特别齐全, 足够用了,足够小, 系统不超过1M, 适合在移动端上使用
(1)新建数据库
(2)新建数据表
(3)添加数据
(4)常用操作
增,删,改,查
SQL ==Structured Query Language
常用语句:
create table if not exists StuInfo(id integer,name varchar(20),age integer,score varchar(20)) //创建数据表
insert into StuInfo(id,name,age,score) values (2,‘zhao‘,19,‘100‘) //添加
//查询
select *from StuInfo ; //查询表格中所有数据
select name from StuInfo; //查询所有name
select *from StuInfo where name = ‘zhang‘; //查找name为zhang的所有信息
select *from StuInfo where name = ‘zhang‘ and id = ‘1‘
select *from StuInfo order by score desc //查询后排序(降序)
select count(*) from StuInfo //数据行数
update StuInfo set score = ‘80‘ where username = ‘zhang‘; //修改数据
delete from StuInfo where id = ‘2‘ //删除
(1)配置
导入文件,
添加二进制库 libsqlite3.dylib,
包含头文件#import "FMDatabase.h"
#import "FMDatabase.h" @interface ViewController () { //数据库对象 FMDatabase *_database; }
打开数据库
//init -(id)init{ if(self = [super init]){ [self openDatabase]; } return self; } //打开数据库 -(void)openDatabase{ NSString *path = [[NSBundle mainBundle] pathForResource:@"data.sqlite" ofType:nil]; _database = [[FMDatabase alloc] initWithPath:path]; if(!_database.open){ NSLog(@"fail"); } }
获取数据
//获取数据 -(NSArray *)firstLevel{ NSString *sql = @"select *from firstlevel"; //查询firstlevel FMResultSet *resultSet = [_database executeQuery:sql];//查询用executeQuery;其余用executeUpdate; NSMutableArray *muarr = [[NSMutableArray alloc] init]; while ([resultSet next]) { FirstLevelModel *model = [[FirstLevelModel alloc] init];//model存储数据 model.pid = [resultSet intForColumn:@"pid"]; model.pname = [resultSet stringForColumn:@"pname"]; model.pcount = [resultSet intForColumn:@"pcount"]; [muarr addObject:model]; } return muarr; }
打印数据
-(void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view. DatabaseManager *manager = [DatabaseManager sharedInstance]; NSArry *arr=[[NSArry alloc]]init]; arr=manager.firstLevel; for(FirstLevelModel *model in arr){ NSLog(@"%@",model.pname); } }
标签:
原文地址:http://www.cnblogs.com/Zhuyao/p/4396234.html