#import "ViewController.h"
#import "FMDatabase.h"
//dic ---> 哈希算法
//保存 1000 999 ----> 数据库 (哈希算法)
// ---> 10GB PC
// ---> Sqlite ---> 基本单位(表) 字段
// int integer
// float real
// string text(varchar)
// data blob
//区分数据唯一性 ---> 主键
//SQL(Structured Query Language)
//表
//Student name age
//创建一个表
//create table if not exists 表名 (字段1 字段类型,字段2 字段类型,...);
//create table if not exists Student (id integer primary key autoincrement,name text,age integer);
//增 删 改 查
//插入数据
//insert into 表名 (字段名,...) values (值,...);
//insert into Student (name,age) values ('zhangsan',100);
//查 *
//select 字段,... from 表名 where语句 order by 字段 desc | asc;
//select name,age from Student;
//select * from Student where name = 'zhangsan' and age > 100;
//select * from Student order by age desc;
//改
//update 表名 set 字段 = 值 条件
//update Student set age = 20 where name = 'zhangsan';
//删除
//delete from 表名 where 语句;
//delete from Student where name = 'zhangsan';
//系统
//库文件 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.4.sdk/usr/lib/libsqlite3.0.dylib
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//dataBase
//.txt
//创建数据库 ----> 沙盒
NSString *docPath = [NSString stringWithFormat:@"%@/Documents/Student.db",NSHomeDirectory()];
NSLog(@"%@",docPath); //
//单例 ----> 全局
FMDatabase *dataBase = [FMDatabase databaseWithPath:docPath];
[dataBase open];
//Cocochina
//http://course.tuicool.com/course/tag/ios-dev
//创建表
NSString *sqlStr = @"create table if not exists Student (id integer primary key autoincrement,name text,age integer)";
[dataBase executeUpdate:sqlStr];
#if 0
//增加数据
sqlStr = @"insert into Student (name,age) values (?,?)";
for(NSInteger i = 0; i < 10;i++ )
{
NSString *name = [NSString stringWithFormat:@"qianfeng%ld",i];
NSInteger age = arc4random() % 100 + 1;
//全部是对象
[dataBase executeUpdate:sqlStr,name,[NSNumber numberWithInteger:age]];
}
#endif
//查询
sqlStr = @"select * from Student where age < ?";
FMResultSet *result = [dataBase executeQuery:sqlStr,@50];
while ([result next]) {
//类型ForColumn:
NSLog(@"%@",[result stringForColumn:@"name"]);
NSLog(@"%d",[result intForColumn:@"age"]);
}
//修改
sqlStr = @"update Student set age = ? where name = ?";
[dataBase executeUpdate:sqlStr,@1000,@"qianfeng0"];
// //删除 和 修改
sqlStr = @"delete from Student where age < 50";
[dataBase executeUpdate:sqlStr];
[dataBase close];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
使用方法
#import "CYDataBase.h"
#import "FMDatabase.h"
@implementation CYDataBase
{
FMDatabase *_fmDataBase;
}
- (id)init
{
self = [super init];
if (self) {
NSString *docPath = [NSString stringWithFormat:@"%@/Documents/User.db",NSHomeDirectory()];
NSLog(@"%@",docPath);
_fmDataBase = [FMDatabase databaseWithPath:docPath];
[_fmDataBase open];
NSString *sqlStr = @"create table if not exists User (id integer primary key autoincrement,username text,userpassword text)";
[_fmDataBase executeUpdate:sqlStr];
[_fmDataBase close];
}
return self;
}
-(void)insertUserName:(NSString *)userName password:(NSString *)userPasswd
{
[_fmDataBase open];
NSString *sqlStr = @"insert into User (username,userpassword) values (?,?)";
[_fmDataBase executeUpdate:sqlStr,userName,userPasswd];
[_fmDataBase close];
}
-(BOOL)isUserExists:(NSString *)userName
{
BOOL ret = NO;
[_fmDataBase open];
NSString *sqlStr = @"select * from User where username = ?";
FMResultSet *result = [_fmDataBase executeQuery:sqlStr,userName];
ret = [result next];
[_fmDataBase close];
return ret;
}
-(BOOL)loginWithUserName:(NSString *)name passwd:(NSString *)userPasswd
{
BOOL ret = NO;
[_fmDataBase open];
NSString *sqlStr = @"select * from User where username = ? and userpassword = ?";
FMResultSet *result = [_fmDataBase executeQuery:sqlStr,name,userPasswd];
ret = [result next];
[_fmDataBase close];
return ret;
}
-(void)changePasswdByUserName:(NSString *)userName passwd:(NSString *)userPasswd
{
[_fmDataBase open];
[_fmDataBase executeUpdate:@"update User set userpassword = ? where username = ?",userPasswd,userName];
[_fmDataBase close];
}
+(CYDataBase *)sharedDataBase
{
static CYDataBase *dataBase = nil;
if(dataBase == nil)
{
dataBase = [[CYDataBase alloc] init];
}
return dataBase;
}
@end