SQLite常用函数及语句
SQLite3.0使用的是C的函数接口,常用函数如下:
sqlite3_open() //打开数据库
sqlite3_close() //关闭数据库
sqlite3_exec() //执行sql语句,例如创建表
sqlite3_prepare_v2() //编译SQL语句
sqlite3_step() //执行查询SQL语句
sqlite3_finalize() //结束sql语句
sqlite3_bind_text() //绑定参数
sqlite3_column_text() //查询字段上的数据
创建数据库表
sqlite3 *sqlite = nil;
NSString *filePath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/%@",@"data.sqlite"];
int result = sqlite3_open([filePath UTF8String],&sqlite);
if (result != SQLITE_OK) {
NSLog(@"打开数据失败");
}
//创建表的SQL语句
NSString *sql = @"CREATE TABLE UserTable (userId text NOT NULL PRIMARY KEY UNIQUE,username text, age integer)";
char *error;
//执行SQL语句
result = sqlite3_exec(sqlite,[sql UTF8String],NULL, NULL, &error);
if (result != SQLITE_OK) {
NSLog(@"创建数据库失败,%s",erorr);
}
sqlite_close(sqlite);
插入数据
sqlite3 *sqlite = nil;
sqlite3_stmt = *stmt = nil;
NSString *filePath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/%@",@"data.sqlite"];
int result = sqlite3_open([filePath UTF8String],&sqlite);
if (result = SQLITE_OK) {
NSLog(@"打开数据失败");
}
NSString *sql = @"INSERT INTO UserTable(userId,userName,age) VALUES(?,?,?)";
sqlite3_prepare_v2(sqlite, [sql UTF8String], -1, &stmt ,NULL);
NSString *userId = @"1002";
NSString *username = @"张三";
int age = 3;
//往SQL中填充数据
sqlite3_bind_text(stmt, 1, [userId UTF8String], -1, NULL);
sqlite3_bind_text(stmt, 2, [userName UTF8String], -1,NULL);
sqlite3_bind_int(stmt, 3, age);
result = sqlite3_step(stmt);
if (result == SQLITE_ERROR || result == SQLITE_MISUSE) {
NSLog(@"执行SQL语句失败");
return NO;
}
sqlite3_finalize(stmt);
sqlite3_close(sqlite);
查询数据
sqlite3 *sqlite = nil;
sqlite3_stmt *stmt = nil;
NSString *filePath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/%@",@"data.sqlite"];
int result = sqlite3_open([filePath UTF8String],&sqlite);
if (result != SQLITE_OK) {
NSLog(@"打开数据失败");
}
NSString *sql = @"SELECT userId, userName,age FROM UserTable WHERE age >?";
sqlite3_prepare_v2(sqlite, [sql UTF8String], -1,&stmt, NULL);
int age = 1;
sqlite3_bind_int(stmt, 1,age);
result = sqlite3_step(stmt);
//循环遍历查询后的数据列表
while (result == SQLITE_ROW) {
char *userid = (char*)sqlite3_column_text(stmt,0);
char *username = (char*)sqlite3_column_text(stmt,1);
int age = sqlite3_column_int(stmt,2);
NSString *userId = [NSString stringWithCString:userid encoding:NSUTF8StringEncoding];
NSString *userName = [NSString stringWithCString:username encoding:NSUTF8StringEncoding];
NSLog(@"-----用户名:%@,用户id:%@,年龄:%d---",userName,userId,age);
result = sqlite3_step(stmt);
}
sqlite3_finalize(stmt);
sqlite3_close(sqlite);
SQLite常用函数及语句的更多相关文章
- Sqlite 常用函数推荐
Sqlite 常用函数 1 .打开数据库: 说明:打开一个数据库,文件名不一定要存在,如果此文件不存在, sqlite 会自动创建.第一个参数指文件名,第二个参数则是定义的 sqlite3 ** 结构 ...
- SQLite 常用函数
SQLite 常用函数 参考: SQLite 常用函数 | 菜鸟教程http://www.runoob.com/sqlite/sqlite-functions.html SQLite 常用函数 SQL ...
- Python常用函数--return 语句
在Python教程中return 语句是函数中常用的一个语句.return 语句用于从函数中返回,也就是中断函数.我们也可以选择在中断函数时从函数中返回一个值.案例(保存为 function_retu ...
- SQL注入的常用函数和语句
1.系统函数 version() Mysql版本user() 数据库用户名database() 数据库名@@datadir 数据库路径@@version_compile_os 操 ...
- MySQL 常用函数和语句笔记
CONCAT()函数 CONCAT()函数代表着字符串的链接,例子有 SELECT COUNT(1) FROM ums_commodity WHERE 1 = 1 and deleted=0 and ...
- SQLite常用函数
length(column_name) 取得相应栏位的长度 substr(column_name, start, length) 截取某个栏位相应长度的值
- sqlite 常用的一些语句
转载:https://blog.csdn.net/qq_25221835/article/details/82768375 转载:https://blog.csdn.net/qq_35449730/a ...
- SQLite进阶-19.常用函数
目录 SQLite常用函数 SQLite常用函数 SQLite 有许多内置函数用于处理字符串或数字数据. 序号 函数 & 描述 1 SQLite COUNT 函数SQLite COUNT 聚集 ...
- iOS开发数据库篇—SQLite常用的函数
iOS开发数据库篇—SQLite常用的函数 一.简单说明 1.打开数据库 int sqlite3_open( const char *filename, // 数据库的文件路径 sqlite3 * ...
随机推荐
- 单KEY业务,数据库水平切分架构实践
本文将以"用户中心"为例,介绍"单KEY"类业务,随着数据量的逐步增大,数据库性能显著降低,数据库水平切分相关的架构实践: 如何来实施水平切分 水平切分后常见的 ...
- mybatis用spring的动态数据源实现读写分离
一.环境: 三个mysql数据库.一个master,两个slaver.master写数据,slaver读数据. 二.原理: 借助Spring的 AbstractRoutingDataSource 这个 ...
- laravel5.5 when()的用法
当你在使用where语句有前提条件时,比如某值为1的时候才执行where子句,否则不执行,这个时候,laravel5.5新出了一个简便方法when($arg,fun1[,fun2]). 具体用法如下: ...
- Servlet--HttpServletResponse的2个操作流的方法
前面已经说过无数多次了,我们的项目都是基于HTTP协议的一次请求,一次响应.实际编码中,我们在处理完逻辑后一般是跳转到一个页面上,或者用输出流返回json字符串.其实跳转到一个页面往往也就是JSP,J ...
- Android4.0新控件
谷歌在推出Android4.0的同时推出了一些新控件,Android4.0中最常用的新控件有下面5种. 1. Switch的使用 Switch顾名思义,就是开关的意思,有开和关两种状态. 当Swit ...
- java类路径classpath和包
*/ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...
- thinkphp5踩坑之部署到服务器模板不存在
一个项目部署到Linux服务器上去的时候,发现某些模板竟然会报错说"模板不存在:/Application/Admin/-.", 解决方法:网上有说是因为使用$this->fe ...
- 【转】利用matlab生成随机数函数
原文地址:利用matlab生成随机数函数 rand(n):生成0到1之间的n阶随机数方阵 rand(m,n):生成0到1之间的m×n的随机数矩阵 (现成的函数) betarnd:贝塔分布的随机数生成 ...
- JDBC访问及操作SQLite数据库
SQLite 是一个开源的嵌入式关系数据库,其特点是高度便携.使用方便.结构紧凑.高效.可靠. 与其他数据库管理系统不同,SQLite 的安装和运行非常简单,在大多数情况下,只要确保SQLite的二进 ...
- 洛谷 [P2766] 最长不下降子序列问题
啊啊啊,再把MAXN和MAXM搞反我就退役 层次图求不相交路径数 第一问简单DP 第二问想办法把每一个不上升子序列转化成DAG上的一条路径,就转换成了求不相交路径数 因为每一个数只能用一次,所以要拆点 ...