SQLite是一款轻型的嵌入式数据库
它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了
它的处理速度比Mysql、PostgreSQL这两款著名的数据库都还快
(建议:尽量给字段设定严格的约束,以保证数据的规范性)
[db executeUpdate:@"UPDATEt_studentSET age = ? WHERE name = ?;", @20, @"Jack"]
// 查询数据
FMResultSet *rs = [db executeQuery:@"SELECT* FROM t_student"];
// 遍历结果集
while ([rs next]) {
NSString*name = [rs stringForColumn:@"name"];
int age= [rs intForColumn:@"age"];
doublescore = [rs doubleForColumn:@"score"];
}
示例: NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
// 拼接文件名
NSString *filePath = [cachePath stringByAppendingPathComponent:@"contact.sqlite"];
// 创建一个数据库的实例,仅仅在创建一个实例,并不会打开数据库
FMDatabase *db = [FMDatabase databaseWithPath:filePath];
_db = db;
// 打开数据库
BOOL flag = [db open];
if (flag) {
NSLog(@"打开成功");
}else{
NSLog(@"打开失败");
}
// 创建数据库表
// 数据库操作:插入,更新,删除都属于update
// 参数:sqlite语句
BOOL flag1 = [db executeUpdate:@"create table if not exists t_contact (id integer primary key autoincrement,name text,phone text);"];
if (flag1) {
NSLog(@"创建成功");
}else{
NSLog(@"创建失败");
}
增:
- (IBAction)insert:(id)sender {
// ?:表示数据库里面的占位符
BOOL flag = [_db executeUpdate:@"insert into t_contact (name,phone) values (?,?)",@"oooo",@"21321321"];
if (flag) {
NSLog(@"success");
}else{
NSLog(@"failure");
}
}
- (IBAction)delete:(id)sender {
BOOL flag = [_db executeUpdate:@"delete from t_contact;"];
if (flag) {
NSLog(@"success");
}else{
NSLog(@"failure");
}
}
- (IBAction)update:(id)sender {
// FMDB?,只能是对象,不能是基本数据类型,如果是int类型,就包装成NSNumber
BOOL flag = [_db executeUpdate:@"update t_contact set name = ?",@"abc"];
if (flag) {
NSLog(@"success");
}else{
NSLog(@"failure");
}
}
- (IBAction)select:(id)sender {
FMResultSet *result = [_db executeQuery:@"select * from t_contact"];
// 从结果集里面往下找
while ([result next]) {
NSString *name = [result stringForColumn:@"name"];
NSString *phone = [result stringForColumn:@"phone"];
NSLog(@"%@--%@",name,phone);
}
}
FMDatabaseQueue*queue = [FMDatabaseQueue databaseQueueWithPath:path];
[queue inDatabase:^(FMDatabase *db) {
[db executeUpdate:@"INSERTINTO t_student(name)VALUES (?)",@"Jack"];
[db executeUpdate:@"INSERTINTO t_student(name)VALUES (?)",@"Rose"];
[db executeUpdate:@"INSERTINTO t_student(name)VALUES (?)",@"Jim"];
FMResultSet *rs = [db executeQuery:@"select* from t_student"];
while ([rs next]) {
// …
}
}];
[queue inTransaction:^(FMDatabase *db, BOOL*rollback) {
[db executeUpdate:@"INSERTINTO t_student(name)VALUES (?)",@"Jack"];
[db executeUpdate:@"INSERTINTO t_student(name)VALUES (?)",@"Rose"];
[db executeUpdate:@"INSERTINTO t_student(name)VALUES (?)",@"Jim"];
FMResultSet *rs = [db executeQuery:@"select* from t_student"];
while ([rs next]) {
// …
}
}];
*rollback = YES;
NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
// 拼接文件名
NSString *filePath = [cachePath stringByAppendingPathComponent:@"user.sqlite"];
// 创建数据库实例
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:filePath];
_queue = queue;
// 创建数据库表
// 提供了一个多线程安全的数据库实例
[queue inDatabase:^(FMDatabase *db) {
BOOL flag = [db executeUpdate:@"create table if not exists t_user (id integer primary key autoincrement,name text,money integer)"];
if (flag) {
NSLog(@"success");
}else{
NSLog(@"failure");
}
}];
- (IBAction)update:(id)sender {
// update t_user set money = 500 where name = 'a';
// update t_user set money = 1000 where name = 'b';
// a -> b 500 a 500
// b + 500 = b 1000
[_queue inDatabase:^(FMDatabase *db) {
// 开启事务
[db beginTransaction];
BOOL flag = [db executeUpdate:@"update t_user set money = ? where name = ?;",@500,@"a"];
if (flag) {
NSLog(@"success");
}else{
NSLog(@"failure");
// 回滚
[db rollback];
}
BOOL flag1 = [db executeUpdate:@"updat t_user set money = ? where name = ?;",@1000,@"b"];
if (flag1) {
NSLog(@"success");
}else{
NSLog(@"failure");
[db rollback];
}
// 全部操作完成时候再去
[db commit];
}];
}
- (IBAction)add:(id)sender {
[_queue inDatabase:^(FMDatabase *db) {
BOOL flag = [db executeUpdate:@"insert into t_user (name,money) values (?,?)",@"a",@1000];
if (flag) {
NSLog(@"success");
}else{
NSLog(@"failure");
}
[db executeUpdate:@"insert into t_user (name,money) values (?,?)",@"b",@500];
}];
}
- (IBAction)delete:(id)sender {
[_queue inDatabase:^(FMDatabase *db) {
BOOL flag = [db executeUpdate:@"delete from t_user;"];
if (flag) {
NSLog(@"success");
}else{
NSLog(@"failure");
}
}];
}