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





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章