IMDB使用范例


#include<iostream>
#include<vector>
#include "imdbdll.h"
//编译说明:如果编译出现 _stdcall编译问题,增加windows.h头文件的包含
int main()
{
	bool g_blnIsOpen=false;
	IMDBHANDLE g_hIMDB = NULL;
	g_hIMDB = IMDB_Create();
	if(g_hIMDB != NULL)
	{
		if(!IMDB_IsOpen(g_hIMDB))
		{
			if(IMDB_Open(g_hIMDB))
			{
				g_blnIsOpen = true;
			}
			else
			{
				int nRet = IMDB_GetLastErrorNumber(g_hIMDB);
				std::cout<<IMDB_GetErrorDescription(g_hIMDB, nRet)<<std::endl;
			}
		}
	}
	if(0 == IMDB_Exist(g_hIMDB, "SELECT * FROM QSYS_TABLE_INFO WHERE TNAME = 'process_flag' "))
	{ 
		//不存在,创建
		char szCmd[512] = {0,};
		strcpy( szCmd, "CREATE TABLE SHARED process_flag( processid int KEY, flag int)" );
		if( !IMDB_ExecSql(g_hIMDB, szCmd ) )
		{
			int nRet = IMDB_GetLastErrorNumber(g_hIMDB);
			std::cout<<IMDB_GetErrorDescription(g_hIMDB, nRet)<<std::endl;
			//OutputDebugString(DEBUGMSG(TRUE,"IMDB execute sql %s failed, errno=%d, errmsg=%s", szCmd, nRet, IMDB_GetErrorDescription(g_hIMDB, nRet)));
		}
	}
	IMDB_ClearAllLocksOnTable(g_hIMDB,"process_flag");
	IMDB_ExecSql(g_hIMDB, "delete * from process_flag where processid = 1" ) ;
	std::vector<int> g_nProcessIdVec;
	g_nProcessIdVec.push_back(1);
#if 0
	//IMDB_ClearAllLocksOnTable(g_hIMDB,"process_flag");
	for (int i = 0;i < g_nProcessIdVec.size();i++)
	{
		char szCmd[512] = {0};
		_snprintf(szCmd,sizeof(szCmd)-1,"INSERT INTO process_flag (processid,flag) VALUES (%d,1000)",g_nProcessIdVec[i]);

		if( !IMDB_ExecSql(g_hIMDB, szCmd ) )
		{
			int nRet = IMDB_GetLastErrorNumber(g_hIMDB);
			std::cout<<IMDB_GetErrorDescription(g_hIMDB, nRet)<<std::endl;
			//OutputDebugString(CDebugMsg("IMDB execute sql %s failed, errno=%d, errmsg=%s", szCmd, nRet, IMDB_GetErrorDescription(g_hIMDB, nRet)));
		}
	}

#endif
	char *pData = "1,5000\r2,1000\r3,10000\r";
	IMDB_ImportFromMemory(g_hIMDB,"process_flag",pData,',','\r');
	char szCmd[512] = {0};
	_snprintf(szCmd,sizeof(szCmd)-1,"SELECT * from process_flag  ");
	char *pBuffer = NULL;
	long lLen = 0;
	pBuffer = IMDB_ExportToMemory(g_hIMDB,"process_flag",(unsigned long*)&lLen,',','&');
	if( !IMDB_ExecSql(g_hIMDB, szCmd ) )
	{
		int nRet = IMDB_GetLastErrorNumber(g_hIMDB);
		std::cout<<IMDB_GetErrorDescription(g_hIMDB, nRet)<<std::endl;
	}

	int m_nRowCount = IMDB_RowCount(g_hIMDB);
	while (IMDB_Next(g_hIMDB))
	{
		int flag =IMDB_GetIntVal(g_hIMDB,1);
		std::cout<<flag<<std::endl;
		std::cout<<IMDB_GetIntVal(g_hIMDB,2)<<std::endl;
	}

	IMDB_ExecSql(g_hIMDB, "delete * from process_flag where processid = 1 " ) ;
	int nRet = IMDB_GetLastErrorNumber(g_hIMDB);
	std::cout<<IMDB_GetErrorDescription(g_hIMDB, nRet)<<std::endl;

	if (IMDB_IsOpen(g_hIMDB))
	{
		IMDB_Close(g_hIMDB);
	}

	IMDB_Destroy(g_hIMDB);
}