zoukankan      html  css  js  c++  java
  • iOS_数据库3_sqlite3基本操作

    终于效果图:

    Sqlite3函数总结 

    1.打开数据库
    int sqlite3_open(
        const char *filename,   // 数据库的文件路径
        sqlite3 **ppDb          // 数据库实例
    );
    
    2.运行不论什么SQL语句
    int sqlite3_exec(
        sqlite3*,                                  // 一个打开的数据库实例
        const char *sql,                           // 须要运行的SQL语句
        int (*callback)(void*,int,char**,char**),  // SQL语句运行完成后的回调
        void *,                                    // 回调函数的第1个參数
        char **errmsg                              // 错误信息
    );
    
    3.检查SQL语句的合法性(查询前的准备)
    int sqlite3_prepare_v2(
        sqlite3 *db,            // 数据库实例
        const char *zSql,       // 须要检查的SQL语句
        int nByte,              // SQL语句的最大字节长度
        sqlite3_stmt **ppStmt,  // sqlite3_stmt实例,用来获得数据库数据
        const char **pzTail
    );
    
    4.查询一行数据
    int sqlite3_step(sqlite3_stmt*); // 假设查询到一行数据,就会返回SQLITE_ROW
    
    5.利用stmt获得某一字段的值(字段的下标从0開始)
    double sqlite3_column_double(sqlite3_stmt*, int iCol);  // 浮点数据
    int sqlite3_column_int(sqlite3_stmt*, int iCol); // 整型数据
    sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol); // 长整型数据
    const void *sqlite3_column_blob(sqlite3_stmt*, int iCol); // 二进制文本数据
    const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);  // 字符串数据











    控制器中直接写SQL语句,未封装
    //
    //  ViewController.m
    //  1.sqlite3基本操作
    //
    //  Created by xss on 14-11-2.
    //  Copyright (c) 2014年 beyond. All rights reserved.
    //
    
    #import "ViewController.h"
    // 1.导入库,2.加入主头文件
    #import <sqlite3.h>
    @interface ViewController ()
    {
        // db代表着整个数据库。db是数据库实例
        sqlite3 *_db;
    }
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // 0.获得沙盒中的数据库文件名称
        NSString *filename = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"student.sqlite"];
        NSLog(@"filePath:%@",filename);
        /*
         2014-11-02 18:32:38.895 1.sqlite3基本操作[85356:245858] filePath:/Users/juns/Library/Developer/CoreSimulator/Devices/3198E002-E3C9-4523-983E-AC3E1283A654/data/Containers/Data/Application/E1150608-3EB8-4B9D-87AF-33EDF9FB6FF3/Documents/student.sqlite
         2014-11-02 18:32:38.896 1.sqlite3基本操作[85356:245858] 成功打开数据库
         2014-11-02 18:32:38.897 1.sqlite3基本操作[85356:245858] 成功创建t_student表
         */
        
        // 1.创建(打开)数据库(假设是首次打开,则数据库文件不存在。那么会自己主动创建)
        // OC字符串,直接转成C语言字符串,通过 UTF8String方法
        int result = sqlite3_open(filename.UTF8String, &_db);
        if (result == SQLITE_OK) {
            NSLog(@"成功打开数据库");
            
            // 2.创表
            const char *sql = "create table if not exists t_student (id integer primary key autoincrement, name text, age integer);";
            // 凡是要传地址的,最好先清空,防止出现野指针,C语言中空是NULL
            char *errorMesg = NULL;
            // 參数3和4是回调时用
            int result = sqlite3_exec(_db, sql, NULL, NULL, &errorMesg);
            if (result == SQLITE_OK) {
                NSLog(@"成功创建t_student表");
            } else {
                NSLog(@"创建t_student表失败:%s", errorMesg);
            }
        } else {
            NSLog(@"打开数据库失败");
        }
    }
    
    // 全然仿造createTable操作
    - (IBAction)insertBtnClicked:(UIButton *)sender
    {
        for (int i = 0; i<30; i++) {
            NSString *name = [NSString stringWithFormat:@"beyond-%d", arc4random()%100];
            int age = arc4random()%100;
            NSString *sql = [NSString stringWithFormat:@"insert into t_student (name, age) values('%@', %d);", name, age];
            
            char *errorMesg = NULL;
            int result = sqlite3_exec(_db, sql.UTF8String, NULL, NULL, &errorMesg);
            if (result == SQLITE_OK) {
                NSLog(@"成功加入数据");
            } else {
                NSLog(@"加入数据失败:%s", errorMesg);
            }
        }
    }
    // 方法同上
    - (IBAction)deleteBtnClicked:(UIButton *)sender
    {
    }
    // 方法同上
    - (IBAction)updateBtnClicked:(UIButton *)sender
    {
    }
    //
    - (IBAction)queryBtnClicked:(UIButton *)sender
    {
        // SQL注入漏洞
        
        /**
         登录功能
         
         1.用户输入账号和password
         * 账号:123' or 1 = 1 or '' = '
         * password:456654679
         
         2.拿到用户输入的账号和password去数据库查询(查询有没有这个用户名和password)
         select * from t_user where username = '123' and password = '456';
         
         
         select * from t_user where username = '123' and password = '456';
         */
        
        // 1.定义sql语句
        const char *sql = "select id, name, age from t_student where name = ?;";
        
        // 2.定义一个stmt存放结果集,用于运行静态 SQL 语句并返回它所生成结果的对象
        sqlite3_stmt *stmt = NULL;
        
        // 3.检測SQL语句的合法性,參数3是sql语句的长度,仅仅要写-1,会自己主动计算,參数4是statement存放结果集
        int result = sqlite3_prepare_v2(_db, sql, -1, &stmt, NULL);
        if (result == SQLITE_OK)
        {
            NSLog(@"查询语句是合法的");
            
            // 设置占位符的内容,參数2 指的是第几个占位符号,注意是从1開始;參数4是占位符的长度,仅仅要写-1,会自己主动计算,
            sqlite3_bind_text(stmt, 1, "beyond", -1, NULL);
            
            // 4.step 运行SQL语句,从结果集中取出数据
            // int stepResult = sqlite3_step(stmt);
            
            // step的运行结果等于SQLITE_ROW,表示真的查询到一行数据
            while (sqlite3_step(stmt) == SQLITE_ROW)
            {
                // 获得这行相应的数据,结果存放在statement中
                
                // 获得第0列的id
                int sid = sqlite3_column_int(stmt, 0);
                
                // 获得第1列的name
                const unsigned char *sname = sqlite3_column_text(stmt, 1);
                
                // 获得第2列的age
                int sage = sqlite3_column_int(stmt, 2);
                
                NSLog(@"%d %s %d", sid, sname, sage);
            }
        } else {
            NSLog(@"查询语句非合法");
        }
    }
    @end
    


    使用工具类封装
    模型
    //
    //  Student.h
    //  2_数据库工具类封装
    //
    //  Created by xss on 14-11-2.
    //  Copyright (c) 2014年 beyond. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface Student : NSObject
    // 学号
    @property (nonatomic, assign) int ID;
    // 姓名
    @property (nonatomic, copy) NSString *name;
    // 年龄
    @property (nonatomic, assign) int age;
    @end
    


    工具类StudentDAO
    //
    //  StudentDAO.h
    //  2_数据库工具类封装
    //
    //  Created by xss on 14-11-2.
    //  Copyright (c) 2014年 beyond. All rights reserved.
    //  学生数据的CRUD(增删改查)
    
    #import <Foundation/Foundation.h>
    @class Student;
    @interface StudentDAO : NSObject
    /**
     *  加入学生
     *
     *  @param student 须要加入的学生
     */
    + (BOOL)addStudent:(Student *)student;
    
    /**
     *  获得全部的学生
     *
     *  @return 数组中装着都是IWStudent模型
     */
    + (NSArray *)students;
    
    /**
     *  依据搜索条件获得相应的学生
     *
     *  @param condition 搜索条件
     */
    + (NSArray *)studentsWithCondition:(NSString *)condition;
    @end
    


    关键封装的代码
    //
    //  StudentDAO.m
    //  2_数据库工具类封装
    //
    //  Created by xss on 14-11-2.
    //  Copyright (c) 2014年 beyond. All rights reserved.
    //
    
    #import "StudentDAO.h"
    #import "Student.h"
    #import <sqlite3.h>
    @implementation StudentDAO
    
    // 重点~~~~加上static的作用:能保证_db这个变量仅仅被StudentDAO.m内部訪问
    static sqlite3 *_db;
    // 重点~~~~工具类第一次载入的时候调用initialize方法
    + (void)initialize
    {
        // 0.获得沙盒中的数据库文件名称
        NSString *filename = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"student.sqlite"];
        
        // 1.创建(打开)数据库(假设数据库文件不存在。会自己主动创建)
        int result = sqlite3_open(filename.UTF8String, &_db);
        if (result == SQLITE_OK) {
            NSLog(@"成功打开数据库");
            
            // 2.创表
            const char *sql = "create table if not exists t_student (id integer primary key autoincrement, name text, age integer);";
            char *errorMesg = NULL;
            int result = sqlite3_exec(_db, sql, NULL, NULL, &errorMesg);
            if (result == SQLITE_OK) {
                NSLog(@"成功创建t_student表");
            } else {
                NSLog(@"创建t_student表失败:%s", errorMesg);
            }
        } else {
            NSLog(@"打开数据库失败");
        }
        
        for (int i = 0; i<30; i++) {
            NSString *name = [NSString stringWithFormat:@"beyond-%d", arc4random()%100];
            int age = arc4random()%100;
            NSString *sql = [NSString stringWithFormat:@"insert into t_student (name, age) values('%@', %d);", name, age];
            
            char *errorMesg = NULL;
            int result = sqlite3_exec(_db, sql.UTF8String, NULL, NULL, &errorMesg);
            if (result == SQLITE_OK) {
                NSLog(@"成功加入数据");
            } else {
                NSLog(@"加入数据失败:%s", errorMesg);
            }
        }
        
    }
    
    + (BOOL)addStudent:(Student *)student
    {
        // Sqlite3中全部的字符串必须用单引號
        NSString *sql = [NSString stringWithFormat:@"insert into t_student (name, age) values('%@', %d);", student.name, student.age];
        
        char *errorMesg = NULL;
        // 两个NULL代表的是回调方法,此处不须要
        int result = sqlite3_exec(_db, sql.UTF8String, NULL, NULL, &errorMesg);
        
        return result == SQLITE_OK;
    }
    // 返回全部的学生对象组成的数组
    + (NSArray *)students
    {
        // 0.定义数组
        NSMutableArray *students = nil;
        
        // 1.定义sql语句
        const char *sql = "select id, name, age from t_student;";
        
        // 2.定义一个stmt存放结果集
        sqlite3_stmt *stmt = NULL;
        
        // 3.检測SQL语句的合法性
        int result = sqlite3_prepare_v2(_db, sql, -1, &stmt, NULL);
        if (result == SQLITE_OK) {
            NSLog(@"查询语句是合法的");
            students = [NSMutableArray array];
            
            // 4.运行SQL语句,从结果集中取出数据
            while (sqlite3_step(stmt) == SQLITE_ROW) { // 真的查询到一行数据
                // 获得这行相应的数据
                
                Student *student = [[Student alloc] init];
                
                // 获得第0列的id
                student.ID = sqlite3_column_int(stmt, 0);
                
                // 获得第1列的name
                const unsigned char *sname = sqlite3_column_text(stmt, 1);
                student.name = [NSString stringWithUTF8String:(const char *)sname];
                
                // 获得第2列的age
                student.age = sqlite3_column_int(stmt, 2);
                
                // 加入到数组
                [students addObject:student];
            }
        } else {
            NSLog(@"查询语句非合法");
        }
        
        return students;
    }
    // 模糊查询
    + (NSArray *)studentsWithCondition:(NSString *)condition
    {
        // 0.定义数组
        NSMutableArray *students = nil;
        
        // 1.定义sql语句  带模糊查询  '%林%'
        const char *sql = "select id, name, age from t_student where name like ?

    ;"; // 2.定义一个stmt存放结果集 sqlite3_stmt *stmt = NULL; // 3.检測SQL语句的合法性 int result = sqlite3_prepare_v2(_db, sql, -1, &stmt, NULL); if (result == SQLITE_OK) { NSLog(@"查询语句是合法的"); students = [NSMutableArray array]; // 填补占位符的内容,%在OC中是keyword,因此转义 NSString *newCondition = [NSString stringWithFormat:@"%%%@%%", condition]; sqlite3_bind_text(stmt, 1, newCondition.UTF8String, -1, NULL); // 4.运行SQL语句,从结果集中取出数据 while (sqlite3_step(stmt) == SQLITE_ROW) { // 真的查询到一行数据 // 获得这行相应的数据 Student *student = [[Student alloc] init]; // 获得第0列的id student.ID = sqlite3_column_int(stmt, 0); // 获得第1列的name const unsigned char *sname = sqlite3_column_text(stmt, 1); student.name = [NSString stringWithUTF8String:(const char *)sname]; // 获得第2列的age student.age = sqlite3_column_int(stmt, 2); // 加入到数组 [students addObject:student]; } } else { NSLog(@"查询语句非法"); } return students; } @end



    控制器中使用工具类为tableView提供数据源,而且监听搜索框的模糊查询 
    //
    //  ViewController.m
    //  2_数据库工具类封装
    //
    //  Created by xss on 14-11-2.
    //  Copyright (c) 2014年 beyond. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "Student.h"
    #import "StudentDAO.h"
    @interface ViewController ()<UISearchBarDelegate>
    // 从数据库中返回的封装好的对象数组,为tableView提供数据源
    @property (nonatomic, strong) NSArray *students;
    @end
    
    @implementation ViewController
    
    #pragma mark - 懒载入
    - (NSArray *)students
    {
        if (_students == nil) {
            _students = [StudentDAO students];
        }
        return _students;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // 加入一个搜索框,并设置代理
        UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
        searchBar.delegate = self;
        // headView
        self.tableView.tableHeaderView = searchBar;
    }
    
    #pragma mark - 搜索框代理
    - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
    {
        // 直接覆盖原来的对象数组
        self.students = [StudentDAO studentsWithCondition:searchText];
        // 刷新表格
        [self.tableView reloadData];
    }
    
    #pragma mark - tableView代理方法
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return self.students.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // 1.创建cell
        static NSString *ID = @"student";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        }
        
        // 2.设置cell的数据
        Student *stu = self.students[indexPath.row];
        cell.textLabel.text = stu.name;
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", stu.age];
        
        return cell;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [self.view endEditing:YES];
    }
    @end
    
















    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    AngularJS各种'service'的区别
    js 斐波那契数列实现
    Mac下Apache+MySQL+PHP开发环境安装过程
    原生封装的js前端工具库(包含了jquery的常用功能)
    BFC 神奇背后的原理
    CSS清浮动处理(Clear与BFC)
    JavaScript实现 页面滚动图片加载(懒加载)
    CodeForce 814B
    排序算法
    uva1610
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4865134.html
Copyright © 2011-2022 走看看