我想把我的iPhone应用程序迁移到一个新的数据库版本。由于我没有保存一些版本,我需要检查是否存在某些列名。
这个Stackoverflow条目建议执行选择
SELECT sql FROM sqlite_master
WHERE tbl_name = 'table_name' AND type = 'table'
并解析结果。
这是常见的方式吗?选择呢?
我想把我的iPhone应用程序迁移到一个新的数据库版本。由于我没有保存一些版本,我需要检查是否存在某些列名。
这个Stackoverflow条目建议执行选择
SELECT sql FROM sqlite_master
WHERE tbl_name = 'table_name' AND type = 'table'
并解析结果。
这是常见的方式吗?选择呢?
当前回答
我知道这是一个旧的帖子,但最近我需要同样的帖子,并找到了一个简洁的方法:
SELECT c.name FROM pragma_table_info('your_table_name') c;
其他回答
如果您正在搜索任何特定的列,您可以使用Like语句
ex:
SELECT * FROM sqlite_master where sql like('%LAST%')
要获得列的列表,您可以简单地使用:
.schema tablename
如果你有sqlite数据库,使用sqlite3命令行程序和这些命令:
列出数据库中所有的表。
.tables
要显示给定表名的模式:
.schema tablename
当你运行sqlite3命令行时,输入:
sqlite3 -header
也会给你想要的结果吗
-(NSMutableDictionary*)tableInfo:(NSString *)table
{
sqlite3_stmt *sqlStatement;
NSMutableDictionary *result = [[NSMutableDictionary alloc] init];
const char *sql = [[NSString stringWithFormat:@"pragma table_info('%s')",[table UTF8String]] UTF8String];
if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
{
NSLog(@"Problem with prepare statement tableInfo %@",[NSString stringWithUTF8String:(const char *)sqlite3_errmsg(db)]);
}
while (sqlite3_step(sqlStatement)==SQLITE_ROW)
{
[result setObject:@"" forKey:[NSString stringWithUTF8String:(char*)sqlite3_column_text(sqlStatement, 1)]];
}
return result;
}