[置顶] cocos2dx sqllite 增删查改等操作

首先导入文件shell.c sqllite3.c sqlite3.h sqlite3etx.h文件(注意在生成安卓项目是 不要将shell.c写进android.mk文件中,写进去在cywin中生成会出错,当时搞了很久发现的)

创建数据库

 

	sqlite3 *pDB = NULL;			//数据库指针  

	char * errMsg = NULL;			//错误信息

	std::string sqlstr;				//SQL指令 

	int result;						//sqlite3_exec返回值

	bool isExisted_;				//判断是否开始

	char db[100]="";

 	std::string path = CCFileUtils::sharedFileUtils()->getWritablePath(); 

 	strcpy(db, path.c_str());

 	strcat(db,"save.db");



	result = sqlite3_open(db, &pDB);//如果不存在及新建数据库,存在及打开



	sqlstr="select count(type) from sqlite_master where type='table' and name='achievement_t'";//是否存在这个表

	sqlite3_exec( pDB, sqlstr.c_str() , ::isExisted, &isExisted_, &errMsg );//不存在 及isExisted_为false

 

函数isExisted

 

int isExisted( void * para, int n_column, char ** column_value, char ** column_name ) 

{ 

	bool *isExisted_=(bool*)para; 

	*isExisted_=(**column_value)!='0'; 

	return 0; 

}

 

创建表

 

result=sqlite3_exec( pDB, "create table achievement_t( id integer primary key autoincrement, stute integer ) " , NULL, NULL, &errMsg );

插入数据

 

 

		sqlstr=" insert into achievement_t( stute) values ( 0 ) "; 

		for(int i=0;i<15;i++){

			result = sqlite3_exec( pDB, sqlstr.c_str() , NULL, NULL, &errMsg );

		}


以上是我做项目的

 

对于那些增删查改如下例子

 

	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 ); 



		//创建表,设置ID为主键,且自动增加 

		result=sqlite3_exec( pDB, 

			"create table MyTable_1( ID integer primary key autoincrement, name nvarchar(32) ) " ,

			NULL, NULL, &errMsg ); 

		if( result != SQLITE_OK ) 

			CCLog( "创建表失败,错误码:%d ,错误原因: %s\n" , result, errMsg ); 



		//插入数据 

		sqlstr=" insert into MyTable_1( name ) values ( '克塞' ) "; 

		result = sqlite3_exec( pDB, sqlstr.c_str() , NULL, NULL, &errMsg ); 

		if(result != SQLITE_OK ) 

			CCLog( "插入记录失败,错误码:%d ,错误原因:%s\n" , result, errMsg ); 



		//插入数据 

		sqlstr=" insert into MyTable_1( name ) values ( '葫芦' ) "; 

		result = sqlite3_exec( pDB, sqlstr.c_str() , NULL, NULL, &errMsg ); 

		if(result != SQLITE_OK ) 

			CCLog( "插入记录失败,错误码:%d ,错误原因:%s\n" , result, errMsg ); 



		//插入数据 

		sqlstr=" insert into MyTable_1( name ) values ( '擎天' ) "; 

		result = sqlite3_exec( pDB, sqlstr.c_str() , NULL, NULL, &errMsg ); 

		if(result != SQLITE_OK ) 

			CCLog( "插入记录失败,错误码:%d ,错误原因:%s\n" , result, errMsg ); 

		//查询语句

		sqlstr="select * from MyTable_1"; 

		result= sqlite3_exec( pDB, sqlstr.c_str() ,loadRecord, 0, &errMsg );  

		//关闭数据库 

		sqlite3_close(pDB); 

回到我自己项目需要批量查询我用

	sqlite3 *pDB = NULL;			//数据库指针  

	char * errMsg = NULL;			//错误信息

	std::string sqlstr;				//SQL指令

	char db[100]="";

	std::string path = CCFileUtils::sharedFileUtils()->getWritablePath(); 

	strcpy(db, path.c_str());

	strcat(db,"save.db");

	int* count=new int[15];

	sqlite3_open(db, &pDB);

	for(int i=0;i<15;i++){

		char temp[80];

		int* inttemp=new int[1];

		sprintf(temp, "select stute from achievement_t where ID = %d",i+1);

		sqlstr=temp;

		sqlite3_exec( pDB, sqlstr.c_str() ,  DataControl::queryCallBack, inttemp, &errMsg );

		*(count+i)=*inttemp;

	}

	sqlite3_close(pDB);	

	return count;

添加这个函数(这个我的理解也不够清楚 我就照着别人写的 大家指教一下哈)

 

 

int DataControl::queryCallBack(void* para,int n_column,char** column_value,char** column_name)

{

	//para是你在sqlite3_exec 里传入的void*参数通过para参数,你可以传入一些特殊的指针(比如类指针、结构指针),

	//然后在这里面强制转换成对应的类型(这里面是void*类型,必须强制转换成你的类型才可用)。然后操作这些数据

	//n_column是这一条记录有多少个字段(即这条记录有多少列)

	//char** column_value 是个关键值,查出来的数据都保存在这里,它实际上是个1维数组(不要以为是2维数组),

	//每一个元素都是一个char*值,是一个字段内容(用字符串来表示,以\0结尾)

	//char** column_name 跟column_value是对应的,表示这个字段的字段名称

	int *temp = (int*)para;

	int count=0;

	sscanf(*(column_name+1),"%d",&count);

	*temp=count;

	return 0;

}

如插入批量数据

 

 

	sqlite3 *pDB = NULL;			//数据库指针  

	char * errMsg = NULL;			//错误信息

	char db[100]="";

	std::string path = CCFileUtils::sharedFileUtils()->getWritablePath(); 

	strcpy(db, path.c_str());

	strcat(db,"save.db");

	sqlite3_open(db, &pDB);

	std::string sqlstr;//SQL指令

	char temp[80];

	for(int i=0;i<8;i++){

		sprintf(temp, "update prop_t set rank=%d where ID = %d",prop[i],i+1);

		sqlstr=temp;

		sqlite3_exec( pDB, sqlstr.c_str() , NULL, NULL, &errMsg );

	}	

	sqlite3_close(pDB);


可下载例子 :http://download.csdn.net/detail/five50/5663775

 





 

你可能感兴趣的:(cocos2dx)