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到指定的打包目录中
        }
    }
}


  • 1
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值