如何使用的Ue4自带的SQLiteSupport

[复制链接]
查看8278 | 回复16 | 2016-7-6 20:36:50 | 显示全部楼层 |阅读模式
本帖最后由 Someday 于 2016-7-8 12:56 编辑

UE4.6版本加入的模块。可以让开发者使用SQLite数据库。SQlite是个轻量型的本地数据库。
我下面就来介绍一下如何使用这个模块。
第一步:下载SQLite源代码以及SQLite GUI管理工具SQLite Expert
进入http://www.sqlite.org/,点击
DownLoad,选择源代码下载。
SQLite Expert可以去http://www.sqliteexpert.com/下载,个人版是完全免费,这里我推荐用破解的专业版,同时本人不太喜欢新版本。

第二步:编译对应平台的LIB文件
进入\Engine\Source\ThirdParty\sqlite,并且新建文件夹命名为sqlite,将源代码中的文件解压至新建的文件夹。之后返回上级目录,运行VS工程文件。将DebugRelease以及对应的win32x64都编译一遍,虽然只需要对应平台,但是你以后肯定会打包的,所以为了避免以后找不到这个问题,在这里就全编译了。
第三步:编译引擎
这步我也不确定,官方说明文件中有说需要编译,但是我因为已经编译过了,所以也不太确定。
第四步:在工程文件中加入SQLiteSupport模块
[mw_shl_code=cpp,true]PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" , "SQLiteSupport" }); [/mw_shl_code]
最后在需要使用SQLite的地方包含头文件
[mw_shl_code=cpp,true]#include "Runtime/SQLiteSupport/Public/SQLiteDatabaseConnection.h"[/mw_shl_code]
之后就可以使用SQLite了。

下面是一段我测试用的代码,具体怎么用,麻烦各位去看源代码,一共也没几个函数,如果用过SQLite SDK能很快上手。
[mw_shl_code=cpp,true]        FSQLiteDatabase Database;
        Database.Open (TEXT("D:\\UnrealProject\\Sqlite\\Content\\database.db"), nullptr, nullptr);
        //run query where no results are returned
        Database.Execute (*(FString:rintf (TEXT ("insert into test (id,name) values (1,123)"), *(FDateTime::Now ().ToString ()), *(FApp::GetSessionId ().ToString ()))));
        Database.Close();[/mw_shl_code]
这个是AnswerHub的案例代码,我发现第一句话就错了,下面的仅供参考
[mw_shl_code=cpp,true]//create and open the database
     FSQLiteDatabase Database();
     Database.Open("ath\\To\\File", nullptr, nullptr);
     //run query where no results are returned
     Database->Execute(*(FString:rintf(TEXT("Update GameSession set SessionTimeEnd= %s where SessionID = %s"), *(FDateTime::Now().ToString()), *(FApp::GetSessionId().ToString()))));
     
     //run query to fetch results from database
     FSQLiteResultSet* NameResults = NULL;
     if (Database.Execute(*(FString:rintf(TEXT("Select * from Players where SessionID = %s;"), *SessionID)),NameResults))
     {
         for (FSQLiteResultSet::TIterator NameIterator(NameResults); NameIterator; ++NameIterator)
         {
             //do something with the results here
         }
     }[/mw_shl_code]
2016.7.7补充
PS.SQLiteSupport我已经测试过可以打包。大致看过代码是跨平台的,不过没测试过。
上文的路径可以使用相对目录
包含头文件#include "Runtime/Core/Public/Misc/paths.h",使用FPaths::GameDir(),就可以了,然后再使用ConvertRelativePathToFull(const FString& InPath)转化。
具体用法参考https://wiki.unrealengine.com/Packaged_Game_Paths,_Obtain_Directories_Based_on_Executable_Location
以下是paths.h中的部分代码,在使用Sqlite之前最好先判断是否存在db文件(FPaths::FileExists),不然Sqlite就会自己创建一个新文件。
[mw_shl_code=cpp,true]
/** * Returns the base directory of the current game by looking at FApp::GetGameName(). * This is usually a subdirectory of the installation * root directory and can be overridden on the command line to allow self * contained mod support. * * @return base directory */
static FString GameDir();
/** * Returns the content directory of the current game by looking at FApp::GetGameName(). * * @return content directory */
static FString GameContentDir();
/** @return true if this file was found, false otherwise */
static bool FileExists(const FString& InPath);
/** @return true if this directory was found, false otherwise */
static bool DirectoryExists(const FString& InPath);
/** Convert all / and \ to TEXT("/") */
static void NormalizeFilename(FString& InPath);
/** Normalize all / and \ to TEXT("/") and remove any trailing TEXT("/") if the character before that is not a TEXT("/") or a colon */
static void NormalizeDirectoryName(FString& InPath);
/** * Converts a relative path name to a fully qualified name relative to the process BaseDir(). */
static FString ConvertRelativePathToFull(const FString& InPath);[/mw_shl_code]


评分

参与人数 1能量币 +10 收起 理由
Someday + 10 赞一个!

查看全部评分

Eric_Song | 2016-7-6 21:45:59 | 显示全部楼层
好文章
个人博客 : ericsong.org
回复

使用道具 举报

bluerose | 2016-7-7 18:27:03 | 显示全部楼层
怎么没人0.0
回复 支持 反对

使用道具 举报

atgnaw | 2016-7-7 19:58:25 | 显示全部楼层
好文章 顶一个~
回复 支持 反对

使用道具 举报

虚幻 | 2016-7-7 22:13:44 | 显示全部楼层
猫咪前来顶贴
回复 支持 反对

使用道具 举报

小聪子 | 2016-7-8 10:16:03 | 显示全部楼层
好一个干货,顶一下~加油
回复 支持 反对

使用道具 举报

Someday | 2016-7-8 12:57:40 | 显示全部楼层
楼主记得要用工具栏上的代码标识来分隔代码段落,否则很乱且无着色
回复 支持 反对

使用道具 举报

challengerhawk | 2016-7-15 17:47:15 | 显示全部楼层
厉害啊
回复

使用道具 举报

山楂卷 | 2016-9-9 11:56:10 | 显示全部楼层
可以可以
回复

使用道具 举报

rello | 2016-9-14 16:17:31 | 显示全部楼层
支持一下
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

4

主题

23

回帖

182

积分

初阶编码师

积分
182