/TGame/TUtil/Config/ConfigWriterSqlite.cpp

http://awoe.googlecode.com/ · C++ · 74 lines · 56 code · 8 blank · 10 comment · 6 complexity · 67ff9798dac1795c62ec7b5160ae7870 MD5 · raw file

  1. #include "stdafx.h"
  2. #include "ConfigWriterSqlite.h"
  3. #include "SQLite\DB_SQLite.h"
  4. #include "Log\Log.h"
  5. namespace woe
  6. {
  7. ConfigWriterSqlite* ConfigWriterSqlite::m_Instance = NULL;
  8. ConfigWriterSqlite::ConfigWriterSqlite()
  9. {
  10. //InitializeCriticalSection( &m_csWriteSqlite );
  11. }
  12. ConfigWriterSqlite::~ConfigWriterSqlite()
  13. {
  14. //DeleteCriticalSection( &m_csWriteSqlite );
  15. if ( m_Instance )
  16. {
  17. delete m_Instance;
  18. m_Instance = NULL;
  19. }
  20. }
  21. bool ConfigWriterSqlite::DoSave(const std::string& strFileName, IConfigOperator* config)
  22. {
  23. if ( !config )
  24. {
  25. LOG_DEBUG("Failed to , ConfigWriterSqlite::DoSave, the param config is NULL\n");
  26. return false;
  27. }
  28. CSQLiteBase* pSQLiteBase = new CSQLiteBase;
  29. if ( !pSQLiteBase )
  30. {
  31. LOG_DEBUG("Failed to , ConfigWriterSqlite::DoSave, new CSQLiteBase failed\n");
  32. return false;
  33. }
  34. //const char* s = ".db";
  35. //std::string newdb = strFileName;
  36. //newdb.append(s);
  37. //
  38. //remove( newdb.c_str() );
  39. //::CreateFile( newdb.c_str(), GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  40. //::EnterCriticalSection( &m_csWriteSqlite );
  41. if ( !pSQLiteBase->Open( strFileName.c_str() ) )
  42. {
  43. LOG_DEBUG_V("Failed to Open db %s ,ConfigWriterSqlite::DoSave\n",strFileName.c_str());
  44. return false;
  45. }
  46. char szSQL[256] = "delete from tbl ";
  47. CppSQLite3DB* db = pSQLiteBase->GetDB();
  48. db->execDML( szSQL );
  49. std::string key , value;
  50. while( config->GetNext(key,value) )
  51. {
  52. sprintf( szSQL, "insert into tbl values('%s','%s');",key.c_str(),value.c_str() );
  53. db->execDML( szSQL );
  54. }
  55. //::LeaveCriticalSection( &m_csWriteSqlite );
  56. return true;
  57. }
  58. ConfigWriterSqlite* ConfigWriterSqlite::GetInstance()
  59. {
  60. if ( !m_Instance )
  61. {
  62. m_Instance = new ConfigWriterSqlite;
  63. }
  64. return m_Instance;
  65. }
  66. }