# Execute SQLite queries from console

**URL:** <https://forums.unrealengine.com/t/execute-sqlite-queries-from-console/2102262>\
**Category:** Programming & Scripting\
**Tags:** database, console-commands, unreal-engine\
**Created:** [November 3, 2024, 11:35am UTC](https://forums.unrealengine.com/t/execute-sqlite-queries-from-console/2102262 "2024-11-03T11:35:44Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![MarkEntingh](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/markentingh/32/984569_2.png) [@MarkEntingh](https://forums.unrealengine.com/u/MarkEntingh)\
**Post date:** [November 3, 2024, 11:35am UTC](https://forums.unrealengine.com/t/execute-sqlite-queries-from-console/2102262/1 "2024-11-03T11:35:44Z")

</div>

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**

```auto
#pragma once

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

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

```

**Console\_Sqlite.cpp**

```auto
#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();
        }
    })
);

```

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/6/7/a67ab90bcdca85de2a56ffa2eff86cc2df026869.png)

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