UE4连接MySQL数据库总结
1.创建C++项⽬Test_SQL
2.建⽴空插件TestMySQL
3.插件⽬录下新建C++的Object类MyConnectionObject
4.新建蓝图函数库的C++类SqlBlueprintFunctionLibrary
5.引⼊第三⽅库,先到Plugins/TestMySQL/Source⽂件夹,创建ThirdParty⽂件夹
6.添加⾃定义模块"ConnectorLibs",//添加⾃定义模块
7.选择VC++⽬录,配置项⽬所需的第三⽅库包含⽬录和库⽬录
8.指定插件运⾏的平台为Win64/32。在VS⼯程中到TestMySQL.uplugin⽂件,设置"WhitelistPlatforms": [ "Win64"]
{
"FileVersion": 3,
"Version": 1,
"VersionName": "1.0.0",
"FriendlyName": "TestMySQL",
"Description": "SQL",
"Category": "Other",
"CreatedBy": "likai",
"CreatedByURL": "6@qq",
"DocsURL": "1",
"MarketplaceURL": "",
"SupportURL": "1",
"CanContainContent": false,
"IsBetaVersion": false,
"IsExperimentalVersion": false,
"Installed": false,
"Modules": [
{
"Name": "TestMySQL",
"Type": "Runtime",
"LoadingPhase": "Default",
"WhitelistPlatforms": [
"Win64"
]
}
]
}
9.编写代码
MyConnectionObject.h⽂件
#pragma once
#include "CoreMinimal.h"
#include "UObject/NoExportTypes.h"
//引⼊mysql头⽂件
#include "mysql.h"
#include "ated.h"
/
**
*数据库连接对象类
*/
UCLASS(BlueprintType)//声明为蓝图类型的类
class TESTMYSQL_API UMyConnectionObject : public UObject
{
GENERATED_BODY()
private:
//声明私有构造函数
UMyConnectionObject();
public:
/
/声明MySQL连接对象
MYSQL* Conn;
};
MyConnectionObject.cpp⽂件
#include "MyConnectionObject.h"
UMyConnectionObject::UMyConnectionObject()
{
//初始化连接对象
Conn = nullptr;
}
SqlBlueprintFunctionLibrary.h⽂件
/
/ Copyright 2020 NanGongTianYi. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
//引⼊mysql头⽂件
#include "mysql.h"
//引⼊数据库连接对象头⽂件
#include "MyConnectionObject.h"
#include "ated.h"
/** ⼀⾏所含数据 */
USTRUCT(BlueprintType)
struct FQueryResultRow
{
GENERATED_BODY()
/** ⼀⾏的数据 */
UPROPERTY(BlueprintReadWrite, Category = "Reult Row Value") TArray<FString> RowValue;
};
/** 所有⾏所数据 */
USTRUCT(BlueprintType)
mysql下载的vs库放在那个文件里struct FQueryResultRows
{
{
GENERATED_BODY()
/** 所有⾏数据 */
UPROPERTY(BlueprintReadWrite, Category = "Reult Rows Value")
TArray<FQueryResultRow> RowsValue;
};
/**
* 数据库连接类
*/
UCLASS(BlueprintType)//声明为蓝图类型
class TESTMYSQL_API USqlBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:
/**
* 连接MySQL数据库
* @param Host 数据库IP地址
* @param UserName 数据库⽤户名
* @param Password 数据库密码
* @param DbName 数据库名
* @param Port 端⼝号
* @param Msg 提⽰消息
* @return UMyConnectionObject* 数据库连接对象
*/
UFUNCTION(BlueprintCallable, Category = "SQL Utilities")
static UMyConnectionObject* ConnectToMySQL(FString Host, FString UserName, FString Password, FString DbName, int32 Port, FString& Msg);
/**
* 获取数据库连接状态
* @param ConnObj 数据库连接对象
* @return bool 数据库是否连接,true为已连接
*/
UFUNCTION(BlueprintCallable, Category = "SQL Utilities")
static bool GetConnectionState(UMyConnectionObject* ConnObj);
/**
* 关闭数据库连接
* @param ConnObj 数据库连接对象
* @return bool 是否关闭成功,true为关闭成功
*/
UFUNCTION(BlueprintCallable, Category = "SQL Utilities")
static bool CloseConnection(UMyConnectionObject* ConnObj);
/**
* 向数据库中添加记录
* @param ConnObj 数据库连接对象
* @param SqlQuery 数据库注⼊语句
* @return bool 注⼊状态
*/
UFUNCTION(BlueprintCallable, Category = "SQL Utilities")
static bool InsertData(UMyConnectionObject* ConnObj, FString SqlQuery);
/**
* 修改数据库中的记录
* @param ConnObj 数据库连接对象
* @param SqlQuery 数据库注⼊语句
* @return bool 修改状态
*/
UFUNCTION(BlueprintCallable, Category = "SQL Utilities")
static bool UpdateData(UMyConnectionObject* ConnObj, FString SqlQuery);
/
**
* 删除数据库中的记录
* @param ConnObj 数据库连接对象
* @param ConnObj 数据库连接对象
* @param SqlQuery 数据库注⼊语句
* @return bool 删除状态
*/
UFUNCTION(BlueprintCallable, Category = "SQL Utilities")
static bool DeleteData(UMyConnectionObject* ConnObj, FString SqlQuery);
/**
* 删除数据库中符合条件的记录
* @param ConnObj 数据库连接对象
* @param TableName 表名
* @param Condition 条件
* @return bool 删除状态
*/
UFUNCTION(BlueprintCallable, Category = "SQL Utilities")
static bool DropData(UMyConnectionObject* ConnObj, FString TableName, FString Condition);
/**
* 从数据库中查询数据
* @param ConnObj 数据库连接对象
* @param TableName 要查询的表名
* @param Condition 条件映射
* @param bIsAnd 条件之间的关系,默认为or
* @return bool 查询状态
*/
UFUNCTION(BlueprintCallable, Category = "SQL Utilities")
static bool SelectData(UMyConnectionObject* ConnObj, FString TableName, TMap<FString, FString> Condition, bool bIsAnd, FQueryResultRows& Results); };
SqlBlueprintFunctionLibrary.cpp⽂件
// Copyright 2020 NanGongTianYi. All Rights Reserved.
#include "SqlBlueprintFunctionLibrary.h"
#include "Engine.h"
#include <string>
UMyConnectionObject* USqlBlueprintFunctionLibrary::ConnectToMySQL(FString Host, FString UserName, FString Password, FString DbName, int32 Port, FStrin {
//字符编码格式转换
std::string tHost(TCHAR_TO_UTF8(*Host));
std::string tUserName(TCHAR_TO_UTF8(*UserName));
std::string tPassword(TCHAR_TO_UTF8(*Password));
std::string tDbName(TCHAR_TO_UTF8(*DbName));
//数据库连接对象创建
UMyConnectionObject* ConnObj = NewObject<UMyConnectionObject>();
//初始化MYSQL连接对象
ConnObj->Conn = mysql_init(nullptr);
//判断连接状态,并返回相应信息
if (!mysql_real_connect(ConnObj->Conn, tHost.c_str(), tUserName.c_str(), tPassword.c_str(), tDbName.c_str(), (uint32)Port, nullptr, 0))
{
Msg = TEXT("连接失败!");
}
else
{
Msg = TEXT("连接成功!");
}
//返回数据库连接对象
return ConnObj;
}
bool USqlBlueprintFunctionLibrary::GetConnectionState(UMyConnectionObject* ConnObj)
bool USqlBlueprintFunctionLibrary::GetConnectionState(UMyConnectionObject* ConnObj)
{
if (!ConnObj)
{
GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Red, TEXT("对象为空!"));
return false;
}
else
{
//判断MYSQL连接对象是否为空
if (ConnObj->Conn != nullptr)
{
return true;
}
}
return false;
}
bool USqlBlueprintFunctionLibrary::CloseConnection(UMyConnectionObject* ConnObj)
{
if (GetConnectionState(ConnObj))
{
mysql_close(ConnObj->Conn);
//指针归位,否则会变成悬挂指针
ConnObj->Conn = nullptr;
ConnObj = nullptr;
return true;
}
return false;
}
bool USqlBlueprintFunctionLibrary::InsertData(UMyConnectionObject* ConnObj, FString SqlQuery) {
std::string tSqlQuery(TCHAR_TO_UTF8(*SqlQuery));
//判断连接对象是否为空
if (!ConnObj)
{
GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Red, TEXT("对象为空!"));
return false;
}
//判断语句是否执⾏成功
if (mysql_query(ConnObj->Conn, tSqlQuery.c_str()))
{
GEngine->AddOnScreenDebugMessage(-1, 8.0f, FColor::Blue, TEXT("添加失败!"));
return false;
}
return true;
}
bool USqlBlueprintFunctionLibrary::UpdateData(UMyConnectionObject* ConnObj, FString SqlQuery) {
std::string tSqlQuery(TCHAR_TO_UTF8(*SqlQuery));
//判断连接对象是否为空
if (!ConnObj)
{
GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Red, TEXT("对象为空!"));
return false;
}
//判断语句是否执⾏成功
if (mysql_query(ConnObj->Conn, tSqlQuery.c_str()))
{
GEngine->AddOnScreenDebugMessage(-1, 8.0f, FColor::Blue, TEXT("修改失败!"));
return false;
}
return true;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论