You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
3.7 KiB
76 lines
3.7 KiB
using UnityEngine; |
|
using System.Collections; |
|
using System; |
|
using Mono.Data.Sqlite; |
|
public class DbTool |
|
{ |
|
|
|
// Use this for initialization |
|
private SqliteConnection dbConnection; |
|
public SqliteCommand dbCommand; |
|
public SqliteDataReader reader; |
|
public static string dataBase = "ScoreDB.db"; |
|
public static string connectionString = ""; |
|
//创建 SingleObject 的一个对象 |
|
private static DbTool instance = new DbTool(); |
|
|
|
//获取唯一可用的对象 |
|
public static DbTool getInstance() |
|
{ |
|
return instance; |
|
} |
|
|
|
public void OpenDB(string database) |
|
{ |
|
try |
|
{ |
|
connectionString = "Data Source=" + Application.dataPath + "/" + database + ";";//Version=3 |
|
dbConnection = new SqliteConnection(connectionString); |
|
dbConnection.Open(); |
|
dbCommand = (SqliteCommand)dbConnection.CreateCommand(); |
|
//Debug.Log("Connected to db"); |
|
|
|
} |
|
catch (Exception e) |
|
{ |
|
Debug.Log(e); |
|
} |
|
} |
|
|
|
public void CloseSqlConnection() |
|
{ |
|
if (dbCommand != null) |
|
{ |
|
dbCommand.Cancel(); |
|
} |
|
dbCommand = null; |
|
|
|
if (reader != null) |
|
{ |
|
reader.Close(); |
|
} |
|
reader = null; |
|
|
|
if (dbConnection != null) |
|
{ |
|
dbConnection.Close(); |
|
} |
|
dbConnection = null; |
|
//Debug.Log("Disconnected from db."); |
|
} |
|
|
|
public SqliteDataReader ExecuteQuery(string sqlQuery) |
|
{ |
|
try |
|
{ |
|
//dbCommand = (SqliteCommand)dbConnection.CreateCommand(); |
|
dbCommand.CommandText = sqlQuery; |
|
reader = dbCommand.ExecuteReader(); |
|
} |
|
catch (Exception e) |
|
{ |
|
Debug.Log(e); |
|
} |
|
return reader; |
|
} |
|
}
|
|
|