如何使用的Ue4自帶的SQLiteSupport


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模塊

 PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" , "SQLiteSupport" }); 

最后在需要使用SQLite的地方包含頭文件

#include "Runtime/SQLiteSupport/Public/SQLiteDatabaseConnection.h"

之后就可以使用SQLite了。

 

下面是一段我測試用的代碼,具體怎么用,麻煩各位去看源代碼,一共也沒幾個函數,如果用過SQLite SDK能很快上手。

  FSQLiteDatabase Database;
    Database.Open (TEXT("D:\\UnrealProject\\Sqlite\\Content\\database.db"), nullptr, nullptr);
    //run query where no results are returned
    Database.Execute (*(FString::Printf (TEXT ("insert into test (id,name) values (1,123)"), *(FDateTime::Now ().ToString ()), *(FApp::GetSessionId ().ToString ()))));
    Database.Close();

 

 

這個是AnswerHub的案例代碼,我發現第一句話就錯了,下面的僅供參考

//create and open the database

     FSQLiteDatabase Database();

     Database.Open("Path\\To\\File", nullptr, nullptr);

     //run query where no results are returned

     Database->Execute(*(FString::Printf(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::Printf(TEXT("Select * from Players where SessionID = %s;"), *SessionID)),NameResults))

     {

         for (FSQLiteResultSet::TIterator NameIterator(NameResults); NameIterator; ++NameIterator)

         {

             //do something with the results here

         }

     }

 

2016.7.7補充

上文的路徑可以使用相對目錄

包含頭文件#include "Runtime/Core/Public/Misc/paths.h",使用FPaths::GameDir(),就可以了。

具體用法參考https://wiki.unrealengine.com/Packaged_Game_Paths,_Obtain_Directories_Based_on_Executable_Location

以下是paths.h中的部分代碼,在使用Sqlite之前最好先判斷是否存在db文件(FPaths::FileExists),不然Sqlite就會自己創建一個新文件。

/**
 * 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);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM