iOS下数据库的简单理解

来源:互联网 发布:手机mac地址怎么改 编辑:程序博客网 时间:2024/05/19 19:35

数据库的操作类

对数据库做 创建表,插入,移除,删除,更新,查询操作,从数据库获取数据,处理数据

/创建表的方法
+ (void)creatTabelInDataBase {
//1.打开数据库
sqlite3 *db = [DataBaseManager openDateBase];
//2.创建SQL语句
NSString *creatSQL = @”create table if not exists Contact(con_id integer primary key autoincrement, con_name text, con_gender text, con_age text, con_phoneNum text, con_image blob)”;
//3.创建指令集
sqlite3_stmt *stmt = nil;
/*
参数1:数据库对象
参数2:SQL语句
参数3:是否限制SQL语句的长度 (-1 不限制)
参数4:指令集 地址
参数5:预留参数
*/
//4.进行语法检查
int flag = sqlite3_prepare_v2(db, [creatSQL UTF8String], -1, &stmt, nil);
if (flag == SQLITE_OK) {
NSLog(@”创建表语句检查正确”);
}

//5.获取指令集//6.如果指令集中包含问号需要绑定参数//7.执行SQL语句if (sqlite3_step(stmt) == SQLITE_DONE) {    NSLog(@"创建成功");}//释放数据库的空间sqlite3_finalize(stmt);//关闭数据库[DataBaseManager closeDataBase];

导入libsqlite3.0.dylib动态链接库,然后导入sqlite3.h的头文件

FMDB 使用 :

FMDatabase:创建数据库
创建表
执行SQL语句操作数据库 :1)打开数据库
2)操作 3)关闭数据库
FMResultSet:数据库查询的结果集
FMDatabaseQuene: 对于多线程环境下的数据库操作

//除了查询用 executeQuery 以外,其他的都使用executeUpdate:方法

//在StoryBoard下 界面跳转,可以实现传值

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
if ([segue.identifier isEqualToString:@”Add”]) {
//此时segue.destinationViewController指的是导航控制器
//添加联系人界面
AddViewController *addvc = [segue.destinationViewController viewControllers][0];//添加联系人界面
//在block块内部不能直接使用self 属性 实例变量 方法
//为了防止block和视图控制器对象的循环使用 mRC __block ARC: __weak
// __block ContactListViewController *vc = self;
__block typeof(self)weakSelf = self;
addvc.addBlock = ^(Contact *con){
//插入到数据库
[weakSelf insertDataBaseWithContact:con];
//添加到数据源数组中
[weakSelf.dataSource addObject: con];

        //刷新        [weakSelf.tableView reloadData];    };}else {    //详情界面    ContactDetailViewController *detailVC = segue.destinationViewController;    //把对应Contact对象传到下一界面    NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];//此时参数 sender为该行cell    Contact *con = self.dataSource[indexPath.row];    //属性传值    detailVC.con = con;

//创建数据库
- (void)creatDataBase {
//数据库操作对象
self.database = [FMDatabase databaseWithPath:[self getFilePath]];

}

//文件路径
- (NSString *)getFilePath {
return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject]stringByAppendingPathComponent:@”activity.sqlite”];
}
//创建表
- (void)creatTable {
//打开数据库
BOOL isOpen = [self.database open];
if (!isOpen) {
return;
}
//2.查询SQL语句操作数据库
[self.database executeUpdate:@”create table if not exists User(id integer primary key autoincrement, user text, password text)”];
//3.关闭数据库
[self.database close];

}

//查询
- (void)selectDataBase {
//1.打开数据库
if (![self.database open]) {
return;
}
//2.通过SQL语句操作数据库(查询)
FMResultSet result = [self.database executeQuery:@”select from User”];
while ([result next]) {
NSInteger ID = [result intForColumn:@”id”];
NSString *user = [result stringForColumn:@”user”];
NSString *password = [result stringForColumn:@”password”];

    //封装到Login对象中    Login *login = [[Login alloc] init];    login.user = user;    login.password =password;    login.ID = ID;    NSLog(@"^^^^^%ld",ID);    //存放到数据源数组中  [self.dataSource addObject:login];    [login release];}

}

//新增一条数据
- (void)insertDataBaseWithLogin: (Login *)log {
//打开数据库
if (![self.database open]) {
return;
}
//操作数据库(插入数据)
BOOL is = [self.database executeUpdate:@”insert into User(user, password) values(?, ?)”,log.user,log.password];//注意这时插入的字段,只有创建表时需要类型
NSLog( @”%@”,is ? @”success” :@”shibai”);
//关闭数据库
[self.database close];

}

0 0
原创粉丝点击