+2 votes

Hey,

I consider to change from unity to godot.
My next project needs a small dedicated server, which settles on a postgreSQL - database (it is my main-database, which I can manage central across all project (dumps, administration,...).
I searched for postgreSQL-Support but the last answer I find was a "no, not yet" from 2018. Is there any update of this answer?
Is it possible to include e.g. Npgsql in a godot-project?

I am sorry for such a "beginner"-question. I read a few comparison between unity and godot and with some experience in my mind (love2d, .NET) I want to switch - but with my favorite database for server :-)

Thanks for reading and I wish you a great sunday.

asked May 24 in Engine by Görf (14 points)

From this i guess you can include c# libraries, so it should work, but i'll let someone who knows for sure answer.

First, I had to install the mono-version for c#-support.
My test-project throw out a build-error, that the mbuild couldn't find the .Net-Framework, but it was installed.
After installing the whole Visual-Studio-Package or editing the csproj I got the same error.
Before giving up for the day I decided to create a brand new godot-project. This one I could build.
So I created the test project with a moving player.
After that I installe VS-code and there I installed the package manager.
With STRG+P and add package I added Npgsql.
VS-Code added the lines:



to my csproj - File.
Now I run dotnet restore in VS-code.
Now I run nuget restore in VS-Code. Sadly, this didn't worked and I can't figure out, why.
I removed the lines in csproj-files.

Now I try to open a test-database and write a line in a table for test-reasons:

public override void _Ready(){
    GD.Print("test");
    _screenSize = GetViewport().Size;
    string cs = "Host=kandula.db.elephantsql.com;Username=hidden_user;Password=hidden_pw;Database=hidden_db";
    con = new NpgsqlConnection(cs);
    try
    {
        con.Open();
        NpgsqlCommand cmd = new NpgsqlCommand("INSERT INTO public.cars (name, price) VALUES (@n, @p)", con);
        cmd.Parameters.AddWithValue("n", "bla");
        cmd.Parameters.AddWithValue("p", 123);
        //cmd.Prepare();
        cmd.ExecuteNonQuery();
    }
    catch
    {
        GD.Print("catch");
        return;
    }}

First, I don't know why it doesn't work with VS-Studio. Also, I don't know how to handle exceptions in godot.
But: Awesome, it works, the values are in the database. I think I give godot and myself a try and look for some basic tutorials.
Thanks for your help!

1 Answer

+1 vote

It works!

I just added the Npgsql Nuget Package to my Project. Here an Code Example:

using System;
using Npgsql;
using Godot;

public class MainScene : Control

{
public override void _Ready()
{
try
{
NpgsqlConnection conn = new NpgsqlConnection("Host=localhost;Username=postgres;Password=postgres;Database=postgres");
conn.Open();
NpgsqlCommand command = new NpgsqlCommand("SELECT * FROM people", conn);
NpgsqlDataReader dataReader = command.ExecuteReader();

        while (dataReader.Read())
        {
            GD.Print(dataReader[0], " ", dataReader[1]);
        }
        conn.Close();
    }
    catch (Exception exception)
    {
        GD.Print(exception);
    }
}

}

answered May 25 by juppi (304 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.