Sqlite 데이터베이스에서 테이블 이름과 필드 이름 가져오기

1827 단어
코드가 간략해서 보호가 많이 추가되지 않았다
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"database_name"];
    
    sqlite3 *database;
    sqlite3_open([path UTF8String], &database);
    
    char *errorMsg;
    const char *createSQL = "CREATE TABLE IF NOT EXISTS PEOPLE (ID INTEGER PRIMARY KEY AUTOINCREMENT, FIELD_DATA TEXT)";
    int result = sqlite3_exec(database, createSQL, NULL, NULL, &errorMsg);
    
    sqlite3_stmt *statement;
    if (result == SQLITE_OK) {
        const char *getTableInfo = "select * from sqlite_master where type='table' order by name;";
        result = sqlite3_prepare_v2(database, getTableInfo, -1, &statement, nil);
       while (sqlite3_step(statement) == SQLITE_ROW) {
            char *nameData = (char *)sqlite3_column_text(statement, 1);
            NSString *tableName = [[NSString alloc] initWithUTF8String:nameData];
            NSLog(@"name:%@",tableName);
        }
        
        const char *getColumn = "PRAGMA table_info(PEOPLE)";
        result = sqlite3_prepare_v2(database, getColumn, -1, &statement, nil);
        while (sqlite3_step(statement) == SQLITE_ROW) {
            char *nameData = (char *)sqlite3_column_text(statement, 1);
            NSString *columnName = [[NSString alloc] initWithUTF8String:nameData];
            NSLog(@"columnName:%@",columnName);
        }
        
        sqlite3_finalize(statement);
    }
    sqlite3_close(database);

지적 사항:
sqlite_Master 테이블 구조 CREATE TABLE sqlitemaster (   type TEXT,   name TEXT,   tbl_name TEXT,   rootpage INTEGER,   sql TEXT );

좋은 웹페이지 즐겨찾기