zoukankan      html  css  js  c++  java
  • 如何使用的Ue4自带的SQLiteSupport

    UE4.6版本加入的模块。可以让开发者使用SQLite数据库。SQlite是个轻量型的本地数据库。

    我下面就来介绍一下如何使用这个模块。

    第一步:下载SQLite源代码以及SQLite GUI管理工具SQLite Expert

    进入http://www.sqlite.org/,点击

    DownLoad,选择源代码下载。

     

    SQLite Expert可以去http://www.sqliteexpert.com/下载,个人版是完全免费,这里我推荐用破解的专业版,同时本人不太喜欢新版本。

    第二步:编译对应平台的LIB文件

    进入EngineSourceThirdPartysqlite,并且新建文件夹命名为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);
  • 相关阅读:
    关于PHP高并发抢购系统设计
    阿里云服务器带宽跑满怎么办
    DedeCMS数据负载性能优化方案简单几招让你提速N倍
    linux===启动sdk manager下载配置sdk的时候报错的解决办法
    linux===linux后台运行和关闭、查看后台任务(转)
    自动化测试===热门开源自动化测试框架
    python实战===图片转换为字符的源码(转)
    移动端测试===安卓设备共享程序-发布版本“share device”
    MACACA===gradle下载和安装
    jython
  • 原文地址:https://www.cnblogs.com/blueroses/p/5648025.html
Copyright © 2011-2022 走看看