IOS FMDB新增表结构或者新增表字段
// 创建数据库
- (FMDatabase *)db{
if (_db == nil) {
_db = [FMDatabase databaseWithPath:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"StaffPosition.db"]];
}
return _db;
}
// 创建表
if ([self.db open]) {
if ([_db tableExists:@"summerxx"]) {
NSLog(@"不进行创建");
}else{
BOOL res = [_db executeUpdate:@"create table summerxx (name text, age text)"];
if (res == YES) {
NSLog(@"创建表成功");
}else{
NSLog(@"创建表失败");
}
}
}
// 插入数据
if ([_db open]) {
BOOL res = [_db executeUpdate:@"insert into summerxx (name, age) values (?, ?)", @"summerxx", @"22"];
if (res == YES) {
NSLog(@"插入数据成功");
}else{
NSLog(@"插入数据失败");
}
}
// 新增字段*****
if ([_db open]) {
[_db executeUpdate:@"ALTER TABLE summerxx ADD COLUMN hibbit text"];
BOOL res = [_db executeUpdate:@"ALTER TABLE summerxx ADD COLUMN height text"];
if (res == YES) {
NSLog(@"修改表成功");
}else{
NSLog(@"修改表失败");
}
}
// 把原表重新命名为临时表
if ([_db open]) {
BOOL res = [_db executeUpdate:@"ALTER TABLE summerxx RENAME TO __temp__summerxx"];
if (res == YES) {
NSLog(@"修改成临时表成功");
}else{
NSLog(@"失败");
}
}
// 创建新表
if ([self.db open]) {
if ([_db tableExists:@"summerxx"]) {
NSLog(@"不进行创建");
}else{
BOOL res = [_db executeUpdate:@"create table summerxx (name text, age text, hibbit text, height text)"];
if (res == YES) {
NSLog(@"创建表成功");
}else{
NSLog(@"创建表失败");
}
}
}
// 把临时表数据插入新表
if ([self.db open]) {
BOOL res = [_db executeUpdate:@"INSERT INTO summerxx SELECT name, age, hibbit, height FROM __temp__summerxx"];
if (res == YES) {
NSLog(@"成功");
}else{
NSLog(@"失败");
}
}
// 删除临时表
[self.db open];
// DROP TABLE __temp__summerxx
[_db executeUpdate:@"DROP TABLE __temp__summerxx"];