首页 技术 正文
技术 2022年11月19日
0 收藏 534 点赞 4,068 浏览 2719 个字

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);
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,105
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,582
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,429
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919