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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值