UE连接MYSQL数据库

1、安装 Mysql Connect 插件
Install the Mysql Connect plugin

https://www.unrealengine.com/marketplace/zh-CN/product/c15f4e325ba04d5698395414af94644f

2、使用插件
Use plugins

在这里插入图片描述
在这里插入图片描述

Features:(产品特点)

Support local MYSQL server and cloud database connection (eg: Azure Database for MySQL)
支持本地MYSQL服务器和云数据库连接(例如:Azure Database for MySQL)

Support C++ or pure blueprint calls
支持 C++ 或纯蓝图调用

Synchronous and asynchronous support
同步和异步支持

Support MYSQL 5.x.x - 8.x.x series
支持MYSQL 5.x.x - 8.x.x 系列

used in C++(在 C++ 中使用)

/// synchronous connection example(同步连接示例) ///

Mysql = UMysqlConnectSubsystem::Get();
Results = NewObject<UMysqlResult>();

Mysql->Connect("127.0.0.1", 3306, "username", "pw", "TableName", Results);
if (!Results->IsSucceed)
{
	UE_LOG(LogTemp, Warning, TEXT("MYSQL: ConnectError: %s"), *Results->Msg);
	return;
}

//Get the total number of rows
Mysql->Query("SELECT count(1) FROM `testtable`", Results);
UE_LOG(LogTemp, Warning, TEXT("MYSQL: Count: IsSucceed = %d, msg = %s, count = %d"), Results->IsSucceed, *Results->Msg, Results->getCount());

//get row
Mysql->Query("SELECT * FROM `testtable`", Results);
UE_LOG(LogTemp, Warning, TEXT("MYSQL: rows: IsSucceed = %d, msg = %s, Num = %d"), Results->IsSucceed, *Results->Msg, Results->getRows().Num());

/// Asynchronous connection example(异步连接示例) ///

Mysql = UMysqlConnectSubsystem::Get();
Results = NewObject<UMysqlResult>();

const FLatentActionInfo LatentInfo(0, FMath::Rand(), TEXT("ConnectCallback"), this);
Mysql->AConnect("127.0.0.1", 3306, "username", "pw", "TableName", Results, this, LatentInfo);

//Asynchronous callback function
提示:在.h文件中申明回调函数时需要加上UFUNCTION()标志
Tips:The UFUNCTION() flag should be added when the callback function is declared
void APluginProjectGameModeBase::ConnectCallback()  
{
    if (!Results->IsSucceed)
    {
        UE_LOG(LogTemp, Warning, TEXT("MYSQL: ConnectError: %s"), *Results->Msg);
        return;
    }

    //Get the total number of rows
    const FLatentActionInfo LatentInfo(0, FMath::Rand(), TEXT("fetch_count_Callback"), this);
    Mysql->AQuery("SELECT count(1) FROM `testtable`", Results, this, LatentInfo);
}

//Asynchronous callback function
void APluginProjectGameModeBase::fetch_count_Callback()
{
    UE_LOG(LogTemp, Warning, TEXT("MYSQL: Count: IsSucceed = %d, msg = %s, count = %d"), Results->IsSucceed, *Results->Msg, Results->getCount());

    //get row
    const FLatentActionInfo LatentInfo(0, FMath::Rand(), TEXT("fetch_rows_Callback"), this);
    Mysql->AQuery("SELECT * FROM `testtable`", Results, this, LatentInfo);
}

//Asynchronous callback function
void APluginProjectGameModeBase::fetch_rows_Callback()
{
    UE_LOG(LogTemp, Warning, TEXT("MYSQL: rows: IsSucceed = %d, msg = %s, Num = %d"), Results->IsSucceed, *Results->Msg, Results->getRows().Num());
}
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值