当前位置:   article > 正文

Unity关于轻量级数据库Sqlite的封装使用_unity 轻量数据库

unity 轻量数据库
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mono.Data.Sqlite;
/*
 * Author:W
 * sqlite轻量型数据库使用 
 * 【注意:记得把Mono.Data.Sqlite.dll文件放在项目工程的Plugins文件夹下】
 */
public class SqlLiteManager : MonoBehaviour
{
    public static SqlLiteManager Instance;

    private SqliteConnection sqliteConnection;
    private SqliteCommand sqliteCommand;
    void Awake()
    {
        Instance = this;
    }

    /// <summary>
    /// 数据库打开
    /// </summary>
    /// <param name="path"></param>
    public void Open(string path)
    {
        sqliteConnection = new SqliteConnection("Data Source="+path);

        if (sqliteConnection != null)
            sqliteConnection.Open();
    }


    /// <summary>
    /// 执行命令
    /// 创建一张表SQL语句
    /// sqlStr = "create table if not exists User(uid integer primary key autoincrement, name text,score integer)";
    /// 添加一条数据
    /// sqlStr = "insert into User(name,score) values('W',66)";
    /// 删除一条数据
    /// sqlStr ="delete from User where uid=1 ";
    /// 修改一条数据
    /// sqlStr = "update User set name='L' where uid = 1";
    /// </summary>
    /// <param name="sqlStr"></param>
    public void ExcuteNonQuery(string sqlStr)
    {
        sqliteCommand = new SqliteCommand(sqlStr,sqliteConnection);

        if (sqliteCommand == null) return;

        sqliteCommand.ExecuteNonQuery();
        sqliteCommand.Dispose();
    }




    /// <summary>
    /// 执行单个结果查询
    /// sqlStr ="select count(*) from User";
    /// sqlStr = "select * from User where uid = 1";
    /// </summary>
    /// <param name="sqlStr"></param>
    /// <returns></returns>
    public object ExcuteScaler(string sqlStr)
    {
        sqliteCommand = new SqliteCommand(sqlStr, sqliteConnection);

        if (sqliteCommand == null) return null;

        object value =sqliteCommand.ExecuteScalar();
        sqliteCommand.Dispose();

        return value;
    }
 
    /// <summary>
    /// 执行多个结果查询
    /// sqlStr = "select * from User";
    /// </summary>
    /// <param name="sqlStr"></param>
    /// <returns></returns>
    public SqliteDataReader ExcuteQuery(string sqlStr)
    {
        sqliteCommand = new SqliteCommand(sqlStr,sqliteConnection);

        if (sqliteCommand == null) return null;

        SqliteDataReader reader = sqliteCommand.ExecuteReader();

        sqliteCommand.Dispose();
        reader.Close();

        return reader;
    }

    /// <summary>
    /// 数据库关闭
    /// </summary>
    private void Close()
    {
        if (sqliteConnection != null)
            sqliteConnection.Close();
    }
}

  • 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/108926
推荐阅读
相关标签
  

闽ICP备14008679号