CREATE TABLE sqlite_master (
type TEXT,
name TEXT,
tbl_name TEXT,
rootpage INTEGER,
sql TEXT
);
对于表来说,type 字段永远是 ‘table’,name 字段永远是表的名字。所以,要获得数据库中所有表的列表, 使用下列SELECT语句:
SELECT name FROM sqlite_master
WHERE type=’table’
ORDER BY name;
对于索引,type 等于 ‘index’,name 则是索引的名字,tbl_name 是该索引所属的表的名字。 不管是表还是索引,sql 字段是原先用 CREATE TABLE 或 CREATE INDEX 语句创建它们时的命令文本。对于自动创建的索引(用来实现 PRIMARY KEY 或 UNIQUE 约束),sql字段为NULL。
sqlITE_MASTER 表是只读的。不能对它使用 UPDATE、INSERT 或 DELETE。 它会被 CREATE TABLE、CREATE INDEX、DROP TABLE 和 DROP INDEX 命令自动更新。
临时表不会出现在 sqlITE_MASTER 表中。临时表及其索引和触发器存放在另外一个叫 sqlITE_TEMP_MASTER 的表中。sqlITE_TEMP_MASTER 跟 sqlITE_MASTER 差不多, 但它只是对于创建那些临时表的应用可见。如果要获得所有表的列表, 不管是永久的还是临时的,可以使用类似下面的命令:
SELECT name FROM
(SELECT * FROM sqlite_master UNION ALL
SELECT * FROM sqlite_temp_master)
WHERE type=’table’
ORDER BY name
判断一个表是否存在
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{
@H_426_301@ char*err;NSString*sql = [NSStringstringWithFormat:@"SELECT COUNT(*) FROM sqlite_master where type='table' and name='%@';",name];
@H_426_301@ constchar*sql_stmt = [sqlUTF8String]; @H_426_301@ if(sqlite3_exec(db,sql_stmt,NULL,&err) ==1){ @H_426_301@ returnYES; @H_426_301@ }else{ @H_426_301@ returnNO; @H_426_301@ } @H_426_301@ }