IOS Sqlite用户界面增删改查案例

Posted gccbuaa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IOS Sqlite用户界面增删改查案例相关的知识,希望对你有一定的参考价值。

1.案例简单介绍

对SQLite操作进行了简单的封装,将对数据表操作转变成对对象的操作,并通过UI界面完毕对用户表的增、删、改、查,执行界面例如以下图所看到的
技术分享a

2.项目project文件夹


技术分享
UserModel类为用户对象模型。与数据库表中字段一一相应
BaseDB类为对sqlite简单封装,抽象出对数据库操作的三个方法。详细内容看http://blog.csdn.net/whzhaochao/article/details/38865535博文
UserDB继承于BaseDB。主要是完毕UserModel对象的增、删、改、查,其内容例如以下
UserDB.h
//
//  UserDB.h
//  SqliteDemo
//
//  Created by 赵超 on 14-8-27.
//  Copyright (c) 2014年 赵超. All rights reserved.
//

#import "BaseDB.h"
#import "UserModel.h"

@interface UserDB : BaseDB{
    
}
//单例
+(id)shareUserDB;
//创建User表
-(void)creatTableWithDataBaseName:(NSString*) dbName;
//添加一个UserModel
-(BOOL)addUser:(UserModel*)userModel dbName:(NSString*)dbName;
//改动一个UserModel
-(BOOL)updateUser:(UserModel*)userModel dbName:(NSString*)dbName;
//查询全部
-(NSArray*)findAllUser:(NSString*)dbName;
//删除一个对象
-(BOOL) deleteUser:(UserModel*)userModel dbName:(NSString*)dbName;

@end
UserDB.m
//
//  UserDB.m
//  SqliteDemo
//
//  Created by 赵超 on 14-8-27.
//  Copyright (c) 2014年 赵超. All rights reserved.
//

#import "UserDB.h"

@implementation UserDB

static UserDB * db;

+(id) shareUserDB{
    if (db==nil) {
        db= [[UserDB alloc] init];
    }
    return  db;
}

-(void)creatTableWithDataBaseName:(NSString*) dbName{
    NSString *[email protected]"create table user( userName text primary key,passWord text, userEmail text)";
    [self createTable:sql dataBaseName:dbName];
    
}

-(BOOL) deleteUser:(UserModel*)userModel dbName:(NSString*)dbName{
        NSString *[email protected]"delete from user where userName =?";
        NSArray *[email protected][ userModel.userName];
     return  [self execSql:sql parmas:params dataBaseName:dbName];
}


-(BOOL)addUser:(UserModel *)userModel dbName:(NSString *)dbName{
    NSString *[email protected]"insert into user (userName,passWord,userEmail) values (?,?,?

)"; NSArray *[email protected][userModel.userName,userModel.passWord,userModel.userEmail]; return [self execSql:sql parmas:params dataBaseName:dbName]; } -(NSArray*) findAllUser:(NSString *)dbName{ NSString *[email protected]"select userName,passWord,userEmail from user"; NSArray *result= [self selectSql:sql parmas:nil dataBaseName:dbName]; NSMutableArray *users=[NSMutableArray array]; for (NSDictionary *dic in result) { UserModel *user=[[UserModel alloc] init]; user.userName=[dic objectForKey:@"userName"]; user.passWord=[dic objectForKey:@"passWord"]; user.userEmail=[dic objectForKey:@"userEmail"]; [users addObject:user]; } return users; } -(BOOL)updateUser:(UserModel *)userModel dbName:(NSString *)dbName{ NSString *[email protected]"update user set userName=?

,passWord=?,userEmail=? where userName=?"; NSArray *[email protected][userModel.userName,userModel.passWord,userModel.userEmail,userModel.userName]; return [self execSql:sql parmas:params dataBaseName:dbName]; } @end


MainViewController是用户显示全部用户列表
AddViewController用于添加和改动用户界面的显示




























以上是关于IOS Sqlite用户界面增删改查案例的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot整合MyBatis实现增删改查案例完整版(附源代码)

yii2框架增删改查案例

用vue实现一个增删改查案例

(01)Restful风格的增删改查案例及其junit测试详解

(01)Restful风格的增删改查案例及其junit测试详解

Mysql 增删改查的