引入libsqlite3.0.dylib
#import <UIKit/UIKit.h>
//引入头文件
#import "FMDatabase.h"
#import "Student.h"
@interface RootViewController : UIViewController
{
FMDatabase *db;
}
@end
//****************************************************************//
#import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 获取douPath路径
NSString *douPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// 获取documens文件下的dbPath路径
NSString *dbPath = [douPath stringByAppendingPathComponent: @"students.sqlite"];
// 建库
db = [FMDatabase databaseWithPath: dbPath];
NSLog(@"%@",NSHomeDirectory());
self.navigationItem.title = @"数据库";
UIButton *creatButton = [UIButton buttonWithType:UIButtonTypeCustom];
creatButton.frame = CGRectMake(50, 10, 100, 40);
[creatButton setTitle:@"建表" forState:UIControlStateNormal];
creatButton.backgroundColor = [UIColor blackColor];
creatButton.showsTouchWhenHighlighted = YES;
[self.view addSubview: creatButton];
[creatButton addTarget:self action:@selector(createTable) forControlEvents:UIControlEventTouchUpInside];
UIButton *insertButton = [UIButton buttonWithType:UIButtonTypeCustom];
insertButton.frame = CGRectMake(50, 70, 100, 40);
[insertButton setTitle:@"插入" forState:UIControlStateNormal];
insertButton.backgroundColor = [UIColor blackColor];
insertButton.showsTouchWhenHighlighted = YES;
[self.view addSubview: insertButton];
[insertButton addTarget:self action:@selector(insertStudent) forControlEvents:UIControlEventTouchUpInside];
UIButton *updateButton = [UIButton buttonWithType:UIButtonTypeCustom];
updateButton.frame = CGRectMake(50, 130, 100, 40);
[updateButton setTitle:@"更新" forState:UIControlStateNormal];
updateButton.backgroundColor = [UIColor blackColor];
updateButton.showsTouchWhenHighlighted = YES;
[self.view addSubview: updateButton];
[updateButton addTarget:self action:@selector(updateStudent) forControlEvents:UIControlEventTouchUpInside];
UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
selectButton.frame = CGRectMake(50, 190, 100, 40);
[selectButton setTitle:@"查询" forState:UIControlStateNormal];
selectButton.backgroundColor = [UIColor blackColor];
selectButton.showsTouchWhenHighlighted = YES;
[self.view addSubview: selectButton];
[selectButton addTarget:self action:@selector(selectStudent) forControlEvents:UIControlEventTouchUpInside];
UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
deleteButton.frame = CGRectMake(50, 250, 100, 40);
[deleteButton setTitle:@"删除" forState:UIControlStateNormal];
deleteButton.backgroundColor = [UIColor blackColor];
deleteButton.showsTouchWhenHighlighted = YES;
[self.view addSubview: deleteButton];
[deleteButton addTarget:self action:@selector(deleteStudent) forControlEvents:UIControlEventTouchUpInside];
// Do any additional setup after loading the view.
}
// 建表操作
- (void)createTable
{
// 打开数据库
if ([db open]) {
// 建表的语句
// 表名叫student 表中有三个字段(姓名 年龄 爱好)
// 数据库操作 创建语句 执行语句
NSString *createTableSql = [NSString stringWithFormat:@"create table if not exists student(id integer primary key autoincrement,name text,age integer,hobby text)"];
// 执行语句executeUpdate
BOOL result = [db executeUpdate: createTableSql];
if (result) {
NSLog(@"建表成功!");
} else {
NSLog(@"建表失败!");
}
}
// 关闭数据库
[db close];
}
// 向表中插入数据
- (void)insertStudent
{
Student *stu1 = [[Student alloc] init];
stu1.name = @"东哥";
stu1.age = 15;
stu1.hobby = @"三国演义";
if ([db open]) {
//创建插入语句
NSString *insertSql = [NSString stringWithFormat:@"insert into student (name,age,hobby) values ('%@',%ld,'%@')",stu1.name,stu1.age,stu1.hobby];
//执行语句
BOOL result = [db executeUpdate:insertSql];
if (result) {
NSLog(@"插入成功!");
} else {
NSLog(@"插入失败!");
}
}
//关闭数据库
[db close];
}
//更新学生的方法
- (void)updateStudent
{
if ([db open]) {
//创建数据库更新语句
NSString *updateSql = [NSString stringWithFormat:@"update student set hobby = '小文文' where hobby = '罗玉凤'"];
//执行语句
BOOL result = [db executeUpdate: updateSql];
if (result) {
NSLog(@"更新成功!");
} else {
NSLog(@"更行失败!");
}
}
[db close];
}
//删除学生数据 数据库删除语句
- (void)deleteStudent
{
if ([db open]) {
NSString *deleteSql = [NSString stringWithFormat:@"delete from student where name = '班长'"];
//执行语句
BOOL result = [db executeUpdate: deleteSql];
if (result) {
NSLog(@"删除成功!");
} else {
NSLog(@"删除失败!");
}
}
[db close];
}
//查询数据库方法
- (void)selectStudent
{
NSMutableArray *array = [NSMutableArray array];
if ([db open]) {
//创建数据库查询语句
NSString *selectSql = [NSString stringWithFormat:@"select * from student"];
//执行查询
FMResultSet *set = [db executeQuery: selectSql];
while ([set next]) {
Student *stu = [[Student alloc] init];
stu.name = [set stringForColumn:@"name"];
stu.age = [set intForColumn:@"age"];
stu.hobby = [set stringForColumn:@"hobby"];
[array addObject: stu];
}
NSLog(@"%@",array);
}
[db close];
}
//**********************************************************************//#import <Foundation/Foundation.h>
@interface Student : NSObject
@property(nonatomic,strong)NSString *name;
@property(nonatomic,assign)NSInteger age;
@property(nonatomic,strong)NSString *hobby;
@end