如何在objective-c中检索sqlite3表名。例如,如果我创建了几个表,现在我想.table
在终端中获取(显示)它们的名称。谢谢。
问问题
4538 次
4 回答
3
使用以下代码:
-(NSMutableArray *)fetchTableNames
{
sqlite3_stmt* statement;
NSString *query = @"SELECT name FROM sqlite_master WHERE type=\'table\'";
int retVal = sqlite3_prepare_v2(YOUR_DB_OBJ,
[query UTF8String],
-1,
&statement,
NULL);
NSMutableArray *selectedRecords = [NSMutableArray array];
if ( retVal == SQLITE_OK )
{
while(sqlite3_step(statement) == SQLITE_ROW )
{
NSString *value = [NSString stringWithCString:(const char *)sqlite3_column_text(statement, 0)
encoding:NSUTF8StringEncoding];
[selectedRecords addObject:value];
}
}
sqlite3_clear_bindings(statement);
sqlite3_finalize(statement);
return selectedRecords;
}
于 2013-03-27T11:26:54.590 回答
2
SELECT name FROM sqlite_master WHERE type='table'
于 2013-03-27T10:49:08.120 回答
1
SELECT * FROM sqlite_master where type='table'
尝试这个。
于 2014-03-20T12:39:02.780 回答
0
- (NSArray *)listOfColumnNamesForTable:(NSString *)tableName WithDBFilePath:(NSString*)databasePath {
sqlite3 *sqlite3Database;
NSMutableArray *columnNames = [NSMutableArray array];
int openDatabaseResult = sqlite3_open([databasePath UTF8String], &sqlite3Database);
if(openDatabaseResult == SQLITE_OK) {
sqlite3_stmt *compiledStatement;
NSString *query = [NSString stringWithFormat:@"pragma table_info ('%@')",tableName];
int prepareStatementResult = sqlite3_prepare_v2(sqlite3Database, [query UTF8String], -1, &compiledStatement, NULL);
if(prepareStatementResult == SQLITE_OK) {
sqlite3_stmt* statement;
int retVal = sqlite3_prepare_v2(sqlite3Database,
[query UTF8String],
-1,
&statement,
NULL);
if ( retVal == SQLITE_OK )
{
while(sqlite3_step(statement) == SQLITE_ROW )
{
NSString *value = [NSString stringWithCString:(const char *)sqlite3_column_text(statement, 1)
encoding:NSUTF8StringEncoding];
[columnNames addObject:value];
}
}
sqlite3_clear_bindings(statement);
sqlite3_finalize(statement);
sqlite3_close(sqlite3Database);
return [columnNames mutableCopy];
}
}
sqlite3_close(sqlite3Database);
return nil;
}
于 2015-08-10T08:36:34.263 回答