Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Test Connect Script to PostgreSQL Server

Discussion in 'Scripting' started by will_w, Feb 21, 2008.

  1. will_w

    will_w

    Joined:
    Feb 20, 2008
    Posts:
    56
    I'm struggling to find how to connect to an existing postgreSQL database from within Unity, let alone send a query. I'm sure there is some basic concept I'm missing. I am basically familiar with some SQL, javascript, and a some of the C variants. I'm trying to understand the WWW command, but I'm not clicking.

    Could someone please post a small script in any of the above languages that connects to a postgreSQL db and performs a SELECT * FROM testdb using the following:

    Server Name: testserver or ip 192.168.2.65 (whichever is easier)
    Database: testdb
    Username: testuser
    Password: testpass

    Thanks.
     
  2. jfogh

    jfogh

    Joined:
    Feb 20, 2008
    Posts:
    3
    The WWW class is for interacting with web pages, or at least
    a HTTP based server. The easiest way to perform the query is
    probably to set up some kind of web-based front-end to the
    PostgreSQL-server.
    Something like Ruby on Rails (rubyonrails.com) might be useful
    for this.
    Otherwise you could take a look at Mono (mono-project.com).
    Since Unity is based on Mono it might be possible to use a
    PostgreSQL wrapper from there.
    But performing queries on a PostgreSQL server from Unity is
    probably going to take some work.
     
  3. jashan

    jashan

    Joined:
    Mar 9, 2007
    Posts:
    3,306
    Has anybody ever tried using classes of the System.Data namespace(s) in a Unity game? That might be the most convenient and quick way to do it. Obviously, that'll only work for game servers or game clients that are in a local network with the database server - otherwise you'll run into all kinds of unpleasant issues and in WANs I would guess that having some sort of "Web service" (in the general sense "a service provided over the Web") would be easier and more practical to set up.

    I'm not sure if the System.Data namespaces are the same in Mono as they are in Microsoft's .NET, but if they are, that would probably be the most direct and convenient way for accessing a database from Unity (I think it's just some 3 or 5 or so lines of code to get a SELECT executed). If there's a .NET-driver for PostgreSQL, it'll probably be based on the System.Data namespace, so that would probably be the "natural way" to do it.

    Jashan
     
  4. hai_ok

    hai_ok

    Joined:
    Jun 20, 2007
    Posts:
    193
    I've gotten unity to talk to PHP that then can do just about anything via SQL with a database.

    It obviously requires SQL/PHP/Linux. It works great for me. Still have to add some functionality and straighten things out a bit though.
     
  5. will_w

    will_w

    Joined:
    Feb 20, 2008
    Posts:
    56
    Thanks for the pushes in the right direction. Mono does indeed have a set of libraries to support postgreSQL.

    Npgsql is a .Net Data Provider for Postgresql Database Server.

    Npgsql/bin/ms1.1 - Npgsql compiled for MS.Net 1.1

    Npgsql/bin/mono - Npgsql compiled for Mono


    I believe the following workflow will solve the problem:

    postgreSQL > Npgsql > C# Mono > Unity

    Anyone (read Unity devs from their Asset Server) want to share a simple C# script to accomplish the above? Or maybe a little plugin :)
     
  6. rom

    rom

    Joined:
    Jul 2, 2006
    Posts:
    265
    Here is a c# script that uses Npgsql
    Perhaps it will point you in the right direction
     

    Attached Files:

  7. will_w

    will_w

    Joined:
    Feb 20, 2008
    Posts:
    56
    Thanks for the script. I thought it had helped, but I'm still struggling with I think the most basic of things. I'm not sure I've got Npgsql installed correctly.

    I get this error:

    Assets/DBTestConnect.cs(5) error: The type or namespace name `Npgsql' could not be found. Are you missing a using directive or an assembly reference?

    Can someone confirm where the Npgsql needs to be. I get the same error regarding "using System.Data" as well.

    Finally, here is the C# demo script that comes w/the Npgsql documentation. Could someone translate this to a Unity script? I would post my version but as I have been completely unsuccessful in connecting, I don't want to clutter the thread with bad code.

    Code (csharp):
    1. using System;
    2. using System.Data;
    3. using Npgsql;
    4.  
    5. public class NpgsqlUserManual
    6. {
    7.   public static void Main(String[] args)
    8.   {
    9.     NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;Port=5432;User Id=joe;Password=secret;Database=joedata;");
    10.     conn.Open();
    11.     conn.Close();
    12.   }
    13. }
    14.  
    BTW I have read, created and edited records from the DB I'm trying to connect to with RealBasic and Visual Studio on a Mac and PC so I can confirm that the db is in fact listening and operating correctly.

    Thanks,
    Will
     
  8. rprrs

    rprrs

    Joined:
    Jan 16, 2015
    Posts:
    3
    for any with the same problem that will_w remember also copy from C:\Program Files\Unity\Editor\Data\Mono\lib\mono\2.0 the "System.Data.dll" into assets
     
    twobob and kalavinka13 like this.