IOS FMDB 获取数据库表和表中的数据

时间:2023-03-09 06:34:24
IOS FMDB 获取数据库表和表中的数据

ios开发中,经常会用到数据库sqlite的知识,除了增,删,改,查之外,我们说说如何获取数据库中有多少表和表相关的内容。

前言

跟数据库使用相关的一般的增删改查的语句,这里就不做解释了。在网上有很多。记得之前项目中曾经有这样的一个场景。一个数据库中存在了好几个表。假定我们不知道表名的前提下来获取这些数据。

数据库结构

下来看看数据库结构:

IOS FMDB 获取数据库表和表中的数据

我们可以看到,在这个数据库文件中一共有10个表。我们的目的就是获取这些表中的内容。

获取数据库中有多少表

在这里我们使用的是 FMDB 来操作的。我们下打开一个数据库。

NSString *smsPaht = [[NSBundle mainBundle] pathForResource:@"sms" ofType:@"sqlite"];
FMDatabase *db = [FMDatabase databaseWithPath:smsPaht]; if ([db open]) { // 根据请求参数查询数据
FMResultSet *resultSet = nil; }

我们已经打开了数据库,众所周知 FMDB 使用executeQuery来调用数据库语句。在这里就不卖关子了。查询一个数据库中的所有的表的语句为:

resultSet = [db executeQuery:@"SELECT * FROM sqlite_master where type='table';"];

获取每个表的信息

FMResultSet 这个对象中有一个属性columnNameToIndexMap。它是一个NSMutableDictionary对象。其中key表示的是指定结果集中对应列的名称,value表示的是指定结果集中对应的列号(columnIdx)。我们打印出来可以看到:

{
name = 1;
rootpage = 3;
sql = 4;
"tbl_name" = 2;
type = 0;
}

后边我们获取表名的时候,就用到了name = 1 这个字段。我们看下边的代码:

NSString *smsPaht = [[NSBundle mainBundle] pathForResource:@"sms" ofType:@"sqlite"];

FMDatabase *db = [FMDatabase databaseWithPath:smsPaht];

if ([db open]) {

    // 根据请求参数查询数据
FMResultSet *resultSet = nil; resultSet = [db executeQuery:@"SELECT * FROM sqlite_master where type='table';"];
NSMutableDictionary *dict = resultSet.columnNameToIndexMap;
// 遍历查询结果
while (resultSet.next) { NSString *str0 = [resultSet stringForColumnIndex:0];
NSString *str1 = [resultSet stringForColumnIndex:1];
NSString *str2 = [resultSet stringForColumnIndex:2];
NSString *str3 = [resultSet stringForColumnIndex:3];
NSString *str4 = [resultSet stringForColumnIndex:4];
QKYLog(@"%@,%@,%@,%@,%@",str0,str1,str2,str3,str4); } }

打印结果为:

2016-08-26 15:20:05.909 xxxx[5255:190053] table,sqlite_sequence,sqlite_sequence,3,CREATE TABLE sqlite_sequence(name,seq)
2016-08-26 15:20:05.910 xxxx[5255:190053] table,member,member,4,CREATE TABLE 'member'( [id] integer PRIMARY KEY autoincrement, [recordid] integer,
[groupid] integer )
2016-08-26 15:20:05.910 xxxx[5255:190053] table,groups,groups,2,CREATE TABLE groups ( [id] integer PRIMARY KEY autoincrement, [name] varchar (100) )
2016-08-26 15:20:05.911 xxxx[5255:190053] table,blessing,blessing,5,CREATE TABLE blessing(title text primary key, content text)
2016-08-26 15:20:05.911 xxxx[5255:190053] table,festivel,festivel,83,CREATE TABLE festivel(title text primary key, content text)
2016-08-26 15:20:05.911 xxxx[5255:190053] table,funny,funny,357,CREATE TABLE funny(title text primary key, content text)
2016-08-26 15:20:05.913 xxxx[5255:190053] table,love,love,419,CREATE TABLE love(title text primary key, content text)
2016-08-26 15:20:05.944 xxxx[5255:190053] table,other,other,449,CREATE TABLE other(title text primary key, content text)
2016-08-26 15:20:05.944 xxxx[5255:190053] table,xiaoyuan,xiaoyuan,607,CREATE TABLE xiaoyuan(title text primary key, content text)
2016-08-26 15:20:05.944 xxxx[5255:190053] table,yuehui,yuehui,642,CREATE TABLE yuehui(title text primary key, content text)
2016-08-26 15:20:05.944 xxxx[5255:190053] table,zhichang,zhichang,670,CREATE TABLE zhichang(title text primary key, content text)

通过对比columnNameToIndexMap中的内容,大家应该看明白了吧。我们就是通过NSString *str1 = [resultSet stringForColumnIndex:1];来获取表名的。

代码

NSString *smsPaht = [[NSBundle mainBundle] pathForResource:@"sms" ofType:@"sqlite"];
FMDatabase *db = [FMDatabase databaseWithPath:smsPaht]; if ([db open]) { // 根据请求参数查询数据
FMResultSet *resultSet = nil; resultSet = [db executeQuery:@"SELECT * FROM sqlite_master where type='table';"]; NSMutableArray *tableNames = [NSMutableArray array];
// 遍历查询结果
while (resultSet.next) { NSString *str1 = [resultSet stringForColumnIndex:1];
[tableNames addObject:str1]; } NSMutableDictionary *result = [NSMutableDictionary dictionary]; for (NSString *tableName in tableNames) { FMResultSet *resultSet = nil; resultSet = [db executeQuery:[NSString stringWithFormat:@"SELECT * FROM %@;",tableName]];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
// 遍历查询结果
while (resultSet.next) { NSString *key = [resultSet stringForColumnIndex:0];
NSString *value = [resultSet stringForColumnIndex:1];
if (key && value) { // 在这里要对value按照⭐️进行分割
NSArray *array = [value componentsSeparatedByString:@"★"];
if (array) {
dict[key] = array;
} }
} result[tableName] = dict;
} NSData *jsonData = [NSJSONSerialization dataWithJSONObject:result options:0 error:NULL];
if (!jsonData) {
return;
} NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; QKYLog(@"%@",jsonStr);
}

总结

上边的代码仅作参考,在实际开发中会根据不同的数据结构有不用的写法。但是我们的目的主要是懂得如何去获取数据。

最终解析出的json数据为:

{
"member": {},
"groups": {},
"xiaoyuan": {
"校园": [
"上课瞌睡过度,眼皮打架无数,实在支持不住,误入梦境深处,呼噜,呼噜,惊起同学全部",
"校园风光如此多骄,引无数情侣尽弯腰,惜龙女杨过,牛郎织女略输一等,一代大傻韦小宝尽拿灵通发短信!",
"大学中永恒的爱情如钻石一般的少,大多是:只因思颜意,未求终生缘!",
"有风的日子,不妨出去走走。不妨放松呼吸,走向绚丽阳光,把发黄的",
"心事交给流水,向远去的雾霭行个注目礼。",
"逝去的年华在风中摇曳,枫树下依旧残留着她暖暖的气息,紧紧的抱着她,却发现眼前只有纷纷落下的枫叶.",
"我们的校园生活像一支旋律,它是热情,快乐和友爱的组装,是激越,奔放,令人陶醉的交响乐.",
"校园是我的一架钢琴,作息是钢琴的一副踏板,放飞的心是起伏的琴键,苦辣酸甜是多彩的乐章",
"我愿青春如校园春雨中的牡丹,夏夜幽静处的白兰,秋风微露里的海棠,漫天飞雪中的寒梅.",
"虎子最近上火连续两天上课鼻出血,昨天当铃响起虎子鼻血再次喷出同桌小青很是关心:你的周期咋那么准?",
"路是自己走出来的:爱情是自己努力得来的:前途是自己拼出来的,你要努力啊",
"忘了,忘了吧,忘掉所有的不愉快,我们都非圣贤,在我心底,依然珍惜...",...