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


复制代码














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





本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/4865134.html,如需转载请自行联系原作者


相关文章
|
2月前
|
API 数据库 C语言
【C/C++ 数据库 sqlite3】SQLite C语言API返回值深入解析
【C/C++ 数据库 sqlite3】SQLite C语言API返回值深入解析
175 0
|
3月前
|
存储 监控 安全
内网屏幕监控软件的数据存储与管理:使用SQLite数据库保存监控记录和配置信息
在当今数字化时代,安全和监控在企业和组织中变得至关重要。内网屏幕监控软件作为一种关键工具,帮助组织监视员工的活动并确保信息安全。这种软件不仅需要高效地记录和管理监控数据,还需要能够方便地进行配置和调整。本文将讨论如何使用SQLite数据库来保存监控记录和配置信息,并介绍如何通过自动化机制将监控到的数据提交到指定网站。
187 2
|
2天前
|
数据库 数据库管理 关系型数据库
|
6天前
|
SQL 存储 数据库连接
C#SQL Server数据库基本操作(增、删、改、查)
C#SQL Server数据库基本操作(增、删、改、查)
7 0
|
15天前
|
SQL 存储 Ubuntu
在ubuntu中将dict.txt导入到数据库sqlite3
这样,你就成功将 `dict.txt` 中的数据导入到名为 `mydatabase.db` 的SQLite3数据库中的 `words` 表格中了。请根据实际情况调整表格结构和数据导入命令。
18 0
|
16天前
|
关系型数据库 MySQL 数据库
【MySQL探索之旅】数据库的基本操作
【MySQL探索之旅】数据库的基本操作
|
26天前
|
SQL 数据库 数据库管理
Python数据库操作(SQLAlchemy、SQLite等)面试题集
【4月更文挑战第15天】本文介绍了Python数据库操作的面试重点,涵盖SQLAlchemy ORM和SQLite。内容包括:1) 使用SQLAlchemy定义SQLite表的Python类及执行CRUD操作,强调ORM使用和会话管理;2) 查询优化与性能,涉及JOIN、分组、聚合查询,并提醒注意懒加载和索引创建;3) 异常处理和事务管理,展示如何捕获异常并进行事务控制。通过理解这些知识点并避免常见错误,可在面试中表现出色。
26 0
|
1月前
|
SQL 关系型数据库 数据库
Python中SQLite数据库操作详解:利用sqlite3模块
【4月更文挑战第13天】在Python编程中,SQLite数据库是一个轻量级的关系型数据库管理系统,它包含在一个单一的文件内,不需要一个单独的服务器进程或操作系统级别的配置。由于其简单易用和高效性,SQLite经常作为应用程序的本地数据库解决方案。Python的内置sqlite3模块提供了与SQLite数据库交互的接口,使得在Python中操作SQLite数据库变得非常容易。
|
1月前
|
关系型数据库 MySQL 数据库连接
Python+SQLite数据库实现服务端高并发写入
Python中使用SQLite内存模式实现高并发写入:创建内存数据库连接,建立表格,通过多线程并发写入数据。虽然能避免数据竞争,但由于SQLite内存模式采用锁机制,可能在高并发时引发性能瓶颈。若需更高性能,可选择MySQL或PostgreSQL。
38 0
|
1月前
|
SQL 存储 数据库
数据库的基本操作
数据库的基本操作