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 * ...
随机推荐
- 第一个简单的maven项目
学习一个新的东西,最快的方式就是实践.所以我们也不用多说什么了,直接拿一个项目来练手.下面的整理取自maven权威指南,在一堆maven资料中,我觉得这本书写的最好. 简介 我们介绍一个用Maven ...
- 在Tomcat中采用基于表单的安全验证
.概述 (1)基于表单的验证 基于From的安全认证可以通过TomcatServer对Form表单中所提供的数据进行验证,基于表单的验证使系统开发者可以自定义用户的登陆页面和报错页面.这种验证方法 ...
- docker搭建私服
拉registry镜像 假设在192.168.100.17服务器上搭建私服 docker pull registry docker run -d -v /data/docker/registry:/v ...
- Halcon一日一练:Halcon异常判断方法
1.TryCatch tryCatch处理的方式如下: try *可能会出现错误的语句 .... catch(Exception) *获取错误代码 ErrorCode:=Exception[] **对 ...
- CURL处理POST、GET请求
Curl是一个库,它允许你通过各种协议和各种不同的服务器进行连接和通讯 a.php <?php function curlRequest($url,$data=''){ $ch=curl_ini ...
- awk空行合并
[root@localhost ~]#cat urfile [DEFAULT] key1=value1 key2=value2 key3=value3 [agent] key1=value1 key2 ...
- PHP date()函数详解
date (PHP 4, PHP 5) date - 格式化一个本地时间/日期 说明¶ string date ( string $format [, int $timestamp ] ) 返回将整数 ...
- java存放数据的5个地方
1.寄存器:最快的存储区,位于处理器内部,但是寄存器的数量极其有限,所以寄存器根据需求进行分配,你不 能直接控制,也不能在程序中感觉到寄存器存在的任何迹象.(C/C+允许向寄存器建议寄存器配, 但它不 ...
- R语言-单一变量分析
R语言简介: R语言是一门专用于统计分析的语言,有大量的内置函数和第三方库来制作基于数据的表格 准备工作 安装R语言 https://cran.rstudio.com/bin/windows/base ...
- 使用hexo搭建个人博客
安装前提 node.js git 如果缺少以上条件,则前往相应的官网下载安装即可.. 安装hexo $ npm install hexo-cli -g 待安装完成后,执行相关命令查看hexo的信息. ...