PostgreSQL

Hi everybody! ^^
I’m using PostgreSQL + Npgsql to handle my database. I’m not very experienced with SQL or Mono though, so my progress has come to a halt.
The dll’s I need are in my asset folder and Unity finds no problems so far. But I have a script that I took from the Npgsql manual just to try and see if I can get it working the way I want. The script is applied on an empty GameObject and when I hit Play, nothing happens. No inserts no nothing.
Thought that I would come here to see what I’m doing wrong, if anyone knows. I guess most of you guys are using MySQL though.

Here’s the script:

using UnityEngine;
using System.Collections;
using System;
using System.Data;
using Npgsql;

public class NpgsqlUserManual
{
 public static void Main(String[] args)
  {
    NpgsqlConnection conn = new NpgsqlConnection("Server=XXXXXXXXX;Port=XXXX;User Id=XXXX;Password=XXXX;Database=test;");
    conn.Open();
    
    NpgsqlCommand command = new NpgsqlCommand("insert into testtable values(1, 1)", conn);
    Int32 rowsaffected;
    
    try
    {
      rowsaffected = command.ExecuteNonQuery();
     
      Console.WriteLine("It was added {0} lines in table PlayerDB", rowsaffected);
    }
    
    finally
    {
      conn.Close();
    }
  }
}

I know some lines are junk and not needed but I don’t want to remove too much until I get it working.

Not 100% sure if postgre is any different but normally SQL commands are all upper case, so it would be "INSERT INTO VALUES(…) for the command

The postgresql log should give you an idea on whats going wrong or if the connection works at all (if it is not on the same box the answer is likely that the connection doesn’t work at all unless you configured your user that way cause remote login is by default disabled in all postgre sql installations)

Hmm thanks for the input :slight_smile:
But the script, as I pasted it, works in PostgreSQL so hmm… I’ll check that out though. Thank you!

if the command works like that then its likely a remote user thing that you aren’t allowed to login with that account from your ip. (normally only users from 127.0.0.1 are allowed to login to users)

Hi. Thanks again for taking the time. :slight_smile:

The server is at 127.0.0.1. The game and sql server are on the same computer.
It seems to me that the script just isn’t executed. I have no idea how to make it run though. :\

The code, as you wrote it, is going to do bupkis. public static void Main(String[] args) doesn’t mean anything to a Monobehaviour. I’m not sure how you even “applied” that code to an empty GameObject, as only MonoBehaviours can be added as components to GO’s. Try this:

using UnityEngine;
using System.Collections;
using System;
using System.Data;
using Npgsql;

public class NpgsqlUserManual : MonoBehaviour
{

 public void Start ()
  {
    NpgsqlConnection conn = new NpgsqlConnection("Server=XXXXXXXXX;Port=XXXX;User Id=XXXX;Password=XXXX;Database=test;");
    conn.Open();
   
    NpgsqlCommand command = new NpgsqlCommand("insert into testtable values(1, 1)", conn);
    Int32 rowsaffected;
   
    try
    {
      rowsaffected = command.ExecuteNonQuery();
     
      Console.WriteLine("It was added {0} lines in table PlayerDB", rowsaffected);
    }
   
    finally
    {
      conn.Close();
    }
  }
}

Can’t guarantee it works, but that at least lets you use it as you would any regular Unity component. I’ve dabbled briefly with Npgsql in Unity and it is possible to establish a connection and execute queries (without yielding, even), but I don’t have that code handy.

@burnumd

Thanks a lot. That partly did the trick. :slight_smile:
The script could be applied to a gameobject when the class name was different than the file name of the cs. Adding the monobehaviour line and renaming the script made it work perfectly. Thanks to both of you for taking the time to help me out.

Hi, I am following your code, but… encountered this problem:

Internal compiler error. See the console log for more information. output was:
Unhandled Exception: System.TypeLoadException: Could not load type ‘Npgsql.NpgsqlConnection’ from assembly ‘Npgsql, Version=2.0.0.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7’.

Can anyone help me?

P.S. I’ve also tried several other versions of Npgsql, but none seems to be working.

I would like some help with this too as I am also having the same problem :slight_smile: