SQLite将数据库的数据存储在磁盘的单一文件中,并通过简单的外部接口提供SQL支持。由于其设计之初即是针对小规模数据的操作,在查询优化、高并发读写等方面做了极简化的处理,可以保证不占用系统额外的资源,因此,在大多数的嵌入式开发中,会比专业数据库有更快速、高效的执行效率。
好了,看看如何加入Sqlite3吧,先从官网http://www.sqlite.org/download.html,下载你所想要的版本的sqlite,我这下的是3.8.2的,在解压后,只有4个文件,没有.def或者是什么dll,lib,这个是要自己生成的。
解压后,将这四个文件添加到你的当前工程中,然后进行编译一下就可以了。
好了,先看个如何创建一个表:
回调函数的格式如下: int sqlite_callback( void* pv, /* 由 sqlite3_exec() 的第四个参数传递而来 */ int argc, /* 表的列数 */ char** argv, /* 指向查询结果的指针数组, 可以由 sqlite3_column_text() 得到 */ char** col /* 指向表头名的指针数组, 可以由 sqlite3_column_name() 得到 */ ); 参数格式: 传给sqlite3_exec的回调函数,用来显示查询结果 对每一条查询结果调用一次该回调函数 参数: pv:由sqlite3_exec传递的初始化参数 argc:表头的列数 col:表头的名字数组指针 argv:表头的数据数组指针 返回值: 1:中断查找 0:继续列举查询到的数据 示例表: +-----------------------------------+ | id | pic | data(16进制数据) | |-----------------------------------| | 1 | a.jpg | 00 00 00 ... | |-----------------------------------| | 2 | b.jpg | XX XX XX | +-----------------------------------+ 对第一行数据: argc=3 即 [0]...[2] argv[0]="1",argv[1]="a.jpg",argv[2]="00 00 00..."(实际16进制数据,非这里显示的字符串形式) col[0]="id",col[1]="pic",col[2]="data" 说明: sqlite3_exec() 的回调函数必须按照此格式, 当然形参的名字任意. 如果某列的数据类型不是char*, 则可以对结果执行相关的转换, 如:用atoi()把结果转换为整数(integer), 如果是二进制数据, 则可以直接强制类型转换, 如:(void*)argv[i]. 该回调函数有两种返回值类型. 1.返回零:sqlite3_exec() 将继续执行查询. 2.返回非零:sqlite3_exec()将立即中断查询, 且 sqlite3_exec() 将返回 SQLITE_ABORT. 示例: int i; for(i=0; i<argc; i++) { printf("%s\t%s\n\n", col[i], argv[i]); } 例子: m_pGameDataBase = prepareTableInDB( "MyGame2", "E:\\database\\MyGame"); std::string insert_sql_("insert into MyGame2 values(123, \"aaa\", 26, \"man\")"); insertRecordIntoSqlite(insert_sql_); readRecord();// read the record // int readFromSQLite(void *anyParam, int iColumn, char **pColumnValue, char **pColumnName ) { int icolumenCount = iColumn; for(int i = 0; i < icolumenCount; ++i) { CCLOG("%s: %s \n", pColumnName[i], pColumnValue[i]); } return 0; } void GameController::readRecord() { std::string read_sql_("select * from MyGame2"); char *perrMsg = 0; sqlite3_exec( m_pGameDataBase, read_sql_.c_str(), readFromSQLite, NULL, &perrMsg); } sqlite3* GameController::prepareTableInDB(const char* table, const char* dbFilename) { sqlite3 *splite2Data = 0; char *errMsg = 0; if(SQLITE_OK != sqlite3_open(dbFilename, &splite2Data)) { CCLOG("Open the Game Data Table falied\n"); return 0; } std::string string_sql = "create table if not exists " + std::string(table) + "( id int unsigned auto_increment primary key, name varchar(50), age int, sex varchar(10))"; sqlite3_exec(splite2Data, string_sql.c_str(), NULL, NULL, &errMsg); // create a table; if(errMsg != 0) { CCLOG("Create the Data table failed\n"); sqlite3_close(splite2Data); return 0; } return splite2Data; } void GameController::insertRecordIntoSqlite(std::string &r_stringOrder) { char *perrMsg = 0; int info = sqlite3_exec( m_pGameDataBase, r_stringOrder.c_str(), 0, 0, &perrMsg); do{ if(perrMsg != 0) { // execute the instruct failed CCLOG("insert a record into the table failed\n"); CCLOG("reason: %d, Error Message: %s \n", info, perrMsg); break; } } while(0); sqlite3_close(m_pGameDataBase); // }