Execute SQLite queries from console

Anyone who wants to be able to execute SQLite queries from the console, here’s a quick bit of C++ you can add to your project. You’ll need to reference your static database object instead of my DbConnection:Db object and you’ll probably need to change a few other things about this code, but it will work and show you rows of data from SELECT queries, and will also show SQLite errors, too.

Console_Sqlite.h

#pragma once

#include "CoreMinimal.h"
#include "Console_Sqlite.Generated.h"

UCLASS()
class YOURGAME_API UConsoleSqlite: public UObject
{
    GENERATED_BODY()
};

Console_Sqlite.cpp

#include "../Public/Console_Sqlite.h"
#include "../../Db/Connection/Public/DbConnection.h"
#include "HAL/IConsoleManager.h"

static FAutoConsoleCommandWithWorldAndArgs CmdSqlite(
    TEXT("sqlite"),
    TEXT("SQLite query execution"),
    FConsoleCommandWithWorldAndArgsDelegate::CreateLambda([](
        const TArray<FString>& Args, UWorld* World
    )
    {
        if(Args.Num() > 0){
            FString query = FString::Join(Args, TEXT(" "));
            auto statement = DbConnection::Db.PrepareStatement(query.GetCharArray().GetData(), ESQLitePreparedStatementFlags::None);
            if (statement.Execute()) {
                UE_LOG(LogTemp, Warning, TEXT("Executed SQLite Statement: %s"), query.GetCharArray().GetData());
                TArray<FString> cols = statement.GetColumnNames();
                int rownum = 1;
                while (statement.Step() == ESQLitePreparedStatementStepResult::Row) {
                    FString value;
                    TArray<FString> row;
                    for (FString col : cols) {
                        statement.GetColumnValueByName(col.GetCharArray().GetData(), value);
                        row.Add(col + ": " + value);
                    }
                    FString rowstr = FString::Join(row, TEXT("; "));
                    UE_LOG(LogTemp, Warning, TEXT("Row #%i: %s"), rownum, rowstr.GetCharArray().GetData());
                    GEngine->AddOnScreenDebugMessage(-1, 5.0, FColor::Green, FString::Printf(TEXT("Row #%i: %s"), rownum, rowstr.GetCharArray().GetData()));
                    rownum += 1;
                }
            }
            else {
                GEngine->AddOnScreenDebugMessage(-1, 5.0, FColor::Red, FString::Printf(TEXT("%s"), DbConnection::Db.GetLastError().GetCharArray().GetData()));
            }
            statement.Destroy();
        }
    })
);

The image above shows data from one row after executing the console command sqlite SELECT * FROM PlayerInfo