using UnityEngine;
using System.Collections;
using Mono.Data.Sqlite;
using System.Data;
using System;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Runtime.Serialization;
///
/// 数据库操作工具类 封装了通用的数据库增、删、改、查接口
///
public class DBAccess {
private SqliteConnection dbConnection;
private SqliteCommand dbCommand;
private SqliteDataReader reader;
public bool isOpen=false;
public DBAccess(string connectionStr)
{
OpenDB(connectionStr);
}
private void OpenDB(string connectionStr)
{
try
{
dbConnection = new SqliteConnection(connectionStr);
dbConnection.Open();
isOpen = true;
Debug.Log("数据库连接成功!");
}
catch
{
isOpen = false;
Debug.Log("数据库连接失败");
throw new Exception("数据库连接异常!");
}
}
///
/// 断开数据库连接
///
public void CloseSqlConnectionDB()
{
if(dbCommand!=null)
{
dbCommand.Dispose();
}
dbCommand = null;
if(reader!=null)
{
reader.Close();
}
reader = null;
if(dbConnection!=null)
{
dbConnection.Close();
dbConnection.Dispose();
}
dbConnection = null;
Debug.Log("数据库连接已断开");
}
///
/// 根据键入的命令,操作数据库
///
/// 需要读取的方式及内容
///
public SqliteDataReader ExecuteQuery(string sqlQuery)
{
dbCommand = dbConnection.CreateCommand();
dbCommand.CommandText = sqlQuery;
reader = dbCommand.ExecuteReader();
return reader;
}
///
/// 直接操作数据库的接口
///
///
///
///
public SqliteDataReader ExecuteQuery(SqliteParameter[] sqliteParameters, string CommandText)
{
dbCommand = dbConnection.CreateCommand();
dbCommand.Parameters.AddRange(sqliteParameters);
dbCommand.CommandText = CommandText;
reader = dbCommand.ExecuteReader();
return reader;
}
///
/// 读取整张数据表
///
///
///
public SqliteDataReader ReaderFullTable( string tableName)
{
string query = "SELECT * FROM " + tableName;
return ExecuteQuery(query);
}
///
/// 更新指定数据表内的数据
///
/// The values.
/// 数据表名称
/// 字段名
/// 字段名对应的数据
/// 关键字
/// 关键字对应的值
public bool UpdateValues(string tableName,string[] colNames,string[] colValues,string key,string operation,string value)
{
//当字段名称和字段数值不对应时引发异常
if(colNames.Length!=colValues.Length) {
throw new SqliteException("colNames.Length!=colValues.Length");
}
string queryString = "UPDATE " + tableName + " SET " + colNames[0] + "=" + $"'{colValues[0]}'";
for(int i=1; i0;
}
///
/// 删除指定数据表内的数据
///
/// The values.
/// 数据表名称
/// 字段名
/// 字段名对应的数据
public bool DeleteValuesOR(string tableName,string[] colNames,string[] operations,string[] colValues)
{
//当字段名称和字段数值不对应时引发异常
if(colNames.Length!=colValues.Length || operations.Length!=colNames.Length || operations.Length!=colValues.Length) {
throw new SqliteException("colNames.Length!=colValues.Length || operations.Length!=colNames.Length || operations.Length!=colValues.Length");
}
string queryString = "DELETE FROM " + tableName + " WHERE " + colNames[0] + operations[0] + colValues[0];
for(int i=1; i 0;
}
///
/// 删除指定数据表内的数据
///
/// The values.
/// 数据表名称
/// 字段名
/// 运算符
/// 字段名对应的数据
public bool DeleteValuesAND(string tableName,string[] colNames,string[] operations,string[] colValues)
{
//当字段名称和字段数值不对应时引发异常
if(colNames.Length!=colValues.Length || operations.Length!=colNames.Length || operations.Length!=colValues.Length) {
throw new SqliteException("colNames.Length!=colValues.Length || operations.Length!=colNames.Length || operations.Length!=colValues.Length");
}
string queryString = "DELETE FROM " + tableName + " WHERE " + colNames[0] + operations[0] + colValues[0];
for(int i=1; i0;
}
///
///在数据库中创建表格
///
///表格名称
///表格所有的列名
///表格所有列所对应的格式
///
public SqliteDataReader CreatTable(string name,string [] col,string []colType)
{
if(col.Length!=colType.Length)
{
throw new SqliteException("创建的数据类型与数据本身未对应!");
}
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 InsterInto(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 Select(string tableName, string[] items, string[] key,string[] value)
{
if(key.Length!=value.Length)
{
throw new SqliteException("比较条件未满足!");
}
string query = "SELECT " + items[0];
for (int i = 1; i < items.Length; i++)
{
query += "," + items[i];
}
query += " FROM " + tableName + " WHERE " + key[0] + "=" + $"'{value[0]}'";
for (int i = 1; i < key.Length; i++)
{
query += " AND " + key[i] + "=" + $"'{value[i]}'";
}
return ExecuteQuery(query);
}
///
/// 查询一个表中指定列的所有数据
///
///
/// 列名称
///
public SqliteDataReader GetAllLineMess(string tableName,string item)
{
string query="SELECT DISTINCT "+item+" FROM "+tableName;
return ExecuteQuery(query);
}
///
/// 按时间段查找所有的值
///
///
/// 开始时间 注意格式为"yyyy-m-dd hh:mm:ss"
/// 结束时间 注意格式为"yyyy-m-dd hh:mm:ss"
///
public SqliteDataReader SelectTime(string tableName,string key,string startTime,string finishTime)
{
string query = "SELECT * FROM " + tableName + " WHERE " + key + " BETWEEN " + $"'{startTime}'" + " AND " + $"'{finishTime}'";
return ExecuteQuery(query);
}
///
/// 根据一个键和值查找
///
///
///
///
///
public SqliteDataReader Select(string tableName, string key, string value)
{
string query = "SELECT * FROM " + tableName + " WHERE " + key + "=" +$"'{value}'" ;
return ExecuteQuery(query);
}
///
/// 根据时间段和一个键和值进行查找
///
///
///
/// 注意格式为"yyyy-m-dd hh:mm:ss"
/// 注意格式为"yyyy-m-dd hh:mm:ss"
///
///
///
public SqliteDataReader SelectDataAndName(string tableName, string timeKey, string startTime, string finishTime,string nameKey,string nameValue)
{
string query = "SELECT * FROM " + tableName + " WHERE " + timeKey + " BETWEEN " +$"'{startTime}'" + " AND " + $"'{finishTime}'" + " AND " + nameKey + "=" + $"'{nameValue}'";
return ExecuteQuery(query);
}
///
/// 根据多个条件查询所有数据
///
///
///
///
///
public SqliteDataReader Select(string tableName, string[] key, string[] value)
{
string query = "SELECT *";
query += " FROM " + tableName + " WHERE " + key[0] + "=" + $"'{value[0]}'";
for (int i = 1; i < key.Length; i++)
{
query += " AND " + key[i] + "=" + $"'{value[i]}'";
}
return ExecuteQuery(query);
}
}