Как подключить Postgresql, к проекту на Unity?
Я за пару дней просмотрел кучу форумов и материалов по этой теме, но так и не нашёл как это сделать. Не получается собрать проект с подключённым Postgresql . Читал тут. Релиз npgsql , есть ещё тут. Как эти пользоваться я не совсем понимаю. Может кто подключал Postgresql к Unity? Объясните по шагам как это сделать.
Ответы (1 шт):
Не где нет хорошего мануала для того чтоб подружить Unity и Postgresql решил написать тут (у меня Windows 10 , Postgresql 9.4.26, Unity_2019.3.3f1) :
1 Чтоб не вылетали ошибки типа:
Assembly 'Library/ScriptAssemblies/Assembly-CSharp.dll' will not be loaded due to errors: Reference has errors 'Npgsql'
PrecompiledAssemblyException: Multiple precompiled assemblies with the same name Npgsql.resources.dll included for the current platform. Only one assembly with the same name is allowed per platform. Assembly paths: Assets/Plugins/es/Npgsql.resources.dll, Assets/Plugins/zh-CN/Npgsql.resources.dll, Assets/Plugins/de/Npgsql.resources.dll, Assets/Plugins/ja/Npgsql.resources.dll, Assets/Plugins/fr/Npgs
После установки Postgresql добавьте в переменные среды его:
У меня версия Postgresql 9.4.26 .
2 Этап скомпилировать или с помощью visual studio 2019 достать нужную библиотеку Npgsql.dll. Для этого в visual studio 2019 воспользуйтесь NuGet:
Для меня подошла Npgsql.2.2.7 . После установки она появиться в паке H:\Unity\postgres\Packages\Npgsql.2.2.7\lib\net20 ту в папке lib есть папки net20,net35,net40,net45 я так понял надо выбирать в зависимости от того какой Api Compatibility Level* стоит у вас, у меня: .NET Standard 2.0
Берёте из папке именно тот Npgsql.dll который нужен вам и создаёте в папке Assets(у меня она по пути H:\Unity\postgres\Assets) папку Plugins . Помещаете туда Npgsql.dll .
Вуаля теперь у вас есть все чтоб подключиться к Postgresql.
3 Этап пишете класс Test у меня она выглядит так :
using System.Collections;
using System.Collections.Generic;
using System;
//using System.Data; ## REMOVE THIS
using Npgsql;
using UnityEngine;
public class Test
{
public static void postgres()
{
string connectionString =
"Port = 5433;"+
"Server=localhost;" +
"Database=db_eco;" +
"User ID=postgres;" +
"Password=postgres;";
// IDbConnection dbcon; ## CHANGE THIS TO
NpgsqlConnection dbcon;
dbcon = new NpgsqlConnection(connectionString);
dbcon.Open();
//IDbCommand dbcmd = dbcon.CreateCommand();## CHANGE THIS TO
NpgsqlCommand dbcmd = dbcon.CreateCommand();
// requires a table to be created named employee
// with columns firstname and lastname
// such as,
// CREATE TABLE employee (
// firstname varchar(32),
// lastname varchar(32));
string sql =
"SELECT vendor, product " +
"FROM visualinspectiondata.devices";
dbcmd.CommandText = sql;
//IDataReader reader = dbcmd.ExecuteReader(); ## CHANGE THIS TO
NpgsqlDataReader reader = dbcmd.ExecuteReader();
while (reader.Read())
{
//string FirstName = (string)reader.GetString(0);
string LastName = (reader.IsDBNull(1)) ? "NULL" : reader.GetString(1).ToString();
//string LastName = (string)reader.GetString(1);
Debug.Log("Name: " + " " + LastName);
//Console.WriteLine();
}
// clean up
reader.Close();
reader = null;
dbcmd.Dispose();
dbcmd = null;
dbcon.Close();
dbcon = null;
}
}






