1、打开数据库:sqlite3_open
原型:
int sqlite3_open(
const char *filename, /* Database filename (UTF-8) */
sqlite3 **ppDb /* OUT: SQLite db handle */
);
函数功能:打开或创建数据库
参数说明:filename -- 数据库文件名(相对或绝对路径),
如果该文件不存在,则该函数会创建该数据库文件并打开
如果该文件存在,则打开它
ppDb--返回的数据库句柄,所有与该数据库相关的操作都要基于该句柄来执行
返回值:SQLITE_OK(SQLITE_OK的值为0)则表示操作正常
2、关闭数据库:sqlite3_close
原型:
int sqlite3_close(sqlite3 *ppDb);
ppDb--数据库句柄
3、执行sql操作:sqlite3_exec
原型:
int sqlite3_exec(
sqlite3* ppDb, /* An open database */
const char *sql, /* SQL to be evaluated */
int (*callback)(void*,int,char**,char**), /* Callback function */
void *, /* 1st argument to callback */
char **errmsg /* Error msg written here */
);
第1个参数ppDb,数据库句柄
第2个参数constchar*sql,是一条sql 语句,以\0结尾。
第3个参数sqlite3_callback ,是回调函数,当SQL语句执行完毕后,会自动调用。如果SQL语句不需要处理返回数据,则可以置为NULL
第4个参数void*是你所提供的指针,该参数作为上面这个回调函数的参数,可以作为回调函数的一个输入。不需要时可以置为NULL
第5个参数char** errmsg 是错误信息。注意是指针的指针。sqlite3里面有很多固定的错误信息。执行sqlite3_exec 之后,执行失败时可以查阅这个指针(直接cout<<errmsg得到一串字符串信息,这串信息告诉你错在什么地方。sqlite3_exec函数通过修改你传入的指针的指针,把你提供的指针指向错误提示信息,这样sqlite3_exec函数外面就可以通过这个char*得到具体错误提示。
说明:通常,sqlite3_callback 和它后面的void*这两个位置都可以填NULL。填NULL表示你不需要回调。比如你做insert 操作,做delete操作,就没有必要使用回调。而当你做select 时,就要使用回调,因为sqlite3 把数据查出来,得通过回调告诉你查出了什么数据。
exec 的回调
typedef int(*sqlite3_callback)(void*,int,char**,char**);
你的回调函数必须定义成上面这个函数的类型。
sqlite 每查到一条记录,就调用一次这个回调
例如:
int LoadMyInfo(void* para,intn_column,char** column_value,char** column_name);
para--是你在sqlite3_exec 里传入的void*参数通过para参数,你可以传入一些特殊的指针(比如类指针、结构指针),然后在这里面强制转换成对应的类型(这里面是void*类型,必须强制转换成你的类型才可用)。然后操作这些数据
n_column--是这一条记录有多少个字段(即这条记录有多少列)
char** column_value --是个关键值,查出来的数据都保存在这里,它实际上是个1维数组(不要以为是2维数组),每一个元素都是一个char*值,是一个字段内容(用字符串来表示,以\0结尾)
char** column_name 跟column_value是对应的,表示每个字段的字段名称
实例:
#include <iostream>
using namespace std;
#include "sqlite/sqlite3.h"
int callback(void*,int,char**,char**);
int main()
{
sqlite3* db;
int nResult = sqlite3_open("test.db",&db);
if (nResult != SQLITE_OK)
{
cout<<"打开数据库失败:"<<sqlite3_errmsg(db)<<endl;
return 0;
}
else
{
cout<<"数据库打开成功"<<endl;
}
char* errmsg;
nResult = sqlite3_exec(db,"create table abc(id integer primary key autoincrement,name varchar(100))",NULL,NULL,&errmsg);
if (nResult != SQLITE_OK)
{
sqlite3_close(db);
cout<<errmsg;
sqlite3_free(errmsg);
return 0;
}
string strSql;
strSql+="begin;\n";
for (int i=0;i<100;i++)
{
strSql+="insert into abc values(null,'heh');\n";
}
strSql+="commit;";
//cout<<strSql<<endl;
nResult = sqlite3_exec(db,strSql.c_str(),NULL,NULL,&errmsg);
if (nResult != SQLITE_OK)
{
sqlite3_close(db);
cout<<errmsg<<endl;
sqlite3_free(errmsg);
return 0;
}
strSql = "select * from abc";
nResult = sqlite3_exec(db,strSql.c_str(),callback,NULL,&errmsg);
if (nResult != SQLITE_OK)
{
sqlite3_close(db);
cout<<errmsg<<endl;
sqlite3_free(errmsg);
return 0;
}
sqlite3_close(db);
return 0;
}
int callback(void* ,int nCount,char** pValue,char** pName)
{
string s;
for(int i=0;i<nCount;i++)
{
s+=pName[i];
s+=":";
s+=pValue[i];
s+="\n";
}
cout<<s<<endl;
return 0;
}
int sqlite3_get_table(
sqlite3 *db, /* An open database */
const char *zSql, /* SQL to be evaluated */
char ***pazResult, /* Results of the query */
int *pnRow, /* Number of result rows written here */
int *pnColumn, /* Number of result columns written here */
char **pzErrmsg /* Error msg written here */
);
函数功能:
void sqlite3_free_table(char **result);
函数功能:释放函数sqlite3_get_table返回的查询数据pazResult的内存
#include <iostream>
using namespace std;
#include "sqlite/sqlite3.h"
int callback(void*,int,char**,char**);
int main()
{
sqlite3* db;
int nResult = sqlite3_open("test.db",&db);
if (nResult != SQLITE_OK)
{
cout<<"打开数据库失败:"<<sqlite3_errmsg(db)<<endl;
return 0;
}
else
{
cout<<"数据库打开成功"<<endl;
}
char* errmsg;
nResult = sqlite3_exec(db,"create table abc(id integer primary key autoincrement,name varchar(100))",NULL,NULL,&errmsg);
if (nResult != SQLITE_OK)
{
sqlite3_close(db);
cout<<errmsg;
sqlite3_free(errmsg);
return 0;
}
string strSql;
strSql+="begin;\n";
for (int i=0;i<100;i++)
{
strSql+="insert into abc values(null,'heh');\n";
}
strSql+="commit;";
//cout<<strSql<<endl;
nResult = sqlite3_exec(db,strSql.c_str(),NULL,NULL,&errmsg);
if (nResult != SQLITE_OK)
{
sqlite3_close(db);
cout<<errmsg<<endl;
sqlite3_free(errmsg);
return 0;
}
strSql = "select * from abc";
//nResult = sqlite3_exec(db,strSql.c_str(),callback,NULL,&errmsg);
char** pResult;
int nRow;
int nCol;
nResult = sqlite3_get_table(db,strSql.c_str(),&pResult,&nRow,&nCol,&errmsg);
if (nResult != SQLITE_OK)
{
sqlite3_close(db);
cout<<errmsg<<endl;
sqlite3_free(errmsg);
return 0;
}
string strOut;
int nIndex = nCol;
for(int i=0;i<nRow;i++)
{
for(int j=0;j<nCol;j++)
{
strOut+=pResult[j];
strOut+=":";
strOut+=pResult[nIndex];
strOut+="\n";
++nIndex;
}
}
sqlite3_free_table(pResult);
cout<<strOut<<endl;
sqlite3_close(db);
return 0;
}