简体   繁体   English

Unity SQLite本地数据库

[英]Unity SQLite local database

I am making a small game in Unity and I'm in need of a database. 我正在Unity做一个小游戏,我需要一个数据库。 I tried using SQLite database because that seemed to be recommended by the web. 我尝试使用SQLite数据库,因为这似乎是Web推荐的。

Now I'm having troubles with actually connecting to the local database via c#. 现在,我很难通过c#连接到本地数据库。

I implemented the Data in SQLite .dll's. 我在SQLite .dll中实现了数据。 I am trying to get 1 name from the database that I created using SQLite developer. 我试图从使用SQLite开发人员创建的数据库中获得1个名称。

Below is my DataConnection class, which I use to connect to the database. 下面是我的DataConnection类,该类用于连接数据库。

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: 运行代码时出现以下错误:

在此处输入图片说明

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?: 所以我猜想它与32/64位不匹配有关,或者创建这样的脚本实例是否有问题?:

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. 我熟悉使用数据库,这是SQLite的新手。

It says it cannot load the native sqlite.dll because you have there 64 bit version and it needs 32 bit 它说它无法加载本地sqlite.dll,因为您有64位版本,并且需要32位

Place this in your app folder https://www.sqlite.org/2015/sqlite-dll-win32-x86-3081001.zip 将此放置在您的应用程序文件夹中https://www.sqlite.org/2015/sqlite-dll-win32-x86-3081001.zip

Please fill that empty catch on line 38 with a throw; 请在第38行的空渔获物上扔一掷;

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. 您也可以发布您的连接字符串,以便我更好地回答。

I got it working now. 我现在开始工作了。 The problem was my that one of the SQLite .dll's was still 32bit. 问题是我的SQLite .dll仍然是32位。 I did this tutorial over again and searched google for the 64bit .dll files and now it's working. 我再次完成了教程,并在google中搜索了64位.dll文件,现在它可以工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM