DBUtil.h:
- class DBUtil{
- public:
- /************************************************************
- 封装 <a target="_blank" style="color: #0000F0; display:inline; position:static; background:none;" href="http://www.so.com/s?q=sqlite3&ie=utf-8&src=se_lighten_f">sqlite3</a>操作
- ************************************************************/
- //用来创建一个db数据库 db为数据库的名字
- // 打开数据库
- static void initDB(const char *db);
- //用来判断表格是否存在
- // name:表示表名
- static bool tableIsExist(string name);
- //用来创建一个表名为name的表格,创建时会先匹配时否有该表的存在如果存在则不创建
- //创建表
- static void createTable(string sql,string name);
- //用来删除一张表名为name的表格,删除时会先匹配是否有该表的存在如果不存在则不执行删除操作
- //删除表名
- static void deleteTable(string sql,string name);
- //用来向表中插入一条数据
- //插入一条数据
- static void insertData(string sql);
- //用来向表中删除一条数据
- //删除一条数据
- static void deleteData(string sql);
- //用来向表中修改一条数据
- // 修改一条数据
- static void updateData(string sql);
- //获取一个记录的条数
- // 获得记录的条数
- static int getDataCount(string sql);
- //读取一条记录的信息
- /*
- * 此方法是查询方法,相当之重要,pSender最好是个vector
- */
- static void getDataInfo(string sql,void *pSend);
- //关闭打开的数据库
- static void closeDB();
- };
DBUtil.cpp:
- sqlite3 *pDB = NULL;//数据库指针
- char * errMsg = NULL;//错误信息
- std::string sqlstr;//SQL指令
- int result;//sqlite3_exec返回值
- //创建数据库
- void DBUtil::initDB(const char *db )
- {
- //打开一个数据库,如果该数据库不存在,则创建一个数据库文件
- result = sqlite3_open(db, &pDB);
- if( result != SQLITE_OK )
- CCLog( "打开数据库失败,错误码:%d ,错误原因:%s\n" , result, errMsg );
- }
- //tableIsExist的<a target="_blank" style="color: #0000F0; display:inline; position:static; background:none;" href="http://www.so.com/s?q=%E5%9B%9E%E8%B0%83%E5%87%BD%E6%95%B0&ie=utf-8&src=se_lighten_f">回调函数</a>
- int isExisted( void * para, int n_column, char ** column_value, char ** column_name )
- {
- bool *isExisted_=(bool*)para;
- *isExisted_=(**column_value)!='0';
- return 0;
- }
- //判断表格是否存在
- bool DBUtil::tableIsExist( string name )
- {
- if (pDB!=NULL)
- {
- //判断表是否存在
- bool tableIsExisted;
- sqlstr = "select count(type) from sqlite_master where type='table' and name ='"+name+"'";
- result =sqlite3_exec(pDB,sqlstr.c_str(),isExisted,&tableIsExisted,&errMsg);
- return tableIsExisted;
- }
- return false;
- }
- //在数据库中判断名为name的表示否存在,如果不存在则创建这张表
- //@示例语句string sqls = "create table user(id integer,username text,password text)";
- void DBUtil::createTable( string sql,string name )
- {
- if (!tableIsExist(name))
- {
- //创建表,设置ID为主键,且自动增加
- result = sqlite3_exec(pDB,sql.c_str(),NULL,NULL,&errMsg);
- if( result != SQLITE_OK )
- CCLog( "创建表失败,错误码:%d ,错误原因:%s\n" , result, errMsg );
- }
- }
- //删除表格
- //@示例语句sqlstr="drop table name";
- void DBUtil::deleteTable( string sql,string name )
- {
- if (tableIsExist(name))
- {
- result = sqlite3_exec(pDB,sql.c_str(),NULL,NULL,&errMsg);
- if( result != SQLITE_OK )
- CCLog( "创建表失败,错误码:%d ,错误原因:%s\n" , result, errMsg );
- }
- }
- //插入数据
- //@示例语句sqlstr=" insert into MyTable_1( name ) values ( '<a target="_blank" style="color: #0000F0; display:inline; position:static; background:none;" href="http://www.so.com/s?q=%E6%93%8E%E5%A4%A9%E6%9F%B1&ie=utf-8&src=se_lighten_f">擎天柱</a>' ) ";
- void DBUtil::insertData( string sql ){
- result = sqlite3_exec( pDB, sql.c_str() , NULL, NULL, &errMsg );
- if(result != SQLITE_OK )
- CCLog( "插入记录失败,错误码:%d ,错误原因:%s\n" , result, errMsg );
- }
- //删除数据
- //@示例语句sqlstr="delete from MyTable_1 where ID = 2";
- void DBUtil::deleteData( string sql )
- {
- result=sqlite3_exec( pDB, sql.c_str() , NULL, NULL, &errMsg );
- if(result != SQLITE_OK )
- CCLog( "插入记录失败,错误码:%d ,错误原因:%s\n" , result, errMsg );
- }
- //修改数据
- //@示例语句 sqlstr="update MyTable_1 set name='威震天' where ID = 3";
- void DBUtil::updateData( string sql )
- {
- result = sqlite3_exec( pDB, sql.c_str() , NULL, NULL, &errMsg );
- if(result != SQLITE_OK )
- CCLog( "插入记录失败,错误码:%d ,错误原因:%s\n" , result, errMsg );
- }
- //getDataCount的回调函数
- int loadRecordCount( void * para, int n_column, char ** column_value, char ** column_name )
- {
- int *count=(int*)para;
- *count=n_column;
- return 0;
- }
- //获取记录的条数
- //@示例语句string sqlsssss = "select count(*) from user";
- //@示例语句 取得表格字段的语句string sqlsssss = "select * from user";
- int DBUtil::getDataCount( string sql )
- {
- int count=0;
- sqlite3_exec( pDB, sql.c_str() , loadRecordCount, &count, &errMsg );
- return count;
- }
- //getDataInfo的回调函数
- int loadRecord( void * para, int n_column, char ** column_value, char ** column_name )
- {
- CCLOG("n_column:%d",n_column);
- // TestVO* testVO = (TestVO*)para;
- // testVO->mId = atoi(column_value[0]);
- // testVO->level = atoi(column_value[1]);
- // testVO->lastscore = atoi(column_value[2]);
- // testVO->bestscore = atoi(column_value[3]);
- // testVO->star = atoi(column_value[4]);
- /* 可能有5个字段 */
- // id level lastscore bestscore star
- // CCLOG("c[0]:%s,c[1]:%s,c[2]:%s,c[3]:%s,c[4]:%s",column_name[0],column_name[1],column_name[2],column_name[3],column_name[4]);
- //
- CCLog("id=%s,level=%s,lastscore=%s,bestscore=%s,star=%s",column_value[0],column_value[1],column_value[2],column_value[3],column_value[4]);
- return 0;
- }
- //获取一条记录的信息 其中的pSend是一个<a target="_blank" style="color: #0000F0; display:inline; position:static; background:none;" href="http://www.so.com/s?q=%E5%AE%9E%E4%BD%93%E7%B1%BB&ie=utf-8&src=se_lighten_f">实体类</a>我们以后可以自定义一个继承了CCObject的类来代替他保存数据库中取出来的数据
- /*
- * 这里最好扩展下,让 pSend 是一个vector
- */
- void DBUtil::getDataInfo( string sql,void *pSend )
- {
- sqlite3_exec( pDB, sql.c_str() , loadRecord, pSend, &errMsg );
- }
- //关闭数据库
- void DBUtil::closeDB()
- {
- sqlite3_close(pDB);
- }
测试:
- // Test Database
- string fullDBPath = CCFileUtils::sharedFileUtils()->getWriteablePath() + "save.db";
- CCLOG("fullPath : %s",fullDBPath.c_str());
- //打开数据库
- DBUtil::initDB(fullDBPath.c_str());
- string createTableSql = "create table zuma (id integer primary key autoincrement,level integer,lastscore integer,bestscore integer,star integer);";
- DBUtil::createTable(createTableSql.c_str(),"zuma");
- //向表格中插入数据
- string sqlss = "insert into zuma (level,lastscore,bestscore,star) values (100,100,500,1)";
- /* 插入一条数据 */
- DBUtil::insertData(sqlss);
- // 更新
- string updateString = "update zuma set star = 5;";
- DBUtil::updateData(updateString);
- /* 查询数据 */
- string selectStr = "select * from zuma";
- // CCObject* pp = new CCObject();
- // DBUtil::getDataInfo(selectStr, pp);
- TestVO* testVO = new TestVO();
- testVO->mId = 1111;
- DBUtil::getDataInfo(selectStr,testVO);
- /* 显示结果 */
- CCLOG("id:%d",testVO->mId);
- CCLOG("level:%d",testVO->level);
- CCLOG("lastscore:%d",testVO->lastscore);
- CCLOG("bestscore:%d",testVO->bestscore);
- CCLOG("star:%d",testVO->star);
- delete testVO;
- /* 不能忘记关闭数据库 */
- DBUtil::closeDB();