4

I am making a small game in Unity and I'm in need of a database. I tried using SQLite database because that seemed to be recommended by the web.

Now I'm having troubles with actually connecting to the local database via c#.

I implemented the Data in SQLite .dll's. I am trying to get 1 name from the database that I created using SQLite developer.

Below is my DataConnection class, which I use to connect to the database.

using UnityEngine;
using System.Collections;
using System.Data;
using Mono.Data.SqliteClient;

public class Dataconnection : MonoBehaviour {

private string _constr = @"Data Source=C:\Program Files (x86)\SharpPlus\SqliteDev\GameDatabase.db;Version=3;";
private IDbConnection _dbc;
private IDbCommand _dbcm;
private IDataReader _dbr;


public Dataconnection()
{

}

public Dataconnection(string constring)
{
        _constr = constring;
}

public string ExcecuteQuery(string SQL)
{
    string output = "";

    try
    {
        _dbc = new SqliteConnection(_constr);
        _dbc.Open();
        _dbcm = _dbc.CreateCommand();
        _dbcm.CommandText = SQL;
        _dbr = _dbcm.ExecuteReader();
    }
    catch
    {

    }

    while (_dbr.Read())
    {
        output = _dbr.GetString(0);
    }

    _dbc.Close();

    return output;
}   
}

Then I call the following method from another class:

datacon.ExcecuteQuery("SELECT name FROM employee WHERE empid = 1;");

I get the following errors when running the code:

enter image description here

So I'm guessing it has something to do with a 32/64 -bit mismatch or is there something wrong with creating an instance of a script like this?:

private Dataconnection datacon;

void Start()
{
    datacon = new Dataconnection();
}

Happy to receive any help at all. I'm familiar with using database, just new to SQLite.

2 Answers 2

1

It says it cannot load the native sqlite.dll because you have there 64 bit version and it needs 32 bit

Place this in your app folder https://www.sqlite.org/2015/sqlite-dll-win32-x86-3081001.zip

Please fill that empty catch on line 38 with a throw;

as there is an exception hidden there which is the true cause of the null reference.

You could also post your connection string so I could make this answer better.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, that solved 1 of the errors, now I still have the NullReferenceException error. Any idea how to solve that?
The connectionstring: @"Data Source=C:\Program Files (x86)\SharpPlus\SqliteDev\GameDatabase.db;Version=3;";
You were right, the exception gives me "incorrect connection string". Something must be wrong in my con string. Btw it's in my dataconnection.cs up here.
I moved the database to \documents\ and the connection accordingly but it still doesn't seem to work.
I checked it and it was still the same. I keep getting the NullReference exception error.
0

I got it working now. The problem was my that one of the SQLite .dll's was still 32bit. I did this tutorial over again and searched google for the 64bit .dll files and now it's working.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.