IOS-SQLite数据库操作
Objective-c通过FMDB操作SQLite数据库的步骤:
1.安裝FMDB
引入相关文件
2.打开数据库
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *dbPath = [docPath stringByAppendingPathComponent:@"user.db"];
3.操作table//创建表(FMDB中只有update和query操作,出了查询其他都是update操作)
[database executeUpdate:@"create table user (name text,gender text,age integer)"];
//插入数据
BOOL insert = [database executeUpdate:@"insert into user values (?,?,?)", self.nameTextField.text,self.genderTextField.text,self.ageTextField.text];
//不需要像Android中那样关闭Cursor关闭FMResultSet,因为相关的数据库关闭时,FMResultSet也会被自动关闭
FMResultSet *resultSet = [database executeQuery:@"select * from user"];
//参数必须是NSObject的子类,int,double,bool这种基本类型,需要封装成对应的包装类才可以
BOOL update = [database executeUpdate:@"update user set name = ? where age = ?",@"liu",[NSNumber numberWithInt:24]];
//删除数据
BOOL delete = [database executeUpdate:@"delete from user where name = ?",@"liuyinghui"];
4.关闭数据库
[databaseQueue close];
项目实战
1.在Xcode6.2下新建一个Single View Application工程,命名为SaliteModel。
引入FMDB,添加支持库libsqlite3.dylib
2. 在界面上添加控件并关联属性
关联属性
@property (strong, nonatomic) IBOutlet UITextField *nameTextField;
@property (strong, nonatomic) IBOutlet UITextField *genderTextField;
@property (strong, nonatomic) IBOutlet UITextField *ageTextField;
添加按钮响应方法//点击按钮后执行保存到数据库的操作
- (IBAction)saveButtonClicked:(id)sender {
//获取Document文件夹下的数据库文件,没有则创建
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *dbPath = [docPath stringByAppendingPathComponent:@"user.db"];
//获取数据库并打开
FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
if (![database open]) {
NSLog(@"Open database failed");
return;
}
//创建表(FMDB中只有update和query操作,出了查询其他都是update操作)
[database executeUpdate:@"create table user (name text,gender text,age integer)"];
//插入数据
BOOL insert = [database executeUpdate:@"insert into user values (?,?,?)", self.nameTextField.text,self.genderTextField.text,self.ageTextField.text];
if (insert) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"保存成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];
}
[database close];
}
//点击按钮后执行查询操作
- (IBAction)queryButtonTapped:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [paths objectAtIndex:0];
NSString *dbPath = [documentPath stringByAppendingPathComponent:@"user.db"];
FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
if (![database open]) {
return;
}
//不需要像Android中那样关闭Cursor关闭FMResultSet,因为相关的数据库关闭时,FMResultSet也会被自动关闭
FMResultSet *resultSet = [database executeQuery:@"select * from user"];
while ([resultSet next]) {
NSString *name = [resultSet stringForColumn:@"name"];
NSString *gender = [resultSet stringForColumn:@"gender"];
int age = [resultSet intForColumn:@"age"];
NSLog(@"Name:%@,Gender:%@,Age:%d",name,gender,age);
}
[database close];
//这里也不需要release
// [database release];
}
//执行条件查询操作
- (IBAction)queryByConditionBtnTapped:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [paths objectAtIndex:0];
NSString *dbPath = [docPath stringByAppendingPathComponent:@"user.db"];
FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
if (![database open]) {
return;
}
FMResultSet *resultSet = [database executeQuery:@"select * from user where name = ?",@"Ryan"];
while ([resultSet next]) {
NSString *name = [resultSet stringForColumn:@"name"];
NSString *gender = [resultSet stringForColumn:@"gender"];
int age = [resultSet intForColumn:@"age"];
NSLog(@"Name:%@,Gender:%@,Age:%d",name,gender,age);
}
[database close];
}
//执行更新操作
- (IBAction)updateBtnTapped:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [paths objectAtIndex:0];
NSString *dbPath = [docPath stringByAppendingPathComponent:@"user.db"];
FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
if (![database open]) {
return;
}
//参数必须是NSObject的子类,int,double,bool这种基本类型,需要封装成对应的包装类才可以
BOOL update = [database executeUpdate:@"update user set name = ? where age = ?",@"liuyinghui",[NSNumber numberWithInt:24]];
if(update){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"更新成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];
}
[database close];
}
- (IBAction)deleteBtnTapped:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [paths objectAtIndex:0];
NSString *dbPath = [docPath stringByAppendingPathComponent:@"user.db"];
FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
if (![database open]) {
return;
}
BOOL delete = [database executeUpdate:@"delete from user where name = ?",@"liuyinghui"];
if (delete) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"删除成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];
}
[database close];
}
-(void)executeDBOperation
{
//获取Document文件夹下的数据库文件,没有则创建
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *dbPath = [docPath stringByAppendingPathComponent:@"user.db"];
FMDatabaseQueue *databaseQueue = [FMDatabaseQueue databaseQueueWithPath:dbPath];
[databaseQueue inDatabase:^(FMDatabase *db){
[db executeUpdate:@"insert into user values (?,?,?)",@"Ren",@"Male",[NSNumber numberWithInt:20]];
}];
[databaseQueue close];
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。