1:
//1.创建数据库 指定数据库的路径 一次
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"huanzhelast.sqlite"];
_dataBase = [FMDatabase databaseWithPath:path];
// NSLog(@"%@",path);
self.dbProvinceMarray = [NSMutableArray array];
self.dbProvinceName = [NSMutableArray array];
BOOL success = [_dataBase open];
if (success) {
NSLog(@"创建数据库成功!");
//2.创建表 字段 ()
// id 主键 fullname 全称 name 名字 treePath 树路径 parent 父类
//2.创建表
NSString *str = @"CREATE TABLE IF NOT EXISTS t_huanzhelast(id integer PRIMARY KEY AUTOINCREMENT , fullname text NOT NULL,name text NOT NULL, parent text NOT NULL,treePath text NOT NULL);";
if ([_dataBase executeUpdate:str]) {
NSLog(@"表创建成功!");
}else{
NSLog(@"创建表失败!");
}
}else{
NSLog(@"创建数据库失败!");
}
2:
//保存到数据库的省市区 数据
-(void)saveData{
for (int i=0; i<_searchplaceArray.count; i++) {
NSString *fullname=[[_searchplaceArray objectAtIndex:i] objectForKey:@"fullName"];
NSString *treePath=[[_searchplaceArray objectAtIndex:i] objectForKey:@"treePath"];
NSString *name=[[_searchplaceArray objectAtIndex:i] objectForKey:@"name"];
// NSNumber *parent1=[[_searchplaceArray objectAtIndex:i] objectForKey:@"parent"];
// NSInteger parent=[parent1 integerValue];
NSString *parent=[[_searchplaceArray objectAtIndex:i] objectForKey:@"parent"];
BOOL success = [_dataBase executeUpdate:@"INSERT INTO t_huanzhelast(fullname, name, parent,treePath) VALUES(?,?,?,?);",fullname,name,parent,treePath];
if (success) {
// NSLog(@"添加成功!");
}else{
NSLog(@"添加失败!");
}
}
[self selectProvince];
}
3://查询语句 省份
-(void)selectProvince{
NSString *strSql = @"SELECT * FROM t_huanzhelast where parent = 0;";
//查询语句 执行的方法
FMResultSet *set = [_dataBase executeQuery:strSql];
while ([set next]) {
NSString *parent = [set stringForColumn:@"parent"];
[self.dbProvinceMarray addObject:parent];
// NSString *str=@"0";
// if ([str isEqualToString:parent]) {
// [self.dbProvinceMarray addObject:parent];
//
// NSString *name = [set stringForColumn:@"name"];
// [self.dbProvinceName addObject:name];
// NSLog(@"name==%@",name);
// }
}
NSLog(@"==sp%zd",self.dbProvinceMarray.count);
}