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常用函数及语句的更多相关文章

  1. Sqlite 常用函数推荐

    Sqlite 常用函数 1 .打开数据库: 说明:打开一个数据库,文件名不一定要存在,如果此文件不存在, sqlite 会自动创建.第一个参数指文件名,第二个参数则是定义的 sqlite3 ** 结构 ...

  2. SQLite 常用函数

    SQLite 常用函数 参考: SQLite 常用函数 | 菜鸟教程http://www.runoob.com/sqlite/sqlite-functions.html SQLite 常用函数 SQL ...

  3. Python常用函数--return 语句

    在Python教程中return 语句是函数中常用的一个语句.return 语句用于从函数中返回,也就是中断函数.我们也可以选择在中断函数时从函数中返回一个值.案例(保存为 function_retu ...

  4. SQL注入的常用函数和语句

    1.系统函数 version()   Mysql版本user()   数据库用户名database()    数据库名@@datadir   数据库路径@@version_compile_os   操 ...

  5. MySQL 常用函数和语句笔记

    CONCAT()函数 CONCAT()函数代表着字符串的链接,例子有 SELECT COUNT(1) FROM ums_commodity WHERE 1 = 1 and deleted=0 and ...

  6. SQLite常用函数

    length(column_name) 取得相应栏位的长度 substr(column_name, start, length) 截取某个栏位相应长度的值

  7. sqlite 常用的一些语句

    转载:https://blog.csdn.net/qq_25221835/article/details/82768375 转载:https://blog.csdn.net/qq_35449730/a ...

  8. SQLite进阶-19.常用函数

    目录 SQLite常用函数 SQLite常用函数 SQLite 有许多内置函数用于处理字符串或数字数据. 序号 函数 & 描述 1 SQLite COUNT 函数SQLite COUNT 聚集 ...

  9. iOS开发数据库篇—SQLite常用的函数

    iOS开发数据库篇—SQLite常用的函数 一.简单说明 1.打开数据库 int sqlite3_open( const char *filename,   // 数据库的文件路径 sqlite3 * ...

随机推荐

  1. scrapy_移除内容中html标签

    如何移除所获取内容中多余的html标签? 通过w3lib模块和re模块 #!/usr/bin/python3 # -*- coding: UTF-8 -*- __author__ = 'beimenc ...

  2. scrapy_随机user-agent

    什么是user-agent? 用户代理,服务器识别用户的操作系统,浏览器类型和渲染引擎,不同浏览器的user-agent是不同的 如何随机更改user-agent? 1. 在setting中添加use ...

  3. 企业级分布式存储应用与实战-mogilefs实现

    Mogilefs是什么 MogileFS是一个开源的分布式文件存储系统,由LiveJournal旗下的Danga Interactive公司开发.Danga团队开发了包括 Memcached.Mogi ...

  4. linkin大话设计模式--策略模式

    linkin大话设计模式--策略模式 Strategy [ˈstrætədʒi]  策略 策略模式用于封装系列的算法,这些算法通常被封装在一个称为Context的类中,客户端程序可以自由的选择任何一种 ...

  5. form表单中enctype属性作用

    上传文件时,提交的表单属性里需要加enctype="multipart/form-data",才能提交文件信息,不然会报错.那么enctype属性的作用是什么?就是设置表单传输的编 ...

  6. 面向对象之原型——challenge

    面向对象之原型 object-oriented面向对象的设计,不同于其他语言,js中的面向对象没有类的概念,因此,其对象也有些特殊. 所谓对象就是无序属性的集合,其属性可以包含基本值.对象.函数.也就 ...

  7. jQuery的Nicescroll滚动条插件使用方法

    Nicescroll滚动条插件是一个非常强大的基于jQuery的滚动条插件,不需要增加额外的css,几乎全浏览器兼容.ie6+,实现只需要一段代码,侵入性非常小,样式可完全自定义,支持触摸事件,可在触 ...

  8. 小白成长系列--HTTP协议(一)

    序:小白成长系列是笔者使用最简单易懂的逻辑来解释常见的计算机相关知识,不仅理解,还让你记忆深刻\(^o^)/ 先理解什么是协议? 协议就是双方要做某件事情而制定的规则,而且双方必须要遵从协议所约定的内 ...

  9. 通过 ['1', '2', '3'].map(parseInt) 学习 map 和 parseInt 函数

    看到一道笔试题: ['1', '2', '3'].map(parseInt) 这道题目中涉及到 map 和 parseInt 函数的运用,如果对这两个函数的理解不充分的话,是很难思考出正确的结果的. ...

  10. Java在已存在的pdf文件中生成文字和图片--基础

    自我总结,有什么不足之处请告知,感激不尽!下一次总结pdf模板映射生成报表(应对多变的pdf报表需求,数据提供和报表生成解耦). 目的:在给定的pdf模板上生成报表,就需要知道最基本的操作:文字添加, ...