首页 > 数据库 >SQLiteTableTool

SQLiteTableTool

时间:2023-05-14 11:34:07浏览次数:45  
标签:Start void tableName SQLiteTableTool manager Debug public

// 提供了一些关于表操作的快捷方法(不是必要组件),当然,SQLiteStudio之类的强大且专业的工具软件也是不可或缺的

// 使用提示:通过在一个脚本的字段上添加[SerializeToTableCol]特性可以直接读取到TableTool的列表里,然后可以根据这个列表创建表(当然也可以手动填写这个表再创建)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

namespace SaveAndLoader
{
    public class SQLiteTableTool : MonoBehaviour
    {
        public SQLiteManager manager;

        public string tableName;
        public string tableName2;

        public List<TableColInfo> list_ColInfos;
        public MonoScript monoScriptWantToGetColInfos;
        public void Start()
        {
            if (manager == null)
                if (gameObject.TryGetComponent<SQLiteManager>(out manager) == false)
                    Debug.LogError($"{this.name}.SQLiteTableTool未连接SQLiteManager。");
        }
        // ==================== ==================== ==================== ====================
        [ContextMenu("IsTableExist?")]
        public void PrintIsTableExist()
        {
            Start();
            manager.RefreshConnectionString();
            if (manager.Connect())
            {
                if (manager.IsTableExist(tableName))
                    Debug.Log($"<color=pink>{tableName}</color>存在于<color=pink>{manager.DBName}</color>。");
                else
                    Debug.LogError($"<color=pink>{tableName}</color>不存在于<color=pink>{manager.DBName}</color>!");

                manager.DisconnectAndClear();
            }
        }

        [ContextMenu("GetList_ColInfosFromTable")]
        public void GetList_ColInfosFromTable()
        {
            Start();
            manager.RefreshConnectionString();
            if (manager.Connect())
            {
                if (!manager.IsTableExist(tableName))
                {
                    Debug.LogError("表不存在。");
                    manager.DisconnectAndClear();
                    return;
                }
                // ---------------- ---------------- ----------------
                list_ColInfos = manager.GetTableColInfos(tableName);

                manager.DisconnectAndClear();
            }
        }

        [ContextMenu("GetList_ColInfosFromMonoScript")]
        public void GetList_ColInfosFromMonoScript()
        {
            if (monoScriptWantToGetColInfos == null)
            { Debug.LogError("没有指定MonoScript。"); return; }

            list_ColInfos = manager.GetColInfosFromMonoScript(monoScriptWantToGetColInfos.GetClass());
        }

        [ContextMenu("CreateTableFromList_ColInfos")]
        public void CreateTableFromList_ColInfos()
        {
            Start();
            manager.RefreshConnectionString();
            if (manager.Connect())
            {
                manager.CreateTable(tableName, list_ColInfos);
                manager.DisconnectAndClear();
            }
        }

        [ContextMenu("RenameToTableName2")]
        public void RenameToTableName2()
        {
            if (tableName2 == null | tableName2 == "")
            { Debug.LogError("TableName2错误!"); return; }

            if (tableName2 == tableName)
            { Debug.LogError("修改的表名与原表名相同!"); return; }
            //---------------- ---------------- ----------------
            Start();
            manager.RefreshConnectionString();
            if (manager.Connect())
            {
                if (manager.IsTableExist(tableName))
                    manager.RenameTable(tableName, tableName2);
                manager.DisconnectAndClear();
            }
        }

        [ContextMenu("DeleteTabel")]
        public void DeleteTabel()
        {
            Start();
            manager.RefreshConnectionString();
            if (manager.Connect())
            {
                if (manager.IsTableExist(tableName))
                {
                    if (EditorUtility.DisplayDialog($"Warning", $"Delete Table:{tableName}", "DELETE!", "Cancel"))
                        manager.DeleteTable(tableName);
                }
                manager.DisconnectAndClear();
            }
        }

        [ContextMenu("GetRows")]
        public void GetRows()
        {
            Start();
            manager.RefreshConnectionString();
            if (manager.Connect())
            {
                if (!manager.IsTableExist(tableName))
                    Debug.LogError("表不存在。");
                else
                {
                    Debug.Log($"{tableName}共有" + manager.GetTableRows(tableName) + "条记录。");
                }

                manager.DisconnectAndClear();
            }
        }
    }
}

 

标签:Start,void,tableName,SQLiteTableTool,manager,Debug,public
From: https://www.cnblogs.com/xhbnfcl/p/17398963.html

相关文章