UE4連接Mysql數據庫解決方案

第一步:請閱讀http://user.qzone.qq.com/378100977/blog/1430883016這位仁兄的鏈接,經過我的實際測試。這篇文章的代碼沒有問題。
第二步:這篇文章中未解決的問題:
程序可以編譯,但是鏈接找不到libmysql。解決的辦法就是將libmysql.dll放到Binaries中。
文章中使用了絕對路徑,比較LOW,估計作者也是稀裏糊塗,沒有深究。我們要相對路徑!!! 做法:將文章中的PrivateIncludePaths改成PublicIncludePaths.爲什麼這麼改就可以了。可以看一下這兩個函數的註釋!有些朋友使用比較新的UE版本,可能會遇到RulesCompiler.GetModuleFileName(String)已過時的問題,解決辦法如下:
RulesAssembly RA;
FileReference CheckProjectFile;
UProjectInfo.TryGetProjectForTarget("工程名", out CheckProjectFile);
RA = RulesCompiler.CreateProjectRulesAssembly(CheckProjectFile);
FileReference FR = RA.GetModuleFileName(this.GetType().Name);
string ModulePath = Path.GetDirectoryName(FR.CanonicalName);
第三步:到這一步,很多新手花了很多時間,欣喜若狂。被UE折騰的扎耳撓思。終於編譯,調用沒有問題了。謝天謝天。那麼問題來了,這時候發現 讀取MYSQL中的中文數據後顯示的都是亂碼!!(如果你MySQL還沒連上,請不用往下看了。。)
亂碼問題:
中文亂碼無非就是編碼格式問題。MySQL現在默認都是UTF8編碼。不存在語言障礙。那爲什麼還是亂碼呢?當然,還是編碼問題!
解決亂碼問題
在mysql_real_connect後,程序中設置編碼爲mysql_set_character_set(mysql, "utf8");【可能有人問,MySQL不是默認UTF8嗎?爲什麼還要設置?sorry,就得設置,否則無法正確顯示。修改MySQL配置文件也不行】。
將讀出的數據使用UTF8_TO_TCHAR()函數轉碼。到此,中文亂碼問題解決。有了TCHAR,就擁有了FString.什麼都不怕了!
第四步:如何訪問MySQL數據庫,使用語句還是存儲過程?我的建議是,都搞搞。那個爽,用那個。存儲過程是MSSQL的TSQL集合,訪問數據庫效 率高,速度快!以前MySQL都沒有,終於良心發現,加上了。在目前的高版本中基本都支持。那麼問題來了,怎麼調用呢?請百度,大把的文章!
using System.IO;

namespace UnrealBuildTool.Rules
{
    public class MySQLSupport : ModuleRules
    {
        public MySQLSupport(ReadOnlyTargetRules Target) : base(Target)
        {
            PrivateDependencyModuleNames.AddRange(
                new string[] {
                "Core",
                "CoreUObject",
                "Engine",
                }
            );
            RulesAssembly RA;
            FileReference CheckProjectFile;
            UProjectInfo.TryGetProjectForTarget("VRHome", out CheckProjectFile);
            RA = RulesCompiler.CreateProjectRulesAssembly(CheckProjectFile);
            FileReference FR = RA.GetModuleFileName(this.GetType().Name);
            string ModulePath = Path.GetDirectoryName(FR.CanonicalName);

            //string ModulePath = Path.GetDirectoryName(RulesCompiler.GetModuleFilename(this.GetType().Name));         // gets the directory path of this module        
            string ThirdPartyPath = Path.GetFullPath(Path.Combine(ModulePath, "../../ThirdParty/"));                 // gets the ThirdParty folder directory path        
            string MySQLConnectorPath = ThirdPartyPath + "MySQL Connector.C6.1/";                                    // gets the MySQL Connector.C 6.1 folder path        
            string MySQLConnectorLibraryPath = MySQLConnectorPath + "lib/";                                          // gets the path of the lib folder        
            string MySQLConnectorIncludePath = MySQLConnectorPath + "include/";                                      // gets the path of the include folder        
            string MySQLConnectorImportLibraryName = Path.Combine(MySQLConnectorLibraryPath, "libmysql.lib");        // gets the file path and name of the libmysql.lib static import library        
            string MySQLConnectorDLLName = Path.Combine(MySQLConnectorLibraryPath, "libmysql.dll");                  // gets the file path and name of libmysql.dll        
            if (!File.Exists(MySQLConnectorImportLibraryName))                                                       // check to ensure the static import lib can be located, or else we'll be in trouble        
            {
                throw new BuildException(string.Format("{0} could not be found.", MySQLConnectorImportLibraryName));        // log an error message explaining what went wrong if not found        
            }
            if (!File.Exists(MySQLConnectorDLLName))                                                                 // check to make sure the dll can be located or else we'll be in trouble        
            {
                throw new BuildException(string.Format("{0} could not be found.", MySQLConnectorDLLName));           // log an error message explaining what went wrong if not found        
            }

            PublicIncludePaths.Add(MySQLConnectorIncludePath);                                                       // add the "include" folder to our dependencies. I've chosen PrivateIncludePaths since I hide the mysql headers from external code        
            PublicLibraryPaths.Add(MySQLConnectorLibraryPath);                                                       // add the "lib" folder to our dependencies        
            PublicAdditionalLibraries.Add(MySQLConnectorImportLibraryName);                                          // add libmysql.lib static import library as a dependency        
            PublicDelayLoadDLLs.Add(MySQLConnectorDLLName);                                                          // add libmysql.dll as a dll    
            RuntimeDependencies.Add(new RuntimeDependency("$(ProjectDir)/Binaries/Win64/libmysql.dll"));             // 自動添加libmysql.dll到指定的打包目錄中
        }
    }
}


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