下面是对SQLite函数接口的部分简介:
先看一下SQLite3的错误代码(在安装好SQLite之后,可以从SQLite3.h中找到下面信息):
#define SQLITE_OK 0 /* Successful result */
/* beginning-of-error-codes */
#define SQLITE_ERROR 1 /* SQL error or missing database */
#define SQLITE_INTERNAL 2 /* Internal logic error in SQLite */
#define SQLITE_PERM 3 /* Access permission denied */
#define SQLITE_ABORT 4 /* Callback routine requested an abort */
#define SQLITE_BUSY 5 /* The database file is locked */
#define SQLITE_LOCKED 6 /* A table in the database is locked */
#define SQLITE_NOMEM 7 /* A malloc() failed */
#define SQLITE_READONLY 8 /* Attempt to write a readonly database */
#define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite3_interrupt()*/
#define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */
#define SQLITE_CORRUPT 11 /* The database disk image is malformed */
#define SQLITE_NOTFOUND 12 /* Unknown opcode in sqlite3_file_control() */
#define SQLITE_FULL 13 /* Insertion failed because database is full */
#define SQLITE_CANTOPEN 14 /* Unable to open the database file */
#define SQLITE_PROTOCOL 15 /* Database lock protocol error */
#define SQLITE_EMPTY 16 /* Database is empty */
#define SQLITE_SCHEMA 17 /* The database schema changed */
#define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */
#define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */
#define SQLITE_MISMATCH 20 /* Data type mismatch */
#define SQLITE_MISUSE 21 /* Library used incorrectly */
#define SQLITE_NOLFS 22 /* Uses OS features not supported on host */
#define SQLITE_AUTH 23 /* Authorization denied */
#define SQLITE_FORMAT 24 /* Auxiliary database format error */
#define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */
#define SQLITE_NOTADB 26 /* File opened that is not a database file */
#define SQLITE_ROW 100 /* sqlite3_step() has another row ready */
#define SQLITE_DONE 101 /* sqlite3_step() has finished executing */
几个最常用的函数接口:
1、
int sqlite3_open(const char *filename, sqlite3 **ppDb);
操作数据库的入口函数。
通过该函数可以打开现有的数据库,在数据库不存在的情况下可以新建数据库。
2、
int sqlite3_open16(const void*, sqlite3**);
sqlite3_open16()使用UTF-16编码(使用本地主机字节顺序)传递数据库文件名。如果要创建新数据库, sqlite3_open16()将内部文本转换为UTF-16编码, 反之sqlite3_open() 将文本转换为UTF-8编码。
3、
int sqlite3_close(sqlite3*);
关闭数据库。
4、
int sqlite3_exec(sqlite3*, const char *sql, sqlite_callback, void*, char**);
将SQL语句作为第二个参数传入,可实现对数据库的操作。
下面是一个简单的实现操作数据库的例子:
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
int main (int argc, char ** argv)
{
int result = 0;
sqlite3* db = NULL;
char * errMsg = NULL;
char sql_cmd[200];
memset(sql_cmd, 0x00, sizeof(sql_cmd));
result = sqlite3_open("lester.db", &db);
printf("Hello Lesterk, result = %d\n", result);
char* sqlCreate = "create table LesterData2 (LesterId INTEGER, LesterName Integer);";
result = sqlite3_exec(db, sqlCreate, 0, 0, &errMsg);
printf("Create_Result = %d , Msg = %s \n", result, errMsg);
char* sqlInsert = "INSERT INTO \"LesterData2\" VALUES( 1, 2);";
result = sqlite3_exec(db, sqlInsert, 0, 0, &errMsg);
printf("Insert_Result = %d , Msg = %s \n", result, errMsg);
char* sql = "SELECT * FROM LesterData2";
int nrow = 1, ncolumn = 1;
char **azResult;
result = sqlite3_get_table( db, sql, &azResult, &nrow, &ncolumn, &errMsg);
printf("Select_Result = %d , Msg = %s \n", result, errMsg);
int i = 0;
printf( "row = %d, column=%d \n" , nrow , ncolumn );
printf( "\nThe result of querying is : \n" );
for( i = 0; i < ( nrow + 1 ) * ncolumn ; i++){
printf( "azResult[%d] = %s\n", i , azResult[i] );
}
sqlite3_free_table( azResult );
printf("Hello World\n, %d", result);
sqlite3_close(db);
return 0;
}
建立两个存在外键关系的表:
#include "string.h"
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
int main ()
{
int result = 0;
sqlite3* db = NULL;
char * errMsg = NULL;
char sql_cmd[200];
memset(sql_cmd, 0x00, sizeof(sql_cmd));
result = sqlite3_open("lester.db", &db);
char* sqlCreate = "create table LesterTable1 (LesterId PRIMARY KEY, LesterNum Integer, LesterName Integer);";
result = sqlite3_exec(db, sqlCreate, 0, 0, &errMsg);
sqlCreate ="create table LesterTable2 (Lester2Id PRIMARY KEY, Lester2Name Integer, foreign2 Integer, FOREIGN KEY(foreign2) REFERENCES LesterTable1(LesterId));";
result = sqlite3_exec(db, sqlCreate, 0, 0, &errMsg);
sqlite3_close(db);
return 1;
}
分享到:
相关推荐
在Windows操作系统下,如果想通过C++调用SQLite开发的时候,会用到资源中的内容。 共包括以下五个文件:sqlite3.lib sqlite3.h sqlite3.dll sqlite3.def mspdb60.dll 相关调用调用步骤和使用方法可见如下内容: ...
用VC++6.0开发的基于对话框的应用程序,可以编译运行。目录下有一个名为student.db的文件,...这个代码说明了怎么用C/C++连接SQLite数据库文件及进行基本的操作,比如插入新的数据,查询数据。对学习SQLite数据库有帮助
int MYDLL open_db(); int MYDLL create_table(); int MYDLL drop_table(); int MYDLL insert_data(int id, char *name, int age); int MYDLL search_data(int id); int MYDLL search_data(char *name);...
将sqlite的简单功能(execsql, query)封闭到dll中.可通过标准c函数接口操作. 以前写过一个简单的类.可以供c/c++调用. 现在写了一个dll可以供其它语言环境使用. 代码中预留了其它数据库接口的扩展性.
在运行中自动释放SQLite.Interop.dll并根据当前运行进程位数进行调用。 支持项目生成时选择为AnyCPU(不受32位系统与64位系统的限制) 已完善为自动检测当前系统位数,本次程序运行只释放对应位数的SQLite.Interop....
在Sqlite官网上下载下来的最新版System.Data.SQLite.dll与SQLite.Interop.dll(X85与X64)需要安装安装对应的Microsoft Visual C++ 2005 Service Pack 1 Redistributable Package(X64与X86)才能正常调用,这样给...
SQLite3 的类库,已经转了格式,可以在c++builder中使用, 使用BCB 2010通过。放在这里留个备份。
sqlite3开发动态库,包含x86和x64的lib和dll,亲测win10和win7 X64系统可用,内附c/c++调用接口说明样例。
sqlite3C++版动态库和头文件,可直接调用。
System.Data.SQLite.Dll C++例子
c++调用sqlite3需要的三个文件,分别是sqlite3.dll sqlite3.h sqlite3.lib
PowerBuilder9.0 调用sqlite3数据库 自己写的一个小文件,学习研究用。 放在这里留个纪念。 http://blog.csdn.net/aasmfox/article/details/8001880
这个C程序的例子显示怎么使用sqlite的C/C++接口。数据库的名字由第一个参数取得,第二个参数是一条或更多的SQL执行语句。这个程序调用sqlite3_open()打开指定的数据库,调用sqlite3_exec()对数据库执行SQL语句,...
SQLite3是一个桌面数据库,可以使用C或者C++方便的调用,使用起来很简单。
VC2015操作SQLITE3数据库及MYSQL数据库,SQL SERVER数据库,并且是对SQLITE3加密
SQL语句是SQL操作的指令,我们用C/C++访问数据库时,需要用char*即C字符串来保存SQL语句,然后调用相应sqlite3库的函数,传入C字符串,来执行SQL指令。 常用术语:表(table)、字段(column,列,属性)、记录(row,...
在Sqlite官网上下载下来的最新版System.Data.SQLite.dll与SQLite.Interop.dll(X85与X64)需要安装安装对应的Microsoft Visual C++ 2005 Service Pack 1 Redistributable Package(X64与X86)才能正常调用,这样给...
工程属性->C/C++->常规->附加包含目录->放入工程文件的路径;工程属性->链接器->常规->附加库目录->放入工程文件的路径;工程属性->链接器->输入->附加依赖项->输入sqlite3.lib,然后再向工程加入头文件和源文件。
cgi c语言中sqlite3的使用,调用sqlite3的api完成数据库的操作。
并用C语言格式导出C接口供C语言的调用; 即本程序是C语言、C++兼容 本程序所使用的Makefile文件由本人创作(注释详细),可支持动态库编译、静态库编译等等,经过长时间优化积累,可通过简单修改参数适用于绝大部分...