#include <sqlite3.h>
#include <string>
#include <stdio.h>
using namespace std;
void doTest()
{
sqlite3* conn = NULL;
//1. 打开数据库
int result = sqlite3_open("D:/mytest.db",&conn);
if (result != SQLITE_OK) {
sqlite3_close(conn);
return;
}
const char* createTableSQL =
"CREATE TABLE TESTTABLE (int_col INT, float_col REAL, string_col TEXT)";
sqlite3_stmt* stmt = NULL;
int len = strlen(createTableSQL);
//2. 准备创建数据表,如果创建失败,需要用sqlite3_finalize释放sqlite3_stmt对象,以防止内存泄露。
if (sqlite3_prepare_v2(conn,createTableSQL,len,&stmt,NULL) != SQLITE_OK) {
if (stmt)
sqlite3_finalize(stmt);
sqlite3_close(conn);
return;
}
//3. 通过sqlite3_step命令执行创建表的语句。对于DDL和DML语句而言,sqlite3_step执行正确的返回值
//只有SQLITE_DONE,对于SELECT查询而言,如果有数据返回SQLITE_ROW,当到达结果集末尾时则返回
//SQLITE_DONE。
if (sqlite3_step(stmt) != SQLITE_DONE) {
sqlite3_finalize(stmt);
sqlite3_close(conn);
return;
}
//4. 释放创建表语句对象的资源。
sqlite3_finalize(stmt);
printf("Succeed to create test table now.\n");
int insertCount = 10;
//5. 构建插入数据的sqlite3_stmt对象。
const char* insertSQL = "INSERT INTO TESTTABLE VALUES(%d,%f,'%s')";
const char* testString = "this is a test.";
char sql[1024];
sqlite3_stmt* stmt2 = NULL;
for (int i = 0; i < insertCount; ++i) {
sprintf(sql,insertSQL,i,i * 1.0,testString);
if (sqlite3_prepare_v2(conn,sql,strlen(sql),&stmt2,NULL) != SQLITE_OK) {
if (stmt2)
sqlite3_finalize(stmt2);
sqlite3_close(conn);
return;
}
if (sqlite3_step(stmt2) != SQLITE_DONE) {
sqlite3_finalize(stmt2);
sqlite3_close(conn);
return;
}
printf("Insert Succeed.\n");
}
sqlite3_finalize(stmt2);
//6. 为了方便下一次测试运行,我们这里需要删除该函数创建的数据表,否则在下次运行时将无法
//创建该表,因为它已经存在。
const char* dropSQL = "DROP TABLE TESTTABLE";
sqlite3_stmt* stmt3 = NULL;
if (sqlite3_prepare_v2(conn,dropSQL,strlen(dropSQL),&stmt3,NULL) != SQLITE_OK) {
if (stmt3)
sqlite3_finalize(stmt3);
sqlite3_close(conn);
return;
}
if (sqlite3_step(stmt3) == SQLITE_DONE) {
printf("The test table has been dropped.\n");
}
sqlite3_finalize(stmt3);
sqlite3_close(conn);
}
int main()
{
doTest();
return 0;
}
//输出结果如下:
//Succeed to create test table now.
//Insert Succeed.
//Insert Succeed.
//Insert Succeed.
//Insert Succeed.
//Insert Succeed.
//Insert Succeed.
//Insert Succeed.
//Insert Succeed.
//Insert Succeed.
//Insert Succeed.
//The test table has been dropped.
http://www.cnblogs.com/stephen-liu74/archive/2012/03/05/2340780.html
#include <sqlite3.h>
#include <string>
using namespace std;
void doTest()
{
sqlite3* conn = NULL;
//1. 打开数据库
int result = sqlite3_open("D:/mytest.db",&conn);
if (result != SQLITE_OK) {
sqlite3_close(conn);
return;
}
const char* createTableSQL =
"CREATE TABLE TESTTABLE (int_col INT, float_col REAL, string_col TEXT)";
sqlite3_stmt* stmt = NULL;
int len = strlen(createTableSQL);
//2. 准备创建数据表,如果创建失败,需要用sqlite3_finalize释放sqlite3_stmt对象,以防止内存泄露。
if (sqlite3_prepare_v2(conn,createTableSQL,len,&stmt,NULL) != SQLITE_OK) {
if (stmt)
sqlite3_finalize(stmt);
sqlite3_close(conn);
return;
}
//3. 通过sqlite3_step命令执行创建表的语句。对于DDL和DML语句而言,sqlite3_step执行正确的返回值
//只有SQLITE_DONE,对于SELECT查询而言,如果有数据返回SQLITE_ROW,当到达结果集末尾时则返回
//SQLITE_DONE。
if (sqlite3_step(stmt) != SQLITE_DONE) {
sqlite3_finalize(stmt);
sqlite3_close(conn);
return;
}
//4. 释放创建表语句对象的资源。
sqlite3_finalize(stmt);
printf("Succeed to create test table now.\n");
//5. 构造查询表数据的sqlite3_stmt对象。
const char* selectSQL = "SELECT * FROM TESTTABLE WHERE 1 = 0";
sqlite3_stmt* stmt2 = NULL;
if (sqlite3_prepare_v2(conn,selectSQL,strlen(selectSQL),&stmt2,NULL) != SQLITE_OK) {
if (stmt2)
sqlite3_finalize(stmt2);
sqlite3_close(conn);
return;
}
//6. 根据select语句的对象,获取结果集中的字段数量。
int fieldCount = sqlite3_column_count(stmt2);
printf("The column count is %d.\n",fieldCount);
//7. 遍历结果集中每个字段meta信息,并获取其声明时的类型。
for (int i = 0; i < fieldCount; ++i) {
//由于此时Table中并不存在数据,再有就是SQLite中的数据类型本身是动态的,所以在没有数据时
//无法通过sqlite3_column_type函数获取,此时sqlite3_column_type只会返回SQLITE_NULL,
//直到有数据时才能返回具体的类型,因此这里使用了sqlite3_column_decltype函数来获取表声
//明时给出的声明类型。
string stype = sqlite3_column_decltype(stmt2,i);
stype = strlwr((char*)stype.c_str());
//下面的解析规则见该系列的“数据类型-->1. 决定字段亲缘性的规则”部分,其链接如下:
//http://www.cnblogs.com/stephen-liu74/archive/2012/01/18/2325258.html
if (stype.find("int") != string::npos) {
printf("The type of %dth column is INTEGER.\n",i);
} else if (stype.find("char") != string::npos
|| stype.find("text") != string::npos) {
printf("The type of %dth column is TEXT.\n",i);
} else if (stype.find("real") != string::npos
|| stype.find("floa") != string::npos
|| stype.find("doub") != string::npos ) {
printf("The type of %dth column is DOUBLE.\n",i);
}
}
sqlite3_finalize(stmt2);
//8. 为了方便下一次测试运行,我们这里需要删除该函数创建的数据表,否则在下次运行时将无法
//创建该表,因为它已经存在。
const char* dropSQL = "DROP TABLE TESTTABLE";
sqlite3_stmt* stmt3 = NULL;
if (sqlite3_prepare_v2(conn,dropSQL,strlen(dropSQL),&stmt3,NULL) != SQLITE_OK) {
if (stmt3)
sqlite3_finalize(stmt3);
sqlite3_close(conn);
return;
}
if (sqlite3_step(stmt3) == SQLITE_DONE) {
printf("The test table has been dropped.\n");
}
sqlite3_finalize(stmt3);
sqlite3_close(conn);
}
int main()
{
doTest();
return 0;
}
//输出结果为:
//Succeed to create test table now.
//The column count is 3.
//The type of 0th column is INTEGER.
//The type of 1th column is DOUBLE.
//The type of 2th column is TEXT.
//The test table has been dropped.
#include <sqlite3.h>
#include <string>
#include <stdio.h>
using namespace std;
void doTest()
{
sqlite3* conn = NULL;
//1. 打开数据库
int result = sqlite3_open("D:/mytest.db",&conn);
if (result != SQLITE_OK) {
sqlite3_close(conn);
return;
}
const char* createTableSQL =
"CREATE TABLE TESTTABLE (int_col INT, float_col REAL, string_col TEXT)";
sqlite3_stmt* stmt = NULL;
int len = strlen(createTableSQL);
//2. 准备创建数据表,如果创建失败,需要用sqlite3_finalize释放sqlite3_stmt对象,以防止内存泄露。
if (sqlite3_prepare_v2(conn,createTableSQL,len,&stmt,NULL) != SQLITE_OK) {
if (stmt)
sqlite3_finalize(stmt);
sqlite3_close(conn);
return;
}
//3. 通过sqlite3_step命令执行创建表的语句。对于DDL和DML语句而言,sqlite3_step执行正确的返回值
//只有SQLITE_DONE,对于SELECT查询而言,如果有数据返回SQLITE_ROW,当到达结果集末尾时则返回
//SQLITE_DONE。
if (sqlite3_step(stmt) != SQLITE_DONE) {
sqlite3_finalize(stmt);
sqlite3_close(conn);
return;
}
//4. 释放创建表语句对象的资源。
sqlite3_finalize(stmt);
printf("Succeed to create test table now.\n");
//5. 显式的开启一个事物。
sqlite3_stmt* stmt2 = NULL;
const char* beginSQL = "BEGIN TRANSACTION";
if (sqlite3_prepare_v2(conn,beginSQL,strlen(beginSQL),&stmt2,NULL) != SQLITE_OK) {
if (stmt2)
sqlite3_finalize(stmt2);
sqlite3_close(conn);
return;
}
if (sqlite3_step(stmt2) != SQLITE_DONE) {
sqlite3_finalize(stmt2);
sqlite3_close(conn);
return;
}
sqlite3_finalize(stmt2);
//6. 构建基于绑定变量的插入数据。
const char* insertSQL = "INSERT INTO TESTTABLE VALUES(?,?,?)";
sqlite3_stmt* stmt3 = NULL;
if (sqlite3_prepare_v2(conn,insertSQL,strlen(insertSQL),&stmt3,NULL) != SQLITE_OK) {
if (stmt3)
sqlite3_finalize(stmt3);
sqlite3_close(conn);
return;
}
int insertCount = 10;
const char* strData = "This is a test.";
//7. 基于已有的SQL语句,迭代的绑定不同的变量数据
for (int i = 0; i < insertCount; ++i) {
//在绑定时,最左面的变量索引值是1。
sqlite3_bind_int(stmt3,1,i);
sqlite3_bind_double(stmt3,2,i * 1.0);
sqlite3_bind_text(stmt3,3,strData,strlen(strData),SQLITE_TRANSIENT);
if (sqlite3_step(stmt3) != SQLITE_DONE) {
sqlite3_finalize(stmt3);
sqlite3_close(conn);
return;
}
//重新初始化该sqlite3_stmt对象绑定的变量。
sqlite3_reset(stmt3);
printf("Insert Succeed.\n");
}
sqlite3_finalize(stmt3);
//8. 提交之前的事物。
const char* commitSQL = "COMMIT";
sqlite3_stmt* stmt4 = NULL;
if (sqlite3_prepare_v2(conn,commitSQL,strlen(commitSQL),&stmt4,NULL) != SQLITE_OK) {
if (stmt4)
sqlite3_finalize(stmt4);
sqlite3_close(conn);
return;
}
if (sqlite3_step(stmt4) != SQLITE_DONE) {
sqlite3_finalize(stmt4);
sqlite3_close(conn);
return;
}
sqlite3_finalize(stmt4);
//9. 为了方便下一次测试运行,我们这里需要删除该函数创建的数据表,否则在下次运行时将无法
//创建该表,因为它已经存在。
const char* dropSQL = "DROP TABLE TESTTABLE";
sqlite3_stmt* stmt5 = NULL;
if (sqlite3_prepare_v2(conn,dropSQL,strlen(dropSQL),&stmt5,NULL) != SQLITE_OK) {
if (stmt5)
sqlite3_finalize(stmt5);
sqlite3_close(conn);
return;
}
if (sqlite3_step(stmt5) == SQLITE_DONE) {
printf("The test table has been dropped.\n");
}
sqlite3_finalize(stmt5);
sqlite3_close(conn);
}
int main()
{
doTest();
return 0;
}
//输出结果如下:
//Succeed to create test table now.
//Insert Succeed.
//Insert Succeed.
//Insert Succeed.
//Insert Succeed.
//Insert Succeed.
//Insert Succeed.
//Insert Succeed.
//Insert Succeed.
//Insert Succeed.
//Insert Succeed.
//The test table has been dropped.
#include <sqlite3.h>
#include <string>
#include <stdio.h>
using namespace std;
void doTest()
{
sqlite3* conn = NULL;
//1. 打开数据库
int result = sqlite3_open("D:/mytest.db",&conn);
if (result != SQLITE_OK) {
sqlite3_close(conn);
return;
}
const char* createTableSQL =
"CREATE TABLE TESTTABLE (int_col INT, float_col REAL, string_col TEXT)";
sqlite3_stmt* stmt = NULL;
int len = strlen(createTableSQL);
//2. 准备创建数据表,如果创建失败,需要用sqlite3_finalize释放sqlite3_stmt对象,以防止内存泄露。
if (sqlite3_prepare_v2(conn,createTableSQL,len,&stmt,NULL) != SQLITE_OK) {
if (stmt)
sqlite3_finalize(stmt);
sqlite3_close(conn);
return;
}
//3. 通过sqlite3_step命令执行创建表的语句。对于DDL和DML语句而言,sqlite3_step执行正确的返回值
//只有SQLITE_DONE,对于SELECT查询而言,如果有数据返回SQLITE_ROW,当到达结果集末尾时则返回
//SQLITE_DONE。
if (sqlite3_step(stmt) != SQLITE_DONE) {
sqlite3_finalize(stmt);
sqlite3_close(conn);
return;
}
//4. 释放创建表语句对象的资源。
sqlite3_finalize(stmt);
printf("Succeed to create test table now.\n");
//5. 为后面的查询操作插入测试数据。
sqlite3_stmt* stmt2 = NULL;
const char* insertSQL = "INSERT INTO TESTTABLE VALUES(20,21.0,'this is a test.')";
if (sqlite3_prepare_v2(conn,insertSQL,strlen(insertSQL),&stmt2,NULL) != SQLITE_OK) {
if (stmt2)
sqlite3_finalize(stmt2);
sqlite3_close(conn);
return;
}
if (sqlite3_step(stmt2) != SQLITE_DONE) {
sqlite3_finalize(stmt2);
sqlite3_close(conn);
return;
}
printf("Succeed to insert test data.\n");
sqlite3_finalize(stmt2);
//6. 执行SELECT语句查询数据。
const char* selectSQL = "SELECT * FROM TESTTABLE";
sqlite3_stmt* stmt3 = NULL;
if (sqlite3_prepare_v2(conn,selectSQL,strlen(selectSQL),&stmt3,NULL) != SQLITE_OK) {
if (stmt3)
sqlite3_finalize(stmt3);
sqlite3_close(conn);
return;
}
int fieldCount = sqlite3_column_count(stmt3);
do {
int r = sqlite3_step(stmt3);
if (r == SQLITE_ROW) {
for (int i = 0; i < fieldCount; ++i) {
//这里需要先判断当前记录当前字段的类型,再根据返回的类型使用不同的API函数
//获取实际的数据值。
int vtype = sqlite3_column_type(stmt3,i);
if (vtype == SQLITE_INTEGER) {
int v = sqlite3_column_int(stmt3,i);
printf("The INTEGER value is %d.\n",v);
} else if (vtype == SQLITE_FLOAT) {
double v = sqlite3_column_double(stmt3,i);
printf("The DOUBLE value is %f.\n",v);
} else if (vtype == SQLITE_TEXT) {
const char* v = (const char*)sqlite3_column_text(stmt3,i);
printf("The TEXT value is %s.\n",v);
} else if (vtype == SQLITE_NULL) {
printf("This value is NULL.\n");
}
}
} else if (r == SQLITE_DONE) {
printf("Select Finished.\n");
break;
} else {
printf("Failed to SELECT.\n");
sqlite3_finalize(stmt3);
sqlite3_close(conn);
return;
}
} while (true);
sqlite3_finalize(stmt3);
//7. 为了方便下一次测试运行,我们这里需要删除该函数创建的数据表,否则在下次运行时将无法
//创建该表,因为它已经存在。
const char* dropSQL = "DROP TABLE TESTTABLE";
sqlite3_stmt* stmt4 = NULL;
if (sqlite3_prepare_v2(conn,dropSQL,strlen(dropSQL),&stmt4,NULL) != SQLITE_OK) {
if (stmt4)
sqlite3_finalize(stmt4);
sqlite3_close(conn);
return;
}
if (sqlite3_step(stmt4) == SQLITE_DONE) {
printf("The test table has been dropped.\n");
}
sqlite3_finalize(stmt4);
sqlite3_close(conn);
}
int main()
{
doTest();
return 0;
}
//输出结果如下:
//Succeed to create test table now.
//Succeed to insert test data.
//The INTEGER value is 20.
//The DOUBLE value is 21.000000.
//The TEXT value is this is a test..
//Select Finished.
//The test table has been dropped.
callback函数
sqlite3* db;
int rc;
char* zErr;
rc = sqlite3_open("test.db, &db);
if(rc) cout<<"ERROR"<<endl;
char * data = "CallBack";
char * sql = "select * from test";
rc = sqlite3_exec( db, sql, callback, data, &zErr );
if( rc != SQLITE_OK) cout<<"ERROR"<<endl;
sqlite3_close(db);
int callback(void* data, int ncols, char**values, char** headers)
{
for( int i=0; i<ncols; i++)
cout << headers[i] <<" --> "<< values[i] <<endl;
}
对sqlite3_exec执行结果中查询到的每条记录应用callback函数
每条记录的相应字段值存放于values数组,表头存放于headers数组,可以完成相应数据处理
sqlite3_get_table查询
char ** result;
int nrows, ncols;
char * zErr;
char * sql = "select * from test;";
int rc = sqlite3_get_table( db, sql, &result, &nrows, &ncols, &zErr );
cout<<"行数: "<< nrows<<endl;
cout<<"列数: "<< ncols<<endl;
for( int i = 0; i <= nrows; i++ )
{
for( int j = 0; j < ncols; j++ )
cout<< result[i*ncols+j] <<"\t";
cout<<endl;
}
sqlite3_free_table(result);
sqlite3_get_table 将查询得到的结果全部存入result数组,并可得到行数和列数
注意,第一行是表头
预处理查询
int rc;
sqlite3 *db;
sqlite3_stmt *stmt;
char *sql = "select * from episodes;";
const char *tail;
rc = sqlite3_open("test.db", &db);
rc = sqlite3_prepare(db, sql, (int)strlen(sql), &stmt, &tail);
rc = sqlite3_step(stmt);
int ncols = sqlite3_column_count(stmt);
while(rc == SQLITE_ROW) //sqlite3_step() has another row ready #define SQLITE_ROW 100
{
for( int i=0; i < ncols; i++ )
cout <<sqlite3_column_text(stmt, i);
cout << endl ;
rc =sqlite3_step(stmt);
}
sqlite3_finalize(stmt);
sqlite3_close(db);
sqlite3_prepare()也能接受一个包括多个SQL语句的字符串,但是只处理第一个SQL
若想处理多个,可应用tail参数,如下
while( sqlite3_complete(sql) )
{
rc = sqlite3_prepare(db, sql, strlen(sql), &stmt, &tail);
sql = tail;
...........
}
取字段信息
sqlite3_stmt * : statement handle
int iConl : 列号
const char *sqlite3_column_name( sqlite3_stmt*, int iCol ); //获取字段名称
const char *sqlite3_database_name( sqlite3_stmt*, int iCol ); //获取数据库名称
const char *sqlite3_table_name( sqlite3_stmt*, int iCol ); //获取表的名称
intsqlite3_column_type( sqlite3_stmt*, int iCol ); //SQLite本身的类型,或称存储类
返回值说明
1:SQLITE_INTEGER
2:SQLITE_FLOAT
3:SQLITE_TEXT
4:SQLITE_BLOB
5:SQLITE_NULL
const char *sqlite3_column_decltype( sqlite3_stmt*, int iCol ); //字段声明时的类型
如果结果集中的一列不是来自一个实际的字段(如来自于表达式、函数或聚合的结果),这个函数将返回NULL
sqlite3_column_xxx()函数取当前记录中每个字段的值
xxx表示你希望得到的数据类型,包括以下函数:
int sqlite3_column_int(sqlite3_stmt*, intiCol);
double sqlite3_column_double(sqlite3_stmt*,int iCol);
long long int sqlite3_column_int64(sqlite3_stmt*,int iCol);
const void*sqlite3_column_blob(sqlite3_stmt*, int iCol);
const unsigned char*sqlite3_column_text(sqlite3_stmt*, int iCol);
const void*sqlite3_column_text16(sqlite3_stmt*, int iCol);