unity 本地数据库

2015年02月01日 09:26 0 点赞 0 评论 更新于 2025-11-21 15:52

今天我将为大家分享关于Unity本地数据库的相关内容。此前在开发过程中,我一直使用iOS原生的数据库,并通过传递消息的方式与Unity3D进行交互。在本文中,我将详细介绍如何使用C#语言在Mac操作系统下创建Unity本地数据库。我可是个C#爱好者呢!

1. 获取必要的DLL文件

首先,你需要获取Mono.Data.Sqlite.dll文件和System.Data.dll文件。如果你在Mac操作系统下使用Unity,可能会遇到找不到这两个文件的问题,至少我当时是没找到。后来,我在Windows下的Unity安装路径中找到了它们。为了方便大家,我已将这两个文件上传至网盘,没有这两个文件的朋友可以点击以下链接下载: 下载地址

2. 放置DLL文件

将下载的.zip文件解压后,把Mono.Data.Sqlite.dll文件和System.Data.dll文件放置在Unity工程的Assets文件夹中。放置完成后,在Project视图中可以看到这两个文件。

3. 编写C#脚本

原始文章没有包含Unity数据库的更新与删除方法,我在这里补充了这些方法,方便大家在开发时使用。因为在Unity中,数据库的更新和删除也是比较重要的功能。

需要注意的是,下面的脚本不要绑定在任何游戏对象上,你可以把它当作一个工具类来使用。

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

public class DbAccess
{
private SqliteConnection dbConnection;
private SqliteCommand dbCommand;
private SqliteDataReader reader;

public DbAccess(string connectionString)
{
OpenDB(connectionString);
}

public DbAccess()
{
}

public void OpenDB(string connectionString)
{
try
{
dbConnection = new SqliteConnection(connectionString);
dbConnection.Open();
Debug.Log("Connected to db");
}
catch (Exception e)
{
string temp1 = e.ToString();
Debug.Log(temp1);
}
}

public void CloseSqlConnection()
{
if (dbCommand != null)
{
dbCommand.Dispose();
}
dbCommand = null;

if (reader != null)
{
reader.Dispose();
}
reader = null;

if (dbConnection != null)
{
dbConnection.Close();
}
dbConnection = null;
Debug.Log("Disconnected from db.");
}

public SqliteDataReader ExecuteQuery(string sqlQuery)
{
dbCommand = dbConnection.CreateCommand();
dbCommand.CommandText = sqlQuery;
reader = dbCommand.ExecuteReader();
return reader;
}

public SqliteDataReader ReadFullTable(string tableName)
{
string query = "SELECT * FROM " + tableName;
return ExecuteQuery(query);
}

public SqliteDataReader InsertInto(string tableName, string[] values)
{
string query = "INSERT INTO " + tableName + " VALUES (" + values[0];
for (int i = 1; i < values.Length; ++i)
{
query += ", " + values[i];
}
query += ")";
return ExecuteQuery(query);
}

public SqliteDataReader UpdateInto(string tableName, string[] cols, string[] colsvalues, string selectkey, string selectvalue)
{
string query = "UPDATE " + tableName + " SET " + cols[0] + " = " + colsvalues[0];
for (int i = 1; i < colsvalues.Length; ++i)
{
query += ", " + cols[i] + " =" + colsvalues[i];
}
query += " WHERE " + selectkey + " = " + selectvalue + " ";
return ExecuteQuery(query);
}

public SqliteDataReader Delete(string tableName, string[] cols, string[] colsvalues)
{
string query = "DELETE FROM " + tableName + " WHERE " + cols[0] + " = " + colsvalues[0];
for (int i = 1; i < colsvalues.Length; ++i)
{
query += " or " + cols[i] + " = " + colsvalues[i];
}
Debug.Log(query);
return ExecuteQuery(query);
}

public SqliteDataReader InsertIntoSpecific(string tableName, string[] cols, string[] values)
{
if (cols.Length != values.Length)
{
throw new SqliteException("columns.Length != values.Length");
}
string query = "INSERT INTO " + tableName + "(" + cols[0];
for (int i = 1; i < cols.Length; ++i)
{
query += ", " + cols[i];
}
query += ") VALUES (" + values[0];
for (int i = 1; i < values.Length; ++i)
{
query += ", " + values[i];
}
query += ")";
return ExecuteQuery(query);
}

public SqliteDataReader DeleteContents(string tableName)
{
string query = "DELETE FROM " + tableName;
return ExecuteQuery(query);
}

public SqliteDataReader CreateTable(string name, string[] col, string[] colType)
{
if (col.Length != colType.Length)
{
throw new SqliteException("columns.Length != colType.Length");
}
string query = "CREATE TABLE " + name + " (" + col[0] + " " + colType[0];
for (int i = 1; i < col.Length; ++i)
{
query += ", " + col[i] + " " + colType[i];
}
query += ")";
return ExecuteQuery(query);
}

public SqliteDataReader SelectWhere(string tableName, string[] items, string[] col, string[] operation, string[] values)
{
if (col.Length != operation.Length || operation.Length != values.Length)
{
throw new SqliteException("col.Length != operation.Length != values.Length");
}
string query = "SELECT " + items[0];
for (int i = 1; i < items.Length; ++i)
{
query += ", " + items[i];
}
query += " FROM " + tableName + " WHERE " + col[0] + operation[0] + "'" + values[0] + "' ";
for (int i = 1; i < col.Length; ++i)
{
// 修正此处的values[0]为values[i]
query += " AND " + col[i] + operation[i] + "'" + values[i] + "' ";
}
return ExecuteQuery(query);
}
}

以上就是在Mac操作系统下使用C#语言创建Unity本地数据库的详细步骤和代码实现,希望对大家有所帮助。