当前位置:   article > 正文

Unity使用SQLite数据库基本的增删改查_unity sqlite 删除数据

unity sqlite 删除数据
using Mono.Data.Sqlite;
using System;
using System.IO;
using System.Collections.Generic;
using UnityEngine;

public class SQLManager
{ 
    //建立数据库连接                                          
    private static SqliteConnection connection;
    //数据库命令
    private static SqliteCommand command;
    //数据库阅读器
    private static SqliteDataReader reader;

    public SQLManager(string dbPath,string dbName)
    {
        //连接数据库
        OpenConnect(dbPath, dbName);
    }

    /// <summary>
    /// 打开数据库
    /// </summary>
    public void OpenConnect(string dbPath, string dbName)
    {
        try
        {
            if (!Directory.Exists(dbPath))
            {
                Directory.CreateDirectory(dbPath);
            }
            //数据库存放在 Asset/StreamingAssets
            string path = dbPath + dbName + ".db";
            //新建数据库连接
            connection = new SqliteConnection(@"Data Source = " + path);
            //打开数据库
            connection.Open();
            Debug.Log("打开数据库");
        }
        catch (Exception ex)
        {
            Debug.Log(ex.ToString());
        }
    }

    /// <summary>
    /// 关闭数据库
    /// </summary>
    public void CloseDB()
    {
        if (command != null)
        {
            command.Cancel();
        }
        command = null;

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

        if (connection != null)
        {
            //connection.Close();
        }
        connection = null;

        Debug.Log("关闭数据库");
    }

    /// <summary>
    /// 插入数据
    /// </summary>
    /// <param name="tableName"></param>
    /// <param name="values"></param>
    /// <returns></returns>
    public bool InsertValues(string tableName, List<string> values)
    {
        try
        {
            string sql = "INSERT INTO " + tableName + " values (";
            foreach (var item in values)
            {
                sql += "'" + item + "',";
            }
            sql = sql.TrimEnd(',') + ")";

            ExecuteQuery(sql);
        }
        catch (Exception e)
        {
            Debug.Log(e.ToString());
        }
        return true;
    }

    /// <summary>
    /// 删除数据
    /// </summary>
    /// <param name="tableName"></param>
    /// <param name="conditions">查询条件</param>
    /// <returns></returns>
    public SqliteDataReader DeleteValues(string tableName, List<string> conditions)
    {
        string sql = "delete from " + tableName + " where (";
        try
        {
            for (int i = 0; i < conditions.Count - 1; i += 2)
            {
                sql += conditions[i] + "='" + conditions[i + 1] + "' and ";
            }
            sql = sql.Substring(0, sql.Length - 4) + ");";
        }
        catch (Exception e)
        {
            Debug.Log(e.ToString());
        }
        return ExecuteQuery(sql);
    }

    /// <summary>
    /// 更新数据
    /// </summary>
    /// <param name="tableName"></param>
    /// <param name="values">需要修改的数据</param>
    /// <param name="conditions">修改的条件</param>
    /// <returns></returns>
    public SqliteDataReader UpdataData(string tableName, List<string> values, List<string> conditions)
    {
        string sql = "update " + tableName + " set ";
        for (int i = 0; i < values.Count - 1; i += 2)
        {
            sql += values[i] + "='" + values[i + 1] + "',";
        }
        sql = sql.TrimEnd(',') + " where (";
        for (int i = 0; i < conditions.Count - 1; i += 2)
        {
            sql += conditions[i] + "='" + conditions[i + 1] + "' and ";
        }
        sql = sql.Substring(0, sql.Length - 4) + ");";
        Debug.Log("更新成功");
        return ExecuteQuery(sql);
    }

    /// <summary>
    /// 查询数据
    /// </summary>
    /// <param name="tableName">表名</param>
    /// <returns></returns>
    public List<string> GetDataBySqlQuery(string tableName)
    {
        List<string> list = new List<string>();
        try
        {
            string queryString = "SELECT * FROM " + tableName;
            reader = ExecuteQuery(queryString);
            while (reader.Read())
            {
                for (int i = 0; i < reader.FieldCount; i++)
                {
                    object obj = reader.GetValue(i);
                    list.Add(obj.ToString());
                }
            }
        }
        catch (Exception e)
        {
            Debug.Log(e.ToString());
        }
        return list;
    }

    /// <summary>
    /// 执行SQL命令
    /// </summary>
    /// <param name="queryString">SQL命令字符串</param>
    /// <returns></returns>
    private SqliteDataReader ExecuteQuery(string queryString)
    {
        command = connection.CreateCommand();
        command.CommandText = queryString;
        reader = command.ExecuteReader();
        return reader;
    }

    /// <summary>
    /// 删除表
    /// </summary>
    /// <param name="tableName"></param>
    /// <returns></returns>
    public SqliteDataReader DeleteTable(string tableName)
    {
        string sql = "DROP TABLE " + tableName;
        return ExecuteQuery(sql);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/127143
推荐阅读
相关标签
  

闽ICP备14008679号