Explorar o código

sqllite创建的优化

zhangyang %!s(int64=6) %!d(string=hai) anos
pai
achega
642f45bb96

BIN=BIN
bin/Win32/Debug/zhipuzi_pos_windows/db/pos.db


BIN=BIN
bin/Win32/Debug/zhipuzi_pos_windows/zhipuzi_pos_windows.exe


+ 22 - 1
lewaimai_dispatch/helper/CSystem.h

@@ -42,7 +42,7 @@ public:
             RegSetValueEx(hKey, L"智铺子收银软件", 0, REG_SZ, (LPBYTE)strExeFullDir, (lstrlen(strExeFullDir) + 1) * sizeof(TCHAR));
 
             //4、关闭注册表
-            RegCloseKey(hKey); 
+            RegCloseKey(hKey);
         }
 
         else
@@ -67,5 +67,26 @@ public:
             RegCloseKey(hKey);
         }
     }
+
+    // 判断文件是否存在
+    static BOOL IsFileExist(const wstring& csFile)
+    {
+        DWORD dwAttrib = GetFileAttributes(csFile.c_str());
+        return INVALID_FILE_ATTRIBUTES != dwAttrib && 0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
+    }
+
+    // 判断文件夹是否存在
+    static BOOL IsDirExist(const wstring& csDir)
+    {
+        DWORD dwAttrib = GetFileAttributes(csDir.c_str());
+        return INVALID_FILE_ATTRIBUTES != dwAttrib && 0 != (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
+    }
+
+    // 判断文件或文件夹是否存在
+    static BOOL IsPathExist(const wstring& csPath)
+    {
+        DWORD dwAttrib = GetFileAttributes(csPath.c_str());
+        return INVALID_FILE_ATTRIBUTES != dwAttrib;
+    }
 };
 

+ 3 - 3
lewaimai_dispatch/lewaimai_dispatch_windows.vcxproj

@@ -123,18 +123,18 @@ copy $(ProjectDir)conf\ $(SolutionDir)bin\$(Platform)\$(Configuration)\conf\</Co
       <WarningLevel>Level3</WarningLevel>
       <Optimization>Disabled</Optimization>
       <SDLCheck>true</SDLCheck>
-      <PreprocessorDefinitions>_DEBUG;_WINDOWS;_CRT_SECURE_NO_WARNINGS;_WIN32_WINNT=0x600;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <PreprocessorDefinitions>_DEBUG;_WINDOWS;_CRT_SECURE_NO_WARNINGS;_WIN32_WINNT=0x600;LOG4CPLUS_DISABLE_DLL_RUNTIME_WARNING;%(PreprocessorDefinitions)</PreprocessorDefinitions>
       <ConformanceMode>false</ConformanceMode>
       <PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
       <AdditionalIncludeDirectories>$(SolutionDir)include;$(ProjectDir)pch</AdditionalIncludeDirectories>
       <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
+      <DisableSpecificWarnings>4099</DisableSpecificWarnings>
     </ClCompile>
     <Link>
       <SubSystem>Windows</SubSystem>
       <GenerateDebugInformation>true</GenerateDebugInformation>
       <AdditionalLibraryDirectories>$(SolutionDir)lib\debug</AdditionalLibraryDirectories>
-      <AdditionalDependencies>DuiLib_ud.lib;log4cplusUD.lib;dbghelp.lib;winmm.lib;setupapi.lib;AdvAPI32.lib;Shell32.lib;user32.lib;kernel32.lib;Gdi32.lib;sqlite3.lib;libboost_date_time-vc141-mt-sgd-x32-1_70.lib;libboost_regex-vc141-mt-sgd-x32-1_70.lib;libcurl.lib;%(AdditionalDependencies)</AdditionalDependencies>
-      <AdditionalOptions>/ignore:4099 %(AdditionalOptions)</AdditionalOptions>
+      <AdditionalDependencies>dbghelp.lib;winmm.lib;setupapi.lib;AdvAPI32.lib;Shell32.lib;user32.lib;kernel32.lib;Gdi32.lib;libboost_date_time-vc141-mt-sgd-x32-1_70.lib;libboost_regex-vc141-mt-sgd-x32-1_70.lib;sqlite3.lib;libcurl.lib;DuiLib_ud.lib;log4cplusUD.lib;%(AdditionalDependencies)</AdditionalDependencies>
       <IgnoreSpecificDefaultLibraries>
       </IgnoreSpecificDefaultLibraries>
       <Version>

+ 133 - 127
lewaimai_dispatch/tool/CSqlite3.cpp

@@ -5,9 +5,16 @@
 
 CSqlite3::CSqlite3()
 {
+    wstring folderPath = CSystem::GetProgramDir() + L"\\db";
+	if (!CSystem::IsDirExist(folderPath))
+	{
+		bool flag = CreateDirectory(folderPath.c_str(), NULL);
+		bool a = flag;
+	}
+
     //如果没有这个文件,这里会创建这个文件
-	wstring path = CSystem::GetProgramDir() + L"\\db\\pos.db";
-	string s_path = CLewaimaiString::UnicodeToUTF8(path);
+    wstring path = CSystem::GetProgramDir() + L"\\db\\pos.db";
+    string s_path = CLewaimaiString::UnicodeToUTF8(path);
 
     m_rc = sqlite3_open(s_path.c_str(), &m_db);
 
@@ -23,7 +30,6 @@ CSqlite3::CSqlite3()
     }
 }
 
-
 CSqlite3::~CSqlite3()
 {
     if(m_db != NULL)
@@ -141,8 +147,8 @@ bool CSqlite3::InitConfig()
                 stmt = NULL;
 
                 sql = "CREATE TABLE pos_chufang_printer("  \
-					  "id         INTEGER          PRIMARY KEY AUTOINCREMENT,"\
-					  "date       CHAR(100)        NOT NULL," \
+                      "id         INTEGER          PRIMARY KEY AUTOINCREMENT,"\
+                      "date       CHAR(100)        NOT NULL," \
                       "name       CHAR(100)        NOT NULL," \
                       "ip         CHAR(100)        NOT NULL," \
                       "guige      CHAR(100)        NOT NULL," \
@@ -155,7 +161,7 @@ bool CSqlite3::InitConfig()
                     //执行该语句
                     if(sqlite3_step(stmt) != SQLITE_DONE)
                     {
-						std::string err = sqlite3_errmsg(m_db);
+                        std::string err = sqlite3_errmsg(m_db);
                         LOG_INFO("create table fail: " << err.c_str());
 
                         sqlite3_finalize(stmt);
@@ -190,7 +196,7 @@ bool CSqlite3::InitConfig()
             {
                 while(sqlite3_step(stmt) == SQLITE_ROW)
                 {
-					std::string date = (char*)sqlite3_column_text(stmt, 1);
+                    std::string date = (char*)sqlite3_column_text(stmt, 1);
                     std::string name = (char*)sqlite3_column_text(stmt, 2);
                     std::string ip = (char*)sqlite3_column_text(stmt, 3);
                     std::string guige = (char*)sqlite3_column_text(stmt, 4);
@@ -198,7 +204,7 @@ bool CSqlite3::InitConfig()
                     std::string fenlei = (char*)sqlite3_column_text(stmt, 6);
                     std::string fenlei_ids = (char*)sqlite3_column_text(stmt, 7);
 
-					//这里仅仅是把数据库内容读到内存,所以之类用false
+                    //这里仅仅是把数据库内容读到内存,所以之类用false
                     CSetting::AddChufangPrinter(date, name, ip, guige, fendan, fenlei, fenlei_ids, false);
                 }
 
@@ -214,84 +220,84 @@ bool CSqlite3::InitConfig()
         }
     }
 
-	sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_user';";
+    sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_user';";
 
-	//读取厨房打印机的参数
-	if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
-	{
-		if (sqlite3_step(stmt) == SQLITE_ROW)
-		{
-			int count = sqlite3_column_int(stmt, 0);
-
-			if (count == 0)
-			{
-				//说明没找到这个表,那么这个时候新建这个表,先释放前面的stmt
-				sqlite3_finalize(stmt);
-				stmt = NULL;
-
-				sql = "CREATE TABLE pos_user("  \
-					"id         INTEGER          PRIMARY KEY AUTOINCREMENT,"\
-					"username       CHAR(100)        NOT NULL," \
-					"password         CHAR(100)        NOT NULL);";
-
-				if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
-				{
-					//执行该语句
-					if (sqlite3_step(stmt) != SQLITE_DONE)
-					{
-						std::string err = sqlite3_errmsg(m_db);
-						LOG_INFO("create table fail: " << err.c_str());
-
-						sqlite3_finalize(stmt);
-						return false;
-					}
-
-					//走到这里就是表创建成功了
-					LOG_INFO("create table success");
-					sqlite3_finalize(stmt);
-				}
-
-				else
-				{
-					LOG_INFO("create table prepare fail: " << sqlite3_errmsg(m_db));
-
-					sqlite3_finalize(stmt);
-
-					return false;
-				}
-			}
-
-			else
-			{
-				//说明已经有这个表了,就不用再创建了
-				sqlite3_finalize(stmt);
-			}
-
-			std::string sql = "SELECT * FROM pos_user;";
-			sqlite3_stmt * stmt = NULL;
-
-			if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
-			{
-				while (sqlite3_step(stmt) == SQLITE_ROW)
-				{
-					std::string username = (char*)sqlite3_column_text(stmt, 1);
-					std::string password = (char*)sqlite3_column_text(stmt, 2);
-
-					//这里仅仅是把数据库内容读到内存,所以之类用false
-					CSetting::SetUser(username, password);
-				}
-
-				sqlite3_finalize(stmt);
-			}
-
-			else
-			{
-				//异常情况
-				sqlite3_finalize(stmt);
-				return false;
-			}
-		}
-	}
+    //读取厨房打印机的参数
+    if(sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
+    {
+        if(sqlite3_step(stmt) == SQLITE_ROW)
+        {
+            int count = sqlite3_column_int(stmt, 0);
+
+            if(count == 0)
+            {
+                //说明没找到这个表,那么这个时候新建这个表,先释放前面的stmt
+                sqlite3_finalize(stmt);
+                stmt = NULL;
+
+                sql = "CREATE TABLE pos_user("  \
+                      "id         INTEGER          PRIMARY KEY AUTOINCREMENT,"\
+                      "username       CHAR(100)        NOT NULL," \
+                      "password         CHAR(100)        NOT NULL);";
+
+                if(sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
+                {
+                    //执行该语句
+                    if(sqlite3_step(stmt) != SQLITE_DONE)
+                    {
+                        std::string err = sqlite3_errmsg(m_db);
+                        LOG_INFO("create table fail: " << err.c_str());
+
+                        sqlite3_finalize(stmt);
+                        return false;
+                    }
+
+                    //走到这里就是表创建成功了
+                    LOG_INFO("create table success");
+                    sqlite3_finalize(stmt);
+                }
+
+                else
+                {
+                    LOG_INFO("create table prepare fail: " << sqlite3_errmsg(m_db));
+
+                    sqlite3_finalize(stmt);
+
+                    return false;
+                }
+            }
+
+            else
+            {
+                //说明已经有这个表了,就不用再创建了
+                sqlite3_finalize(stmt);
+            }
+
+            std::string sql = "SELECT * FROM pos_user;";
+            sqlite3_stmt * stmt = NULL;
+
+            if(sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
+            {
+                while(sqlite3_step(stmt) == SQLITE_ROW)
+                {
+                    std::string username = (char*)sqlite3_column_text(stmt, 1);
+                    std::string password = (char*)sqlite3_column_text(stmt, 2);
+
+                    //这里仅仅是把数据库内容读到内存,所以之类用false
+                    CSetting::SetUser(username, password);
+                }
+
+                sqlite3_finalize(stmt);
+            }
+
+            else
+            {
+                //异常情况
+                sqlite3_finalize(stmt);
+                return false;
+            }
+        }
+    }
 
     return true;
 }
@@ -326,61 +332,61 @@ bool CSqlite3::SaveParams(std::map<std::string, std::string>& params)
 
 bool CSqlite3::SaveChufangPrinter(std::vector<ChufangPrinter>& printers)
 {
-	int result = sqlite3_exec(m_db, "BEGIN;", 0, 0, 0);
+    int result = sqlite3_exec(m_db, "BEGIN;", 0, 0, 0);
 
-	std::string sql = "delete from pos_chufang_printer;";
-	result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
+    std::string sql = "delete from pos_chufang_printer;";
+    result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
 
-	for (std::vector<ChufangPrinter>::iterator it = printers.begin(); it != printers.end(); it++)
-	{
-		std::string date = (*it).date;
-		std::string name = (*it).name;
-		std::string ip = (*it).ip;
-		std::string guige = (*it).guige;
-		std::string fendan = (*it).fendan;
-		std::string fenlei = (*it).fenlei;
-		std::string fenlei_ids = (*it).fenlei_ids;
-
-		sql = "INSERT INTO pos_chufang_printer (date, name, ip, guige, fendan, fenlei, fenlei_ids) VALUES ('" + date + "' ,'" + name + "','" + ip + "','" + guige + "','" + fendan + "','" + fenlei + "','" + fenlei_ids + "')";
-		result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
-	}
+    for(std::vector<ChufangPrinter>::iterator it = printers.begin(); it != printers.end(); it++)
+    {
+        std::string date = (*it).date;
+        std::string name = (*it).name;
+        std::string ip = (*it).ip;
+        std::string guige = (*it).guige;
+        std::string fendan = (*it).fendan;
+        std::string fenlei = (*it).fenlei;
+        std::string fenlei_ids = (*it).fenlei_ids;
+
+        sql = "INSERT INTO pos_chufang_printer (date, name, ip, guige, fendan, fenlei, fenlei_ids) VALUES ('" + date + "' ,'" + name + "','" + ip + "','" + guige + "','" + fendan + "','" + fenlei + "','" + fenlei_ids + "')";
+        result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
+    }
 
-	result = sqlite3_exec(m_db, "COMMIT;", 0, 0, 0);
+    result = sqlite3_exec(m_db, "COMMIT;", 0, 0, 0);
 
-	if (result == SQLITE_OK)
-	{
-		LOG_INFO("save params success");
-		return true;
-	}
+    if(result == SQLITE_OK)
+    {
+        LOG_INFO("save params success");
+        return true;
+    }
 
-	LOG_INFO("save params fail");
-	return false;
+    LOG_INFO("save params fail");
+    return false;
 }
 
 bool CSqlite3::SaveUsers(std::map<string, string> users)
 {
-	int result = sqlite3_exec(m_db, "BEGIN;", 0, 0, 0);
+    int result = sqlite3_exec(m_db, "BEGIN;", 0, 0, 0);
 
-	std::string sql = "delete from pos_user;";
-	result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
+    std::string sql = "delete from pos_user;";
+    result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
 
-	for (std::map<std::string, std::string>::iterator it = users.begin(); it != users.end(); it++)
-	{
-		std::string name = it->first;
-		std::string password = it->second;
+    for(std::map<std::string, std::string>::iterator it = users.begin(); it != users.end(); it++)
+    {
+        std::string name = it->first;
+        std::string password = it->second;
 
-		sql = "INSERT INTO pos_user (username, password) VALUES ('" + name + "' ,'" + password + "')";
-		result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
-	}
+        sql = "INSERT INTO pos_user (username, password) VALUES ('" + name + "' ,'" + password + "')";
+        result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
+    }
 
-	result = sqlite3_exec(m_db, "COMMIT;", 0, 0, 0);
+    result = sqlite3_exec(m_db, "COMMIT;", 0, 0, 0);
 
-	if (result == SQLITE_OK)
-	{
-		LOG_INFO("save params success");
-		return true;
-	}
+    if(result == SQLITE_OK)
+    {
+        LOG_INFO("save params success");
+        return true;
+    }
 
-	LOG_INFO("save params fail");
-	return false;
+    LOG_INFO("save params fail");
+    return false;
 }