下载附件,解压,添加到工程中,加入头文件
- include "sqlite/sqlite3.h"
- include "sqlite/sqlite3.h"
创建数据库的代码 返回sqlite3的一个指针,通过该指针可以添加、修改、删除数据
- sqlite3 * DBUtils::creatTable()
- {
- sqlite3 *pDB = NULL;//数据库指针
- char * errMsg = NULL;//错误信息
- std::string sqlstr;//SQL指令
- int result;//sqlite3_exec返回值
- //打开一个数据库,如果该数据库不存在,则创建一个数据库文件
- result = sqlite3_open("save.db",&pDB);
- if( result != SQLITE_OK )
- // CCLog( "打开数据库失败,错误码:%d ,错误原因:%s\n",result,errMsg );
- cout<<errMsg<<endl;
- //创建表,设置ID为主键,且自动增加
- result=sqlite3_exec( pDB, "create table Info( id integer primary key autoincrement,name nvarchar(32),score nvarchar(50),award nvarchar(50)) ",NULL,&errMsg );
- return pDB;
- // sqlite3_close(pDB);
- }
- sqlite3 * DBUtils::creatTable()
- {
- sqlite3 *pDB = NULL;//数据库指针
- char * errMsg = NULL;//错误信息
- std::string sqlstr;//SQL指令
- int result;//sqlite3_exec返回值
- //打开一个数据库,如果该数据库不存在,则创建一个数据库文件
- result = sqlite3_open("save.db",&pDB);
- if( result != SQLITE_OK )
- // CCLog( "打开数据库失败,错误码:%d ,错误原因:%s\n",errMsg );
- cout<<errMsg<<endl;
- //创建表,设置ID为主键,且自动增加
- result=sqlite3_exec( pDB,"create table Info( id integer primary key autoincrement,award nvarchar(50)) ",&errMsg );
- return pDB;
- // sqlite3_close(pDB);
- }
通过创建数据库返回的指针,保存数据
- int DBUtils::save(sqlite3 *pDB)
- {
- char * errMsg=NULL;
- std::string sqlstr=" insert into Info(name,score,award) values ( '1','1','1' ) ";
- int result = sqlite3_exec( pDB,sqlstr.c_str(),&errMsg );
- return result;
- }
- int DBUtils::save(sqlite3 *pDB)
- {
- char * errMsg=NULL;
- std::string sqlstr=" insert into Info(name,'1' ) ";
- int result = sqlite3_exec( pDB,&errMsg );
- return result;
- }
查询数据
定义一个全局函数,也就是在类外定义
- int loadRecord( void * para,int n_column, char ** column_value,char ** column_name )
- {
- InfoBean * info=new InfoBean(column_value[0],column_value[1],column_value[2],column_value[3]);
- /*info->setId(column_value[0]);
- info->setName(column_value[1]);
- info->setScore(column_value[2]);
- info->setAward(column_value[3]);*/
- DBUtils::list->addObject(info);
- info->autorelease();
- return 0;
- }
- int loadRecord( void * para,int n_column,char ** column_value,char ** column_name )
- {
- InfoBean * info=new InfoBean(column_value[0],column_value[3]);
- /*info->setId(column_value[0]);
- info->setName(column_value[1]);
- info->setScore(column_value[2]);
- info->setAward(column_value[3]);*/
- DBUtils::list->addObject(info);
- info->autorelease();
- return 0;
- }
定义一个类的静态成员函数
- void DBUtils::listAll(sqlite3 *pDB)
- {
- DBUtils::list=new CCSet();
- list->autorelease();
- char * errMsg=NULL;
- std::string sqlstr="select * from Info order by score desc limit 0,10";
- int result = sqlite3_exec( pDB,loadRecord,&errMsg );
- }
- void DBUtils::listAll(sqlite3 *pDB)
- {
- DBUtils::list=new CCSet();
- list->autorelease();
- char * errMsg=NULL;
- std::string sqlstr="select * from Info order by score desc limit 0,10";
- int result = sqlite3_exec( pDB,&errMsg );
- }
当调用listAll的时候,执行到sqlite3_exec的时候,会回调loadRecord函数,一条数据调用一次
遍历CCSet
- CCSetIterator it;
- InfoBean *bean;
- CCSet * list=listAllRecord();
- CCLOG("%s","hello");
- int position[]={300,606,300,530,454,378,302,760,302};
- int i=0;
- for(it=list->begin();it!=list->end();it++)
- {
- bean=(InfoBean *)(*it);
- if(!bean)
- break;
- CCLOG("%s",bean->getName());
- CCLabelTTF *m_pLabelName = CCLabelTTF::labelWithString(bean->getName(),"Arial",30);
- m_pLabelName->setColor(ccc3(255,0));
- int temp_one=2*i;
- int temp_two=(i+1)*2-1;
- m_pLabelName->setPosition(CCPointMake(position[temp_one],position[temp_two]));
- this->addChild(m_pLabelName);
- CCLabelTTF *m_pLabelScore = CCLabelTTF::labelWithString(bean->getScore(),30);
- m_pLabelScore->setColor(ccc3(255,0));
- m_pLabelScore->setPosition(CCPointMake(position[temp_one]+150,position[temp_two]));
- this->addChild(m_pLabelScore);
- i++;
- }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。