Unity/Database

04. Azure CosmosDB - Delete

๐Ÿ”ท Delete Data

๐Ÿ”ถ Unity

- ํ˜„์žฌ ๋ฐ์ดํ„ฐ ํ…Œ์ด๋ธ”

 

 

 

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.Table;
using MRTK.Tutorials.AzureCloudServices.Scripts.Domain;
using UnityEngine;
using UnityEngine.Events;

namespace MRTK.Tutorials.AzureCloudServices.Scripts.Managers
{
    public class DataManager : MonoBehaviour
    {
        public bool IsReady { get; private set; }

        [Header("Base Settings")]
        [SerializeField]
        private string connectionString = default;
        [Header("Table Settings")]
        [SerializeField]
        private string projectsTableName = "students";
        [SerializeField]
        private bool tryCreateTableOnStart = true;
        [Header("Events")]
        [SerializeField]
        private UnityEvent onDataManagerReady = default;
        [SerializeField]
        private UnityEvent onDataManagerInitFailed = default;

        private CloudStorageAccount storageAccount;
        private CloudTableClient cloudTableClient;
        private CloudTable projectsTable;

        private async void Awake()
        {
            storageAccount = CloudStorageAccount.Parse(connectionString);
            cloudTableClient = storageAccount.CreateCloudTableClient();
            projectsTable = cloudTableClient.GetTableReference(projectsTableName);
            if (tryCreateTableOnStart)
            {
                try
                {
                    if (await projectsTable.CreateIfNotExistsAsync())
                    {
                        Debug.Log($"Created table {projectsTableName}.");
                    }
                }
                catch (StorageException ex)
                {
                    Debug.LogError("Failed to connect with Azure Storage.\nIf you are running with the default storage emulator configuration, please make sure you have started the storage emulator.");
                    Debug.LogException(ex);
                    onDataManagerInitFailed?.Invoke();
                }
            }

            // InsertData();
            // ReadData();
            DeleteData();

            IsReady = true;
            onDataManagerReady?.Invoke();

        }

        public void InsertData()
        {
            TableOperation operation;
            TableResult result;

            Student student;

            student = new Student
            {
                PartitionKey = "00",
                RowKey = "00",
                Name = "OJUI",
                Email = "ojui@example.com",
                Address = "Developer Village"
            };

            operation = TableOperation.InsertOrMerge(student);
            result = projectsTable.ExecuteAsync(operation).Result;
        }

        public async void ReadData()
        {
            CloudTable table = projectsTable;
            string partitionKey = "02";
            string rowKey = "02";

            TableOperation retrieveOperation = TableOperation.Retrieve<Student>(partitionKey, rowKey);
            TableResult result = await table.ExecuteAsync(retrieveOperation);
            Student student = result.Result as Student;

            if (student != null)
            {
                Debug.Log($"----- READ FUNCTION -----");
                Debug.Log($"์ด๋ฆ„ : {student.Name}");
                Debug.Log($"์ฃผ์†Œ : {student.Address}");
            }
        }

        public async void DeleteData()
        {
            CloudTable table = projectsTable;

            Student entity;

            entity = new Student
            {
                PartitionKey = "01",
                RowKey = "01",
                ETag = "*"
            };

            TableOperation retrieveOperation = TableOperation.Delete(entity);
            TableResult result = await table.ExecuteAsync(retrieveOperation);
        }

    }

    public class Student : TableEntity
    {
        public Student(string PartitionKey, string RowKey, string Name, string Email = null, string Address = null)
        {
            this.PartitionKey = PartitionKey;
            this.RowKey = RowKey;
            this.Name = Name;
            this.Email = Email;
            this.Address = Address;
        }

        public Student() { }

        public string Name { get; set; }

        public string Email { get; set; }

        public string Address { get; set; }

    }

}

- PartitionKey:01, RowKey:01์ธ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œ

 

 

 

- play

- 01 ๋ฐ์ดํ„ฐ๊ฐ€ ์‚ญ์ œ๋œ ๊ฒƒ ํ™•์ธ

 

 

 

๐Ÿ”ถ Potal

- ์ง์ ‘ ์‚ญ์ œ ๊ฐ€๋Šฅ

 

 

 

- ์‚ญ์ œํ•˜๊ณ  ์‹ถ์€ ๋ฐ์ดํ„ฐ ํด๋ฆญ

- Delete Entities

 

 

 

- ๋ฐ์ดํ„ฐ๊ฐ€ ์‚ญ์ œ๋œ ๊ฒƒ ํ™•์ธazure

'Unity > Database' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[์œ ๋‹ˆํ‹ฐ Json ํŒŒ์‹ฑ] Newtonsoft.Json & Unity JSON Utility  (0) 2022.05.26
03. Azure CosmosDB - Read  (0) 2021.08.29
02. Azure CosmosDB - Insert  (0) 2021.08.28
01. Azure CosmosDB Settings  (0) 2021.08.21
00. Azure ์ฒดํ—˜๊ณ„์ • ํ™œ์„ฑํ™”  (0) 2021.08.20