iOS开发——⾼级篇——FMDB数据库简单使⽤
1#import <Foundation/Foundation.h>
2
3@interface UserDB : NSObject
4
5// 把userDB设计成⼀个单例类
6 + (id)shareInstance;
7
8// 创建⽤户表
9 - (void)createTable;
10
11// 添加⽤户
12 - (void)addData:(NSArray *)dataArray;
13
14// 查询⽤户
15 - (NSArray *)findDatas;
16
17// 删除⼀⾏数据
18 - (void)deleteRowData:(NSArray *)array;
19
20// 清空表中的数据:
21 - (void)clearTableData;
22
23// 更新数据
24 - (void)executeUpdate:(NSArray *)array;
25
26@end
.h⽂件
1#import"UserDB.h"
2#import <FMDB.h>
3
4#define dataBasePath [[(NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)) lastObject]stringByAppendingPathComponent:dataBaseName]  5#define dataBaseName @"GuoBIn.s
qlite"
6
7// 把userDB设计成⼀个单例类
8static UserDB *instnce;
9
10@implementation UserDB
11
12// 把userDB设计成⼀个单例类
13 + (id)shareInstance
14 {
15if (instnce == nil) {
16        instnce = [[[self class] alloc] init];
17    }
18return instnce;
19 }
20
21// 创建⽤户表
22 - (void)createTable
23 {
24// ⽂件路径
25    NSString *filePath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/%@",dataBaseName];
26    NSLog(@"⽂件路径 == %@",filePath);
27
28    FMDatabase *db = [FMDatabase databaseWithPath:dataBasePath];
29
30if ([db open]) {
31if (![db tableExists:@"user"]) {
32if ([db executeUpdate:@"CREATE TABLE user (Serial text primary key,dataText text)"]) {
33                NSLog(@"创建表成功");
34            }else{
35                NSLog(@"创建表失败");
36            }
37        } else {
38            NSLog(@"表已经存在");
39        }
40    } else{
41        NSLog(@"打开表失败");
42    }
43    [db close];
44
45 }
46
47// 添加⽤户
48 - (void)addData:(NSArray *)dataArray
49 {
50    NSString *serial  = [dataArray objectAtIndex:0];
51    NSString *dataText = [dataArray objectAtIndex:1];
52
53    FMDatabase *db = [FMDatabase databaseWithPath:dataBasePath];
54if ([db open]) {
55// 插⼊数据
56    [db executeUpdate:@"insert into user (Serial,DataText) values(?,?)",serial,dataText,nil];
57    }
58    [db close];
59 }
60
61// 查询⽤户
62 - (NSArray *)findDatas
63 {
64    NSMutableArray *users = [NSMutableArray array];
65    FMDatabase *db = [FMDatabase databaseWithPath:dataBasePath];
66if ([db open]) {
67// 获取所有数据
68        FMResultSet *rs = [db executeQuery:@"SELECT * FROM user"];
69while ([rs next]) {
70            NSString *serial = [rs stringForColumn:@"Serial"];
71            NSString *dataText = [rs stringForColumn:@"DataText"];
72
73            [users addObject:@[serial,dataText]];
74        }
75        [rs close];
76    }数据库简单吗
77    [db close];
78return users;
79 }
80
81 - (void)deleteRowData:(NSArray *)array
82 {
83    NSString *serial = [array objectAtIndex:0];
84    NSString *dataText = [array objectAtIndex:1];
85
86    FMDatabase *db = [FMDatabase databaseWithPath:dataBasePath];
87if ([db open]) {
88// 删除某个数据
89        BOOL rs = [db executeUpdate:@"DELETE FROM user WHERE Serial = ? and DataText = ?",serial,dataText]; 90
91if (rs) {
92            NSLog(@"删除成功");
93        } else {
94            NSLog(@"删除失败");
95        }
96    }
97    [db close];
98 }
99
100// 清空表中的数据:
101 - (void)clearTableData
102 {
103    FMDatabase *db = [FMDatabase databaseWithPath:dataBasePath];
104if ([db open]) {
105// 清除全部数据
106        [db executeUpdate:@"DELETE FROM user"];
107    }
108    [db close];
109 }
110
111// 更新数据
112 - (void)executeUpdate:(NSArray *)array
113 {
114    NSString *serial = [array objectAtIndex:0];
115    NSString *dataText = [array objectAtIndex:1];
116
117    FMDatabase *db = [FMDatabase databaseWithPath:dataBasePath];
118if ([db open]) {
119        [db executeUpdate:@"UPDATE user SET DataText = ? WHERE Serial = ?",dataText,serial];
120    }
121    [db close];
122
123 }
124
125@end
.m⽂件

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。