c++ Sqlite 簡單使用-查詢

BOOL CFileConfigure::LoadSQLiteConfig( const CString& szField, CFieldEx* pField)
{
	USES_CONVERSION;
	CString szPath, szSql;
	szPath.Format(_T("%s\\stat\\normal_conf.data"), CApplication::ConfigDir());
	sqlite3 *pDb = NULL;
	sqlite3_stmt* pStmt = NULL;
	
	auto MbcsToUtf8 = [](const char *file) -> char*
	{
		WCHAR   *pwchar=0;
		CHAR    *pchar = 0;
		int len=0;

		int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
		len=MultiByteToWideChar(codepage, 0, file, -1, NULL,0);

		pwchar=new WCHAR[len];
		ASSERT(pwchar);

		if(pwchar!=0)
		{
			len = MultiByteToWideChar(codepage, 0, file, -1, pwchar, len);

			if( len!=0 ) 
			{
				len = WideCharToMultiByte(CP_UTF8, 0, pwchar, -1, 0, 0, 0, 0);
				pchar=new CHAR[len];
				ASSERT(pchar);

				if(pchar!=0)
				{
					len = WideCharToMultiByte(CP_UTF8, 0, pwchar, -1, pchar, len,0, 0);

					if(len!=0)                 
					{
						delete pwchar; 
						return pchar;                    
					}
				}
				delete pwchar; 
			}
		}
		return pchar;
	};

	// 中文路徑支持
	char* pPath = MbcsToUtf8(CT2CA(szPath));
	if( sqlite3_open( pPath, &pDb) != SQLITE_OK)
	{
		delete [] pPath;
		return FALSE;
	}
	
	delete [] pPath;
	szSql.Format( _T("select name, caption, \"type\", formula, formulaname, formulaparam, drill, lua from indexconfig where upper(name) = upper('%s')"), szField);
	int rc = sqlite3_prepare( pDb, CT2CA(szSql), -1, &pStmt, NULL);
	rc = sqlite3_step( pStmt);
	if( rc == SQLITE_ROW )
	{
		stFormula* pFormula = new stFormula();
		pFormula->szCaption = (TCHAR*)sqlite3_column_text16( pStmt, 1);
		pFormula->uType = sqlite3_column_int( pStmt, 2);
		pFormula->uLength = sqlite3_column_int( pStmt, 2) == 3 ? 64 : 8;
		pFormula->uFormulaType = sqlite3_column_int( pStmt, 3);
		pFormula->szFormulaName = (TCHAR*)sqlite3_column_text16( pStmt, 4);
		pFormula->szFormulaParam = (TCHAR*)sqlite3_column_text16( pStmt, 5);
		pFormula->szFormulaLua = (TCHAR*)sqlite3_column_text16( pStmt, 7);
		pFormula->bEnableDrill =  pField->iColFlag & cfDelay ? TRUE : sqlite3_column_int( pStmt, 6);
		pFormula->bIsDim = pField->iColFlag & cfDimension;

		m_vecIndexTrans[ string(CT2CA(szField))] = pFormula;
	}

	sqlite3_finalize( pStmt);
	sqlite3_close( pDb);
	
	return rc == SQLITE_ROW;
}

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