UI - DataBase


<ViewController.h>

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end

<ViewController.m>

#import "ViewController.h"
#import "DataBaseManager.h"
#import "Student.h"
@interface ViewController ()
@property (retain, nonatomic) IBOutlet UITextField *numberTf;
@property (retain, nonatomic) IBOutlet UITextField *nameTf;
@property (retain, nonatomic) IBOutlet UITextField *sexTf;
@property (retain, nonatomic) IBOutlet UITextField *ageTf;

@end

@implementation ViewController




//插入
- (IBAction)insertData:(id)sender {
    //获取学生对象
    Student *stu = [[Student alloc]init];
    stu.name = self.nameTf.text;
    stu.number = [self.numberTf.text intValue];
    stu.age = self.ageTf.text;
    stu.sex = self.sexTf.text;
    
    //数据库插入操作
    //1.打开数据库
    sqlite3 *db = [DataBaseManager openDataBase];
    //2.创建指令集(伴随指针)
    sqlite3_stmt *stmt = nil;
    //3.设置 sql 语句
    NSString *sqlStr = @"INSERT INTO Student(number,name,sex,age)VALUES(?,?,?,?)";
    //4.语法检查(注意,此时仅仅进行语法的检查 没有执行任何数据库的操作)
    
    //此语句中的参数说明
    //sqlite3 *db , 数据库的地址
    //const char *zSql , SQL 语句(C语言字符串)
    //int nByte , SQL 语句长度,一般给-1 代表不限制长度
    //sqlite3_stmt **ppStmt , 给指令集指针的地址,内部会为我们创建的指令集自动赋值
    //const char **pzTail , 预留参数
    
    int flag = sqlite3_prepare_v2( db, [sqlStr UTF8String], -1, &stmt, NULL);
    //5.判定语法检查是否正确
    if (flag == SQLITE_OK) {
        
        //6.参数绑定
        sqlite3_bind_int( stmt, 1, (int)stu.number);
        sqlite3_bind_text(stmt, 2, [stu.name UTF8String], -1, nil);
        sqlite3_bind_text(stmt, 3, [stu.sex UTF8String], -1, nil);
        sqlite3_bind_text(stmt, 4, [stu.age UTF8String], -1, nil);
        
        //7.执行语句
        sqlite3_step(stmt);
        
    }
    [stu release];
    
    //8.释放内存
    sqlite3_finalize(stmt);
    //9.关闭数据库
    [DataBaseManager closeDataBase];
    
}
//更新
- (IBAction)updataData:(id)sender {
    
    //1.打开数据库
    sqlite3 *db = [DataBaseManager openDataBase];
    //2.创建指令集
    sqlite3_stmt *stmt = nil;
    //3.设置 sql语句
    NSString *sqlStr = @"UPDATE Student SET sex = '女汉子' WHERE name = ?";
    //4.语法检查
    int flag = sqlite3_prepare_v2(db,[sqlStr UTF8String], -1, &stmt, NULL);
    //5.判断语法是否正确
    if (flag == SQLITE_OK) {
        //6.绑定
        sqlite3_bind_text(stmt, 1, [self.nameTf.text UTF8String], -1, nil);
        //7.执行
        sqlite3_step(stmt);
    }
    //8.释放内存
    sqlite3_finalize(stmt);
    //9.关闭数据库
    [DataBaseManager closeDataBase];
    
    
}
//查询
- (IBAction)selectData:(id)sender {
    
    //1.打开数据库
    sqlite3 *db = [DataBaseManager openDataBase];
    //2.创建指令集
    sqlite3_stmt *stmt = nil;
    //3.设置 sql 语句
    NSString *sqlStr = @"SELECT *FROM Student WHERE number = ?";
    //4.语法检查
    int result = sqlite3_prepare_v2(db, [sqlStr UTF8String], -1, &stmt, NULL);
    //5.判定语法是否正确
    if (result == SQLITE_OK) {
        
        //6.参数绑定
        sqlite3_bind_int(stmt, 1, (int)[self.numberTf.text intValue]);
        
        //7.执行语句
        //对于数据的查询获取的结果可能有多个,所以要进行数据逐条查询,每一次 while 循环读取一条数据
        //每一次读取一条数据会判断是否有下一条(SQLITE_ROW)当没有数据时, SQLITE_DONE 表示数据读取结束
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
            //数据获取
            int number = sqlite3_column_bytes(stmt, 1);
            char *name = (char *)sqlite3_column_text(stmt, 2);
            char *sex = (char *)sqlite3_column_text(stmt, 3);
            char *age = (char *)sqlite3_column_text(stmt, 4);
            
            //对象封装
            Student *stu = [[Student alloc] init];
            stu.number = number;
            stu.name = [NSString stringWithUTF8String:name];
            stu.sex = [NSString stringWithUTF8String:sex];
            stu.age = [NSString stringWithUTF8String:age];
            
            NSLog(@"%ld , %@ , %@ , %@",stu.number,stu.name,stu.sex,stu.age);
            [stu release];
        }
        
    }
    
    
    //8.释放内存
    sqlite3_finalize(stmt);
    //9.关闭数据库
    [DataBaseManager closeDataBase];
    
}
//删除
- (IBAction)deleteData:(id)sender {
    
    //1.打开数据库
    sqlite3 *db = [DataBaseManager openDataBase];
    //2.穿件指令集
    sqlite3_stmt *stmt = nil;
    //3.设置 sql 语句
    NSString *sqlStr =@"DELETE FROM Student WHERE number = ?";
    //4.语法检查
    int flag = sqlite3_prepare_v2(db, [sqlStr UTF8String], -1, &stmt, NULL);
    //5.判断语法是否正确
    if (flag == SQLITE_OK) {
        
        //6.参数绑定
        sqlite3_bind_int(stmt, 1, (int)[self.numberTf.text intValue]);
        //7.执行语句
        sqlite3_step(stmt);
        
    }
    //8.释放内存
    sqlite3_finalize(stmt);
    //9.关闭数据库
    [DataBaseManager closeDataBase];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_numberTf release];
    [_nameTf release];
    [_sexTf release];
    [_ageTf release];
    [super dealloc];
}
@end
<DataBaseManager.h>
#import <Foundation/Foundation.h>
#import <sqlite3.h>
/*对于数据库需要简单地配置 :
 1.导入动态链接库 sqlite3.0.dylib    //每一个导入的文件.0 和没有.0 的区别在于.0的是最新的
 2.导入库文件 sqlite3.h
*/
@interface DataBaseManager : NSObject
//对于数据库管理类 DataBaseManager 只需要具备两个功能
//1.打开数据库
+(sqlite3 *)openDataBase;
//2.关闭数据库
+(void)closeDataBase;

@end

<DataBaseManager.m>

#import "DataBaseManager.h"

@implementation DataBaseManager
//因为对于一个程序一个数据库足以  所以只需要一个对象
static sqlite3 *db = nil;
+(sqlite3 *)openDataBase
{
    //   @synchronized
    if (db) {
        return db;
    }
    //获取 Caches 文件路径
    NSString *cacherPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    //拼接数据库文件路径
    NSString *dbPath = [cacherPath stringByAppendingString:@"/DB.sqlite"];
    NSLog(@"%@",cacherPath);
    
    //打开数据库
    //根据给定文件路径打开数据库  如果数据库文件不存在就先创建一个
   int result = sqlite3_open([dbPath UTF8String], &db);//[string UTF8String] 把 OC字符串转化成 C语言字符串
    if (result == SQLITE_OK) {
        NSLog(@"成功打开数据库");
        
        //sql 语句
        NSString *sqlStr = @"CREATE TABLE Student (stu_ID INTEGER PRIMARY KEY NOT NULL,number INTEGER NOT NULL,name TEXT NOT NULL,sex TEXT NOT NULL DEFAULT 未知,age TEXT NOT NULL DEFAULT 0)";
        //执行 sql 语句
        sqlite3_exec(db, [sqlStr UTF8String], NULL, NULL, NULL);
        
    }
    return db;
    
}

+(void)closeDataBase
{
    int result = sqlite3_close(db);
    if (result == SQLITE_OK) {
        NSLog(@"关闭数据库");
        db = nil;
    }
}

@end
<Student.h>
#import <Foundation/Foundation.h>

@interface Student : NSObject
@property (nonatomic,copy)NSString *name;
@property (nonatomic)NSInteger number;
@property (nonatomic,copy)NSString *sex;
@property (nonatomic,copy)NSString *age;
@end

<Student.m>

#import "Student.h"

@implementation Student

@end

你可能感兴趣的:(database,数据库操作,数据库总结)