赞
踩
一:准备,Mono.Data.Sqlite.dll,System.Data.dll,sqlite3.dll(X86和X86_64),这些dll文件是必须的。将他们放到Plugins文件夹下即可。dll缺一不可,否则打包出来会有问题。然后需要将API Compatibility Level切换到.NET 2.0 。
二:在网上找了一个连接和读取的脚本:
using UnityEngine;
using System.Data;
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 ()
{
}
/// <summary>
/// 打开数据库
/// </summary>
/// <param name="connectionString">Connection string.</param>
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);
}
}
/// <summary>
/// 关闭数据库
/// </summary>
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.");
}
/// <summary>
/// 执行sql语句
/// </summary>
/// <returns>The query.</returns>
/// <param name="sqlQuery">查询语句.</param>
public SqliteDataReader ExecuteQuery (string sqlQuery)
{
Debug.Log ("sql="+sqlQuery);
dbCommand = dbConnection.CreateCommand ();
dbCommand.CommandText = sqlQuery;
reader = dbCommand.ExecuteReader ();
return reader;
}
/// <summary>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。