Sqlite使用手册
1. 概述SQLite是一个开源的嵌入式关系数据库,SQLite可移植性好,很容易使用,很小,高效而且可靠,占用资源少。 嵌入式数据库的一大好处就是在你的程序内部不需要网络配置,也不需要管理。因为客户端和服务器在同一进程空间运行。SQLite 的数据库权限只依赖于文件系统,没有用户帐户的概念。SQLite 有数据库级锁定,没有网络服务器。它需要的内存,其它开销很小,适合用于嵌入式设备。你需要做的仅仅是把它正确的编译到你的程序。 2. 术语Sql语句: 用来操作已打开的数据库的操作语句 3. 常用API使用说明3.1 所需头文件
3.2 常见API3.2.1 打开数据库原型: 3.2.2 关闭数据库原型: 3.2.3 执行sql语句原型: 3.2.4 编译sql语句的声明原型: 3.2.5 表格行的步进原型: 3.2.6 获取该行的某列的int型数据原型: 3.2.7 获取该行的某列的text型数据原型: 3.2.8 获取该行的某列的blob型数据原型: 3.2.9 获取该行的某列的double型数据原型: 3.2.10 释放一个已准备的声明的类原型: 4. 使用实例#include <sqlite3.h> #include <stdio.h> #include <string.h> //回调函数 int ListTablesName_callback(void *pDB,int argc,char **argv,char **azColName) { int ret; char pSqlQuerySel[128] = "select * from "; char *pError = NULL; sqlite3_stmt *pStmt = NULL; strcat(pSqlQuerySel,argv[0]); fprintf(stdout,"%sn",pSqlQuerySel); //准备读取数据 ret = sqlite3_prepare((sqlite3 *)pDB,pSqlQuerySel,strlen(pSqlQuerySel),&pStmt,(const char**)&pError); if (ret != SQLITE_OK) { fprintf(stderr,"sqlite3_prepare failn"); return -1; } while (1) { ret = sqlite3_step(pStmt); //移动记录集 if (ret != SQLITE_ROW) break; fprintf(stdout,"%d ",sqlite3_column_int(pStmt,0)); fprintf(stdout,"%s n",sqlite3_column_text(pStmt,1)); } return 0; } int main() { int ret = 0; sqlite3 *pDB = NULL; sqlite3_stmt *pStmt = NULL; char *pError = NULL; char *pListTablesNameSel = (char*)"SELECT name FROM sqlite_master";//列出所以表名语句 //打开文件 ret = sqlite3_open("test.db",&pDB); if (ret != SQLITE_OK) { fprintf(stderr,"can't open database : %sn",sqlite3_errmsg(pDB)); sqlite3_close(pDB); return -1; } //查找源文件中所有表名 ret = sqlite3_exec(pDB,pListTablesNameSel,ListTablesName_callback,pDB,&pError); if (ret != SQLITE_OK) { fprintf(stderr,"list table count error: %s",sqlite3_errmsg(pDB)); sqlite3_close(pDB); return -1; } return 0; } 编译:g++ example.cpp –lsqlite3 原文链接 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |