ios開發中,經常會用到數據庫sqlite的知識,除了增,刪,改,查之外,我們說說如何獲取數據庫中有多少表和表相關的內容。
前言
跟數據庫使用相關的一般的增刪改查的語句,這里就不做解釋了。在網上有很多。記得之前項目中曾經有這樣的一個場景。一個數據庫中存在了好幾個表。假定我們不知道表名的前提下來獲取這些數據。
數據庫結構
下來看看數據庫結構:
我們可以看到,在這個數據庫文件中一共有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": {
"校園": [
"上課瞌睡過度,眼皮打架無數,實在支持不住,誤入夢境深處,呼嚕,呼嚕,驚起同學全部",
"校園風光如此多驕,引無數情侶盡彎腰,惜龍女楊過,牛郎織女略輸一等,一代大傻韋小寶盡拿靈通發短信!",
"大學中永恆的愛情如鑽石一般的少,大多是:只因思顏意,未求終生緣!",
"有風的日子,不妨出去走走。不妨放松呼吸,走向絢麗陽光,把發黃的",
"心事交給流水,向遠去的霧靄行個注目禮。",
"逝去的年華在風中搖曳,楓樹下依舊殘留着她暖暖的氣息,緊緊的抱着她,卻發現眼前只有紛紛落下的楓葉.",
"我們的校園生活像一支旋律,它是熱情,快樂和友愛的組裝,是激越,奔放,令人陶醉的交響樂.",
"校園是我的一架鋼琴,作息是鋼琴的一副踏板,放飛的心是起伏的琴鍵,苦辣酸甜是多彩的樂章",
"我願青春如校園春雨中的牡丹,夏夜幽靜處的白蘭,秋風微露里的海棠,漫天飛雪中的寒梅.",
"虎子最近上火連續兩天上課鼻出血,昨天當鈴響起虎子鼻血再次噴出同桌小青很是關心:你的周期咋那么准?",
"路是自己走出來的:愛情是自己努力得來的:前途是自己拼出來的,你要努力啊",
"忘了,忘了吧,忘掉所有的不愉快,我們都非聖賢,在我心底,依然珍惜...",...