3

How can I retrieve sqlite3 table names in objective-c.For example if I created several tables, now i want to get (display) their names like .table in terminal.Thanks.

4 Answers 4

3

Use below code:

-(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;
}
Sign up to request clarification or add additional context in comments.

Comments

2
SELECT name FROM sqlite_master WHERE type='table'

1 Comment

Tnx Peko, as I understand it this is my query but how do i get the name, to work with it, maybe you can add some more code
1
SELECT * FROM sqlite_master where type='table'

try this.

Comments

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;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.