zoukankan      html  css  js  c++  java
  • iOS开发-sqlite3使用

    SQLite3使用

    SQLite简介

    SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中。

    SQLite3

    在XCode工程中,打开targets,在Build Phases下导入Libsqlite.tbd,在需要使用sqlite3的位置导入头文件即可.

    生成路径

    +(NSString *)path{
    
    NSArray *documentArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString *documentPath = [documentArr firstObject];
    // crylown.db 为数据库的名字
    NSString *path = [NSString stringWithFormat:@"%@/crylown.db",documentPath];
    
    return path;
    }

    创建/打开数据库

    sqlite3 *database;
    
    int databaseResult = sqlite3_open([[self path] UTF8String], &database);
    
    if (databaseResult != SQLITE_OK) {
    
        NSLog(@"创建/打开数据库失败,%d",databaseResult);
    }

    创建表

         char *error;
    
    //    建表格式: create table if not exists 表名 (列名 类型,....)    注: 如需生成默认增加的id: id integer primary key autoincrement
        const char *createSQL = "create table if not exists list(id integer primary key autoincrement,name char,sex char)";
    
        int tableResult = sqlite3_exec(database, createSQL, NULL, NULL, &error);
    
        if (tableResult != SQLITE_OK) {
    
            NSLog(@"创建表失败:%s",error);
        }

    添加数据

    // 对SQL语句执行预编译
    int sqlite3_prepare(sqlite3 *db, const char *sql,int byte,sqlite3_stmt **stmt,const char **tail)
    • 1.db代表打开的数据库连接
    • 2.sql代表的sql语句
    • 3.byte代表SQL语句的最大长度
    • 4.传出参数,指向预编译SQL语句产生的sqlite3_stmt
    • 5.指向SQL语句中未使用的部分

    int sqlite3_prapare_v2()版本,代表该函数的最新版本。

        //   添加
        //   sql语句格式: insert into 表名 (列名)values(值)
            const char *insertSQL = "insert into haha (name,sex)values('iosRunner','male')";
            int insertResult = sqlite3_prepare_v2(database, insertSQL, -1, &stmt, nil);
    
            if (insertResult != SQLITE_OK) {
                NSLog(@"添加失败,%d",insertResult); 
            }
            else{
        //           执行sql语句
                sqlite3_step(stmt);
            }

    查找数据

    //返回sqlite3_stmt(预编译SQL语句产生的结果)
    const char* sqlite3_colum_int/text...(sqlite3_stmt *,int N)

    根据结果返回的值的类型不同选择int/text等,N代表列名在表中的位置。

    //    查找
    //   sql语句格式: select 列名 from 表名 where 列名 = 参数       注:前面的列名为查询结果里所需要看到的 列名,后面的 列名 = 参数 用于判断删除哪条数据
         const char *searchSQL = "select id,name,sex from haha where name = 'puyun2'";
            int searchResult = sqlite3_prepare_v2(database, searchSQL, -1, &stmt, nil);
            if (searchResult != SQLITE_OK) {
                NSLog(@"查询失败,%d",searchResult); 
            }
            else{
                while (sqlite3_step(stmt) == SQLITE_ROW) {
                // 查询的结果可能不止一条,直到 sqlite3_step(stmt) != SQLITE_ROW,查询结束。
                    int idWord = sqlite3_column_int(stmt, 0);
                    char *nameWord = (char *) sqlite3_column_text(stmt, 1);
                    char *sexWord = (char *)sqlite3_column_text(stmt, 2);
                    NSLog(@"%d,%s,%s",idWord,nameWord,sexWord);
                }
            }

    修改数据

            // 修改
           // sql语句格式: update 表名 set  列名 = 新参数 where 列名 = 参数   注:前面的 列名 = 新参数 是修改的值, 后面的 列名 = 参数 用于判断删除哪条数据
            const char *changeSQL = "update haha set name = 'buhao' where name = 'iosRunner'";
    
            int updateResult = sqlite3_prepare_v2(database, changeSQL, -1, &stmt, nil);
    
            if (updateResult != SQLITE_OK) {
    
                NSLog(@"修改失败,%d",updateResult);
            }
            else{
    
                sqlite3_step(stmt);
            }

    删除数据

    //        删除
    //        sql语句格式: delete from 表名 where 列名 = 参数     注:后面的 列名 = 参数 用于判断删除哪条数据
            const char *deleteSQL = "delete from haha where name = 'iosRunner'";
    
            int deleteResult = sqlite3_prepare_v2(database, deleteSQL, -1, &stmt, nil);
    
            if (deleteResult != SQLITE_OK) {
    
                NSLog(@"删除失败,%d",deleteResult);
    
            }
            else{
                sqlite3_step(stmt);
            }

    结束处理

    //        销毁stmt,回收资源
            sqlite3_finalize(stmt);
    
    //    关闭数据库
        sqlite3_close(database);
  • 相关阅读:
    C# 利用TTS实现文本转语音
    Windows10提示“没有权限使用网络资源”的解决方案
    INSPIRED启示录 读书笔记
    INSPIRED启示录 读书笔记
    phpfpm的配置
    session 的工作原理
    MySQL 事务
    Redis各种数据类型的使用场景
    JavaScript 和Ajax跨域问题
    如何做URL静态化 和页面的静态化
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/7055476.html
Copyright © 2011-2022 走看看