创建5个UITextField用来输入联系人姓名,电话,年龄,邮箱,地址。自己创建。
首先导入FMDB,再添加libsqlite3.tbd,在修改添加修改-fno-objc-arc,如下图所示
环境配置好了之后,开始正式的操作。
创建数据库和数据表,并向其中插入数据
+(BOOL)insertDataIntoSql:(LHCDataModel *)model { NSString *dbString = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/User.db"]; FMDatabase *database = [[FMDatabase alloc] initWithPath:dbString]; [database open]; //创建表格的SQL语句//primary key不能重复//autoincrement自增长 NSString *createTableSql = @"create table if not exists MyFriends(id integer primary key autoincrement, name varchar(256), phone varchar(256), age varchar(256), email varchar(256),address varchar(256))"; //创建数据困中得表格 BOOL table = [database executeUpdate:createTableSql]; NSLog(@"table = %i",table); NSLog(@"path = %@",dbString); //创建插入数据的SQL语句 NSString *insertSql = @"insert into MyFriends(name,phone,age,email,address) values (?,?,?,?,?)"; BOOL insert = [database executeUpdate:insertSql,model.name,model.phone,model.age,model.email,model.address]; NSLog(@"insert = %i",insert); //关闭数据库 [database close]; return insert; }
这里我们创建了一个User.db的数据库和一张名为MyFriends的表,表中有6个元素:id, name, phone, age, email,address.
2.查询表中数据
+(NSMutableArray *)readDataFromSql { NSMutableArray *dataArray = [[NSMutableArray alloc] init]; NSString *dbString = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/User.db"]; FMDatabase *database = [[FMDatabase alloc] initWithPath:dbString]; [database open]; NSString *selectString = @"select * from MyFriends"; FMResultSet *set = [database executeQuery:selectString]; while ([set next]) { LHCDataModel *model = [[LHCDataModel alloc] init]; model.name = [set stringForColumn:@"name"]; model.phone = [set stringForColumn:@"phone"]; model.age = [set stringForColumn:@"age"]; model.email = [set stringForColumn:@"email"]; model.address = [set stringForColumn:@"address"]; model.rowId = [[set stringForColumn:@"id"] integerValue]; [dataArray addObject:model]; } [database close]; return dataArray; }
3.修改表中数据
+(BOOL)updateDataFromSql:(LHCDataModel *)model andbefore:(LHCDataModel *)beModel { NSString *dbString = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/User.db"]; FMDatabase *database = [[FMDatabase alloc] initWithPath:dbString]; [database open]; //判断条件可用id,如下面的删除数据 NSString *updateString = @"update MyFriends set name = ?, phone = ? , age = ? , email = ? , address = ? where name = ? and phone = ?"; BOOL update = [database executeUpdate:updateString,model.name,model.phone,model.age,model.email,model.address,beModel.name,beModel.phone]; NSLog(@"update = %i",update); [database close]; return update; }
4.删除表中数据
+(BOOL)deleteDataFromSql:(LHCDataModel *)model { NSString *dbString = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/User.db"]; FMDatabase *database = [[FMDatabase alloc] initWithPath:dbString]; [database open]; NSString *deleteString = @"delete from MyFriends where id = ?"; BOOL delete = [database executeUpdate:deleteString,[NSString stringWithFormat:@"%ld",model.rowId]]; NSLog(@"delete = %i",delete); return delete; }
到这里结合UI简易通讯录的核心操作差不多完成了
这里写的比较简陋,可参考我的Demo简易通讯录
地址:http://git.oschina.net/haitangliuxing/MyAddressBook