ocos2d-x中使用sqlite数据库

来源:互联网 发布:欢乐逛软件 编辑:程序博客网 时间:2024/05/22 12:00
ocos2d-x中使用sqlite数据库       
        分类:            cocos2d-x637人阅读评论(0)收藏举报

下载附件,解压,添加到工程中,加入头文件

Cpp代码 复制代码 收藏代码
  1. include "sqlite/sqlite3.h" 
[cpp] view plaincopyprint?
  1. include "sqlite/sqlite3.h" 

创建数据库的代码 返回sqlite3的一个指针,通过该指针可以添加、修改、删除数据

Cpp代码 复制代码 收藏代码
  1. sqlite3 * DBUtils::creatTable()  
  2. {  
  3.     sqlite3 *pDB = NULL;//数据库指针 
  4.     char * errMsg = NULL;//错误信息 
  5.     std::string sqlstr;//SQL指令 
  6.     int result;//sqlite3_exec返回值 
  7.       
  8.     //打开一个数据库,如果该数据库不存在,则创建一个数据库文件 
  9.     result = sqlite3_open("save.db", &pDB);   
  10.     if( result != SQLITE_OK )   
  11.     //  CCLog( "打开数据库失败,错误码:%d ,错误原因:%s\n" , result, errMsg ); 
  12.     cout<<errMsg<<endl;  
  13.     //创建表,设置ID为主键,且自动增加   
  14.     result=sqlite3_exec( pDB, "create table Info( id integer primary key autoincrement, name nvarchar(32),score nvarchar(50),award nvarchar(50)) " , NULL, NULL, &errMsg );   
  15.       
  16.     return pDB;  
  17. //  sqlite3_close(pDB);   
  18.       
[cpp] view plaincopyprint?
  1. sqlite3 * DBUtils::creatTable() 
  2.     sqlite3 *pDB = NULL;//数据库指针  
  3.     char * errMsg = NULL;//错误信息 
  4.     std::string sqlstr;//SQL指令  
  5.     int result;//sqlite3_exec返回值 
  6.      
  7.     //打开一个数据库,如果该数据库不存在,则创建一个数据库文件  
  8.     result = sqlite3_open("save.db", &pDB);  
  9.     if( result != SQLITE_OK )  
  10.     //  CCLog( "打开数据库失败,错误码:%d ,错误原因:%s\n" , result, errMsg ); 
  11.     cout<<errMsg<<endl; 
  12.     //创建表,设置ID为主键,且自动增加  
  13.     result=sqlite3_exec( pDB, "create table Info( id integer primary key autoincrement, name nvarchar(32),score nvarchar(50),award nvarchar(50)) " , NULL, NULL, &errMsg );  
  14.      
  15.     return pDB; 
  16. //  sqlite3_close(pDB);  
  17.      

通过创建数据库返回的指针,保存数据

Cpp代码 复制代码 收藏代码
  1. int DBUtils::save(sqlite3 *pDB)  
  2. {  
  3.      char * errMsg=NULL;  
  4.     std::string sqlstr=" insert into Info(name,score,award) values ( '1','1','1' ) ";   
  5.     int result = sqlite3_exec( pDB, sqlstr.c_str() , NULL, NULL, &errMsg );   
  6.     return result;  
[cpp] view plaincopyprint?
  1. int DBUtils::save(sqlite3 *pDB) 
  2.      char * errMsg=NULL; 
  3.     std::string sqlstr=" insert into Info(name,score,award) values ( '1','1','1' ) ";  
  4.     int result = sqlite3_exec( pDB, sqlstr.c_str() , NULL, NULL, &errMsg );  
  5.     return result; 
 

查询数据

定义一个全局函数,也就是在类外定义

Cpp代码 复制代码 收藏代码
  1. int loadRecord( void * para, int n_column, char ** column_value, char ** column_name )  
  2. {  
  3.       
  4.           
  5.         InfoBean * info=new InfoBean(column_value[0],column_value[1],column_value[2],column_value[3]);  
  6.         /*info->setId(column_value[0]);
  7.         info->setName(column_value[1]);
  8.         info->setScore(column_value[2]);
  9.         info->setAward(column_value[3]);*/ 
  10.         DBUtils::list->addObject(info);  
  11.  
  12.         info->autorelease();  
  13.         return 0;  
[cpp] view plaincopyprint?
  1. int loadRecord( void * para, int n_column,char ** column_value, char ** column_name ) 
  2.      
  3.          
  4.         InfoBean * info=new InfoBean(column_value[0],column_value[1],column_value[2],column_value[3]); 
  5.         /*info->setId(column_value[0]);
  6.         info->setName(column_value[1]);
  7.         info->setScore(column_value[2]);
  8.         info->setAward(column_value[3]);*/ 
  9.         DBUtils::list->addObject(info); 
  10.  
  11.         info->autorelease(); 
  12.         return 0; 
 

定义一个类的静态成员函数

Cpp代码 复制代码 收藏代码
  1. void DBUtils::listAll(sqlite3 *pDB)  
  2. {  
  3.     DBUtils::list=new CCSet();  
  4.     list->autorelease();  
  5.     char * errMsg=NULL;  
  6.     std::string sqlstr="select * from Info order by score desc limit 0,10";   
  7.     int result = sqlite3_exec( pDB, sqlstr.c_str() ,loadRecord, NULL, &errMsg );   
  8.       
  9.       
[cpp] view plaincopyprint?
  1. void DBUtils::listAll(sqlite3 *pDB) 
  2.     DBUtils::list=new CCSet(); 
  3.     list->autorelease(); 
  4.     char * errMsg=NULL; 
  5.     std::string sqlstr="select * from Info order by score desc limit 0,10";  
  6.     int result = sqlite3_exec( pDB, sqlstr.c_str() ,loadRecord, NULL, &errMsg );  
  7.      
  8.      
 

当调用listAll的时候,执行到sqlite3_exec的时候,会回调loadRecord函数,一条数据调用一次

遍历CCSet

Cpp代码 复制代码 收藏代码
  1. CCSetIterator it;  
  2. InfoBean *bean;  
  3. CCSet * list=listAllRecord();  
  4. CCLOG("%s","hello");  
  5. int position[]={300,606,300,530,300,454,300,378,300,302,760,606,760,530,760,454,760,378,760,302};  
  6. int i=0;  
  7. for(it=list->begin();it!=list->end();it++)  
  8. {  
  9.      bean=(InfoBean *)(*it);  
  10.      if(!bean)  
  11.          break;  
  12.      CCLOG("%s",bean->getName());  
  13.      CCLabelTTF *m_pLabelName = CCLabelTTF::labelWithString(bean->getName(),"Arial", 30);  
  14.      m_pLabelName->setColor(ccc3(255,0,0));  
  15.      int temp_one=2*i;  
  16.      int temp_two=(i+1)*2-1;  
  17.      m_pLabelName->setPosition(CCPointMake(position[temp_one],position[temp_two]));  
  18.      this->addChild(m_pLabelName);  
  19.      CCLabelTTF *m_pLabelScore = CCLabelTTF::labelWithString(bean->getScore(),"Arial", 30);  
  20.      m_pLabelScore->setColor(ccc3(255,0,0));  
  21.  
  22.      m_pLabelScore->setPosition(CCPointMake(position[temp_one]+150,position[temp_two]));  
  23.      this->addChild(m_pLabelScore);  
  24.  
  25.  
  26.      i++;  
0 0
原创粉丝点击