SQLite數(shù)據(jù)庫中一個(gè)特殊的名叫 SQLITE_MASTER 上執(zhí)行一個(gè)SELECT查詢以獲得所有表的索引。每一個(gè) SQLite 數(shù)據(jù)庫都有一個(gè)叫 SQLITE_MASTER 的表, 它定義數(shù)據(jù)庫的模式。 SQLITE_MASTER
表看起來如下:
CREATE TABLE sqlite_master (
SELECT name FROM sqlite_master SQLITE_MASTER 表是只讀的。不能對(duì)它使用 UPDATE、INSERT 或 DELETE。 它會(huì)被 CREATE TABLE、CREATE INDEX、DROP TABLE 和 DROP INDEX 命令自動(dòng)更新。 臨時(shí)表不會(huì)出現(xiàn)在 SQLITE_MASTER 表中。臨時(shí)表及其索引和觸發(fā)器存放在另外一個(gè)叫 SQLITE_TEMP_MASTER 的表中。SQLITE_TEMP_MASTER 跟 SQLITE_MASTER 差不多, 但它只是對(duì)于創(chuàng)建那些臨時(shí)表的應(yīng)用可見。如果要獲得所有表的列表, 不管是永久的還是臨時(shí)的,可以使用類似下面的命令:
SELECT name FROM
判斷一個(gè)表是否存在 1.FMDB:
- (BOOL) isTableOK:(NSString *)tableName { FMDatabase * db = [FMDatabase databaseWithPath:[CommonUtil filePathByDocumentDir:kDataBaseName]]; FMResultSet *rs = [db executeQuery:@"select count(*) as 'count' from sqlite_master where type ='table' and name = ?", tableName]; while ([rs next]) { // just print out what we've got in a number of formats. NSInteger count = [rs intForColumn:@"count"]; NSLog(@"isTableOK %d", count);
if (0 == count) { return NO; } else { return YES; } }
return NO; } 或者
- (int)isExistTable:(NSString *)tableName { NSString *name =nil; int isExistTable =0; FMDatabase * db = [FMDatabasedatabaseWithPath:[CommonUtilfilePathByDocumentDir:kDataBaseName]]; if ([dbopen]) { NSString * sql = [[NSStringalloc]initWithFormat:@"select name from sqlite_master where type = 'table' and name = '%@'",tableName]; FMResultSet * rs = [dbexecuteQuery:sql]; while ([rsnext]) { name = [rsstringForColumn:@"name"];
if ([nameisEqualToString:tableName]) { isExistTable =1; } } [dbclose]; } return isExistTable; }
2.sqlite3直接判斷 -(BOOL)checkName:(NSString *)name{ char *err; NSString *sql = [NSString stringWithFormat:@"SELECT COUNT(*) FROM sqlite_master where type='table' and name='%@';",name]; const char *sql_stmt = [sql UTF8String]; if(sqlite3_exec(db, sql_stmt, NULL, NULL, &err) == 1){ return YES; }else{ return NO; } } |
|