sqlite获取所有表的代码实现,注意这里不要使用FMDB,直接使用sqlite的c函数
- {
- sqlite3 *_db;
- BOOL _isOpen;
- }
-
- - (void)upgradeDBFromVersion0 {
- int status = [self openDB];
- if (status == SQLITE_OK) {
- NSLog(@"--开始升级db");
- _isOpen = YES;
- NSArray *allTables = [self getAllTables];
- for (NSString *tableName in allTables) {
- if ([tableName containsString:@"courseware_"] ||
- [tableName containsString:@"messageData_"] ||
- [tableName containsString:@"note_"]) {
- // 做迁移
- NSString *addColumnSql = [NSString stringWithFormat:@"alter table %@ add clearIndex INTEGER default 0 not null", tableName];
- int addColumeExecStatus = sqlite3_exec(_db, addColumnSql.UTF8String,NULL, NULL, NULL);
- if (addColumeExecStatus == SQLITE_OK) {
- NSLog(@"--添加字段成功");
- }
- else {
- NSLog(@"--添加字段失败");
- }
- }
- }
- int closeStatus = [self closeDB];
- if (closeStatus == SQLITE_OK) {
- NSLog(@"--关闭数据库成功");
- }
- else {
- NSLog(@"--关闭数据库失败");
- }
- }
- else {
- NSLog(@"--db打开失败");
- }
- }
-
- - (NSArray *)getAllTables {
- // 获取所有的需要升级的表名,注意:这里直接使用sqlite3的c函数,使用FMDB的话得不到想要的结果
- NSMutableArray *mArr = [[NSMutableArray alloc] init];
- NSString * sql = @"SELECT name FROM sqlite_master WHERE type='table' ORDER BY name";
- //2.执行语句
-
- // int status = sqlite3_exec(_db, sql.UTF8String,NULL, NULL, NULL);
- // NSLog(@"****%d", status);
-
- sqlite3_stmt * stmt = NULL;
- int res = sqlite3_prepare(_db, sql.UTF8String, -1, &stmt, NULL);
- if (res==SQLITE_OK) {
- NSLog(@"预处理成功");
- //3.一条条读数据
- while (sqlite3_step(stmt)==SQLITE_ROW) {
- // char * type = (char *) sqlite3_column_text(stmt, 0);
- // char * name = (char *) sqlite3_column_text(stmt, 1);
- // char * tbl_name = (char *) sqlite3_column_text(stmt, 2);
- // NSLog(@"------%s,%s,%s",type,name,tbl_name);
- char *name = (char *) sqlite3_column_text(stmt, 0);
- [mArr addObject:[NSString stringWithUTF8String:name]];
- NSLog(@"------%s", name);
- }
- }else{
- NSLog(@"预处理失败");
- }
- //释放开辟的内存空间
- sqlite3_finalize(stmt);
- NSLog(@"%@", mArr);
- return mArr.copy;
- }
-
- - (int)openDB {
- NSString *dbFilePath = [DBClient shareInstance].getDBFilePath;
- _db = NULL;
- int openStatus = sqlite3_open(dbFilePath.UTF8String, &_db);
- return openStatus;
- }
-
- - (int)closeDB {
- if (_isOpen) {
- int status = sqlite3_close(_db);
- return status;
- }
- return -1;
- }
信息加载中,请等待
微信客服(速回)
微信客服(慢回)