iOS开发:sqlite的简单实用

来源:互联网 发布:电脑游戏用什么软件 编辑:程序博客网 时间:2024/06/05 19:06

一、SQLite简介

  • 数据库有两个大类:关系型数据库(主流)和对象型数据库
  • SQLite(关系型数据库)是轻型的嵌入式数据库,在移动端是主流数据库
  • 优点:占用资源少,在嵌入式设备中,可能只需要几百k 的内存,它的处理速度比著名的Mysql、PostgreSQL数据库都要快
  • 存储结构是表(table),列:字段,行:记录
  • 推荐一款简单实用的数据库管理软件:Navicat

二、SQLite的方法

    sqlite3 *db,       数据库句柄

    sqlite3_stmt *stmt, 这个相当于ODBCCommand对象,用于保存编译好的SQL语句

    sqlite3_open(),     打开数据库,没有数据库时创建

    sqlite3_exec(),     执行非查询的sql语句

    Sqlite3_step(),     在调用sqlite3_prepare后,使用这个函数在记录集中移动

    Sqlite3_close(),    关闭数据库文件

    sqlite3_column_text(),  text类型的数据

    sqlite3_column_blob(),  blob类型的数据

    sqlite3_column_int(),   int类型的数据

    sqlite3_bind_blob(),    绑定,后面的数据类型可选择

三、SQLite示例代码

////  ViewController.m//  SQLiteDemo////  Created by god on 16/4/12.//  Copyright © 2016年 wakeup. All rights reserved.//#import "ViewController.h"#import <sqlite3.h>@interface ViewController (){    sqlite3 *_dataBase;   //用于指向数据库的指针}- (void)openDataBase;- (void)closeDataBase;- (void)createTable;- (void)createDatas;- (void)deleteDatas;- (void)updateDatas;- (void)queryDatas;@end@implementation ViewController#pragma mark *** Life Cycle ***- (void)viewDidLoad {    [super viewDidLoad];        //此处调用处理逻辑测试}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];}#pragma mark *** SQLite3 Methode ***- (void)openDataBase {        NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];    NSString *companyPath = [documentPath stringByAppendingString:@"company.sqlite"];    /**     *  打开数据库,并建立数据库的连接,如果路径下不存在文件,则创建     *  第一个参数:数据库的存储路径     *  第二个参数:指向数据库的指针,用于保存对数据库的引用     */    int result = sqlite3_open(companyPath.UTF8String, &_dataBase);    if (result == SQLITE_OK) {        NSLog(@"打开成功");    }else {        NSLog(@"打开失败");    }}- (void)closeDataBase {    int result = sqlite3_close(_dataBase);    if (result == SQLITE_OK) {        NSLog(@"关闭成功");    }else {        NSLog(@"关闭失败");    }}- (void)createTable {    char *sql = "create table if not exists t_emplyee(id integer primary key autoincrement, name text, age integer)";    /**     *  执行sql语句     *  @param 数据库指针     *  @param sql语句     *  @param callback 当有返回值的时候执行的回调函数     *  @param 要传入回调函数的第一个参数的值     *  @param 保留错误的指针地址     *  @return BOOL值,判断是否执行成功     */    char *error = NULL;    int result = sqlite3_exec(_dataBase, sql, NULL, NULL, &error);    if (result == SQLITE_OK) {        NSLog(@"创建表成功");    }else {        NSLog(@"创建表失败:%s", error);    }}- (void)createDatas {    char *sql = "insert into t_emplyee (name, age) values (?, ?);";    /**     *  编译sql语句,检查sql语句的合法性     *     *  @param db#>     数据库指针 description#>     *  @param zSql#>   sql语句 description#>     *  @param nByte#>  要编译的社会语句的字节数,如果传入负数自动计算长度 description#>     *  @param ppStmt#> 存储编译好的sql语句 description#>     *  @param pzTail#> 用来存储未编译的sql语句 description#>     *     *  @return 编译是否成功     */    sqlite3_stmt *stmt = NULL;    int result = sqlite3_prepare(_dataBase, sql, -1, &stmt, NULL);    if (result == SQLITE_OK) {        NSLog(@"编译成功");    }else {        NSLog(@"编译失败");        return;    }    /**     *  给sql绑定参数值     *     *  @param stmt description#>     *  @param 第几个问号 description#>     *  @param 要传入的值 description#>     *  @param 要传入的长度,若为负数自动计算  description#>     *  @param 用来指定销毁的函数     *     *  @return     */    static int i = 0;    NSString *nameStr = [NSString stringWithFormat:@"zhangsan%d", i];    int age = 18 + i;    i ++;    sqlite3_bind_text(stmt, 1, nameStr.UTF8String, -1, SQLITE_TRANSIENT);    sqlite3_bind_int(stmt, 2, age);        //执行sql    int stepResult = sqlite3_step(stmt);    if (stepResult == SQLITE_DONE) {        NSLog(@"插入完毕");    }else {        NSLog(@"插入失败");    }}- (void)deleteDatas {    char *sql = "delete from t_emplyee where name = 'zhangsan4'";    int result = sqlite3_exec(_dataBase, sql, NULL, NULL, NULL);    if (result == SQLITE_OK) {        NSLog(@"删除成功");    }}- (void)updateDatas {    char *sql = "update t_emlyee set name = 'lisi' where id = 1;";    int result = sqlite3_exec(_dataBase, sql, NULL, NULL, NULL);    if (result == SQLITE_OK) {        NSLog(@"修改成功");    }}- (void)queryDatas {    char *sql = "select * from t_emplyee;";    sqlite3_stmt *stmt = NULL;    sqlite3_prepare(_dataBase, sql, -1, &stmt, NULL);        NSMutableArray *array = [@[] mutableCopy];    //逐行执行    while (sqlite3_step(stmt) == SQLITE_ROW) {                //从stmt中取数据,第二个参数是某一列                char *name = (char *)sqlite3_column_text(stmt, 1);        int age = sqlite3_column_int(stmt, 2);                NSDictionary *dic = @{@"name":[NSString stringWithUTF8String:name], @"age":@(age)};        [array addObject:dic];    }    NSLog(@"查询结果:%@", array);}@end


0 0
原创粉丝点击