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. JavaScript 教程:对象

    JavaScript 对象是拥有属性和方法的数据.学过编程语言的都知道,此处不再详述! 1.对象的定义: <script> </script> 对象也可以先创建,再添加属性和属 ...

  2. 译-Web Service剖析: XML, SOAP 和WSDL 用于独立于平台的数据交换

    本文是翻译内容,原文参见: Anatomy of a Web Service: XML, SOAP and WSDL for Platform-independent Data Exchange We ...

  3. 重定向stdin stdout stderr |

    在Linux下,当一个用户进程被创建的时候,系统会自动为该进程创建三个数据 流,也就是题目中所提到的这三个.那么什么是数据流呢(stream)? 我们知道,一个程序要运行,需要有输入.输出,如果出错, ...

  4. Sql Server的艺术(七) SQL 数据插入操作

    --用INSERT插入单行数据 在SQL中,可以通过INSERT...VALUES语句直接向数据库表中插入数据.可以整行,也可以部分列. 基本语法: INSERT INTO table_name [c ...

  5. Win和Linux查看端口和杀死进程

    title: Win和Linux查看端口和杀死进程 date: 2017-7-30 tags: null categories: Linux --- 本文介绍Windows和Linux下查看端口和杀死 ...

  6. AppScan扫描结果分析及工具栏使用

    Appscan的窗口大概分三个模块,Application Links(应用链接), Security Issues(安全问题), and Analysis(分析) Application Links ...

  7. Oracle中的游标

    Oracle游标 概念:内存中的一块区域,存放select结果 游标用来处理从数据库中检索的多行记录(使用SELECT语句).利用游标,程序可以逐个地处理和遍历一次检索返回的整个记录集.一.显示游标( ...

  8. Django之反向生成url

    首先新建一个项目test_url,项目包含一个名为app01的应用 在urls.py文件中生成如下内容 from django.conf.urls import url from django.sho ...

  9. python小练习(自己瞎倒腾)

    python小练习 在网上无意中看到一个问题,心血来潮写了写,觉得比较有意思,以后遇到这种有意思的小练习也记录下. #!/usr/bin/env python # -*- coding:utf-8 - ...

  10. ABAP更换请求

    当创建的程序或表操作失误存储在其他的请求下边如何更换请求呢? 事务代码:SE09 双击请求号,复制存储错误的对象 打开一个新窗口,双击正确的请求,点击修改,将复制的对象粘贴在正确的请求下 将错误的请求 ...