获取Sqlite数据库中的表名和字段名

前端之家收集整理的这篇文章主要介绍了获取Sqlite数据库中的表名和字段名前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

代码很简略,没有添加太多的保护


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,&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,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 sqlite_master ( type TEXT,name TEXT,tbl_name TEXT,rootpage INTEGER,sql TEXT );

原文链接:https://www.f2er.com/sqlite/200589.html

猜你在找的Sqlite相关文章