UE4中使用Sqlite数据库操作步骤

首先下载sqlite需要的文件,去Sqlite官方下载源码https://www.sqlite.org/download.html,总共下载两个文件,一个是Source Code下面的sqlite-amalgamation-3300100,以及Precompiled Binaries for Windows下面的sqlite-dll-win64-x64-3300100。

UE4中使用Sqlite数据库操作步骤_第1张图片

解压sqlite-dll-win64-x64-3300100文件,运行VS2017中Visual Studio命令提示程序,进入到解压后的文件夹中,运行命令:lib /def :sqlite3.def  /machine:X64 (编译lib文件X需要大写)。即可生成lib文件。

UE4中使用Sqlite数据库操作步骤_第2张图片       UE4中使用Sqlite数据库操作步骤_第3张图片

创建两个文件夹Includes和Libs,把sqlite-amalgamation-3300100解压的所有.c和.h代码放到Includes文件夹中,把生成的sqlite3.lib文件放到Libs的文件下。将Includes和Libs文件夹放到一个新建的sqlite文件夹下。

UE4中使用Sqlite数据库操作步骤_第4张图片

然后将sqlite库放到项目文件夹下,则可以将sqlite文件夹放到ProjectName.build.cs同目录下,然后在ProjectName.build.cs添加数据库路径,然后编译引擎即可。

UE4中使用Sqlite数据库操作步骤_第5张图片

示例:注意C++类继承自UBlueprintFunctionLibrary,定义的函数声明为static,这样你就可以在蓝图的任意一个类中调用这些函数(全局函数)。

#pragma once

#include "Engine.h"
#include 
#include "Kismet/BlueprintFunctionLibrary.h"
#include "SqliteDataBase.generated.h"

/**
  * Sqlite数据库查询相关操作,在其他类中声明本类之后即可调用本类函数
 *本例中数据库(name,password,type)
 */
UCLASS()
class CLASSNAME_API USqliteDataBase : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()
public:
	USqliteDataBase();
	~USqliteDataBase();

	//得到数据库存放路径
	UFUNCTION(BlueprintCallable, Category = "SqliteDatabase")
		static FString GetDataBasePath();

	//打开数据库
	UFUNCTION(BlueprintCallable, Category = "SqliteDatabase")
		static bool OpenDataBase(FString strDataBasePath);

	//对数据库进行元素插入
	UFUNCTION(BlueprintCallable, Category = "SqliteDatabase")
		static bool InsertItem(FString strUserName, FString strPassword, FString strType);

	//删除元素
	UFUNCTION(BlueprintCallable, Category = "SqliteDatabase")
		static bool DeleteItem(FString strUserName);

	//按照名称查询
	UFUNCTION(BlueprintCallable, Category = "SqliteDatabase")
		static bool QueryItem(FString strUserName);

	//查找数据的类别
	UFUNCTION(BlueprintCallable, Category = "SqliteDatabase")
		static int32 QueryType(FString strUserName, FString strPassword, FString& strType);
};
#include "SqliteDataBase.h"
#include "sqlite3.h"

using namespace std;
sqlite3 *pDataBase = nullptr;

//查询结果存入该数组
TArray Results;
//存放查询临时字符
FString strResults;

USqliteDataBase::USqliteDataBase() {
	//
}

USqliteDataBase::~USqliteDataBase() {
	//
}

FString USqliteDataBase::GetDataBasePath() {
	FString strGameContentPath = *FPaths::ProjectContentDir();
	strGameContentPath += "DataBase";
	FString strPath = FPaths::Combine(*strGameContentPath, L"DataBaseName.db");
	return strPath;
}

bool USqliteDataBase::OpenDataBase(FString strDataBasePath) {
	string strDBPath(TCHAR_TO_UTF8(*strDataBasePath));
	int nRes = sqlite3_open(strDBPath.c_str(), &pDataBase);
	if (nRes != SQLITE_OK)
	{
		UE_LOG(LogTemp, Warning, TEXT("OPEN DATABASE FAILED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"));
		return false;
	}
	else
	{
		UE_LOG(LogTemp, Warning, TEXT("OPEN DATABASE SUCC!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"));
		return true;
	}
}

bool USqliteDataBase::InsertItem(FString strUserName, FString strPassword, FString strType) {
	FString strSql = "";
	strSql += "insert into DataTableName(username, password,type)";
	strSql += "values('";
	strSql += strUserName;
	strSql += "','";
	strSql += strPassword;
	strSql += "','";
	strSql += strType;
	strSql += "');";
	char* cErrMessage;
	string strSqlRes(TCHAR_TO_UTF8(*strSql));
	int nRes = sqlite3_exec(pDataBase, strSqlRes.c_str(), 0, 0, &cErrMessage);
	if (nRes != SQLITE_OK)
	{
		string str(cErrMessage);
		UE_LOG(LogTemp, Warning, TEXT("INSERT FAILED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"));
		return false;
	}
	else
	{
		UE_LOG(LogTemp, Warning, TEXT("INSERT SUCC!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"));
		return true;
	}
}

bool USqliteDataBase::DeleteItem(FString strUserName) {
	FString strSql = "";
	strSql += "delete from DataTableName where username ='";
	strSql += strUserName;
	strSql += "';";
	char* cErrMessage;
	string strSqlRes(TCHAR_TO_UTF8(*strSql));
	int nRes = sqlite3_exec(pDataBase, strSqlRes.c_str(), 0, 0, &cErrMessage);
	if (nRes != SQLITE_OK)
	{
		UE_LOG(LogTemp, Warning, TEXT("DELETE FAILED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"));
		return false;
	}
	else
	{
		UE_LOG(LogTemp, Warning, TEXT("DELETE SUCC!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"));
		return true;
	}
}

/**
*查询回调函数,每次查到一条结果信息,即调用一次本函数
*NotUserd:该参数不清楚
*column_n:指的是查询结果有多少列
*column_Value:指的是某一列的值
*column_Name:指的是某一列的名称。例如username,password,type等
*/
static int QueryResult(void *NotUserd, int column_n, char** column_Value, char** column_Name) {
	for (int i = 0; i < column_n; i++) {
		strResults = "";
		strResults += column_Name[i];
		strResults += ":";
		strResults += column_Value[i];
		strResults += "\n";
		Results.Add(strResults);
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, strResults);
	}
	return 0;
}

bool USqliteDataBase::QueryItem(FString strUserName) {
	FString strSql = "";
	strSql += "select * from DataTableName where username ='";
	strSql += strUserName;
	strSql += "';";
	char* cErrMessage;
	string strSqlRes(TCHAR_TO_UTF8(*strSql));
	int Res = sqlite3_exec(pDataBase, strSqlRes.c_str(), QueryResult, 0, &cErrMessage);
	if (Res != SQLITE_OK) {
		return false;
	}
	return true;
}

int32 USqliteDataBase::QueryType(FString strUserName, FString strPassword, FString& strType) {
	sqlite3_stmt * stmt = NULL;
	const char *zTail;
	FString strSql = "select role from DataTableName where username = '";
	strSql += strUserName;
	strSql += "' and password = '";
	strSql += strPassword;
	strSql += "'";
	string strQuerySql(TCHAR_TO_UTF8(*strSql));
	if (sqlite3_prepare_v2(pDataBase, strQuerySql.c_str(), -1, &stmt, &zTail) == SQLITE_OK)
	{
		int iCount = sqlite3_column_count(stmt);
		while (sqlite3_step(stmt) == SQLITE_ROW)
		{
			int Type = sqlite3_column_int(stmt, 0);
			strType = FString::FromInt(Type);
		}
	}
	sqlite3_finalize(stmt);
	return 0;
}

在蓝图中调用:

UE4中使用Sqlite数据库操作步骤_第6张图片

你可能感兴趣的:(UE4)