#include
int sqlite3_open(
const char *filename, /* Database filename (UTF-8) */
sqlite3 **ppDb /* OUT: SQLite db handle */
);
举例:
if(sqlite3_open("EmployeeManagement.db",&db) != SQLITE_OK)
{
printf("error :%s \n",sqlite3_errmsg(db));
exit(-1);
}
printf("success open\n");
能做到这一点的有2条语句
(1)需要外加回调函数的 sqlite3_exec()
int sqlite3_exec(
sqlite3*, /* 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 */
);
接着看看回调函数的原型
typedef int (*sqlite_callback)(void* para,int columnCount,char** columnValue,char** columnName);
第一个参数 void* para 就是在 sqlite3_exec(),中的第三个参数,用于给回调函数传入数据。
调用回调函数的原理是,数据库执行 *sql 这句数据库语句时,每找到一条符合的数据,就会调用一次回调函数。
(2)不需要回调函数的 sqlite3_get_table()
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 */
);
第三个参数 char ***pazResult 指向符合 *sql 语句的数据
第四个参数 int *pnRow 存放符合 *sql 语句的数据的条数
第五个参数 int *pnColumn 存放提取出来的数据的字段数
而 pazResult 中存放的数据是按一个的规律摆放的:
可以将 pazResult 中存放的数据看作一张二维表, pnRow 中的是行数, pnColumn 中的是一半的列数。这一半的列后面紧跟着的列就是和这些字段对应的数据。
放一个遍历的例子:
index = nColumn;
for( i = 0; i < nRow ; i )
{
for( j = 0 ; j < nColumn; j )
{
printf( “属性:%s 值:%s\n”, Result[j], Result [index++] );
}
}