๐ท Insert Data
๐ถ Document
https://docs.microsoft.com/ko-kr/rest/api/storageservices/Understanding-the-Table-Service-Data-Model
- PartitionKey, RowKey๋ ํ์ ํค์
- ์ ๋ํฌํด์ผํจ
๐ถ 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 = "projects";
[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();
}
}
IsReady = true;
onDataManagerReady?.Invoke();
}
}
- DataManager.cs์์ ํ์ํ ๊ฒ๋ง ๋จ๊ธฐ๊ณ , ๋๋จธ์ง๋ ์ญ์ ํจ.
- connectionString : Azure ์ฐ๊ฒฐ ๋ฌธ์์ด
- projectsTableName : ์์ฑ/๋ถ๋ฌ์ฌ ํ ์ด๋ธ ์ด๋ฆ
- tryCreateTableOnStart : True์ผ๋ -> projectsTableName๋ผ๋ ํ ์ด๋ธ์ด ์์ผ๋ฉด ํด๋น ์ด๋ฆ์ผ๋ก ํ ์ด๋ธ ์์ฑ
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();
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 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; }
}
}
- project ์ด๋ฆ students๋ก ๋ณ๊ฒฝํจ
- Student Class : ๋ฐ์ดํฐ ๊ตฌ์กฐ
- playํ๋ฉด ์์ ๊ฐ์ log๊ฐ ์ฐํ
- students๋ผ๋ ์ด๋ฆ์ ํ ์ด๋ธ์ด ์์ฑ๋จ
- ๋ฐ์ดํฐ๋ ์ฝ์ ๋ ๊ฒ์ ํ์ธ
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();
IsReady = true;
onDataManagerReady?.Invoke();
}
public void InsertData()
{
TableOperation operation;
TableResult result;
Student student;
student = new Student
{
PartitionKey = "01",
RowKey = "01",
Name = "Minu",
Email = "Minu01@example.com",
Address = "Military Village"
};
operation = TableOperation.InsertOrMerge(student);
result = projectsTable.ExecuteAsync(operation).Result;
}
}
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; }
}
}
- ๋ฃ๊ณ ์ํ๋ ๋ฐ์ดํฐ๋ฅผ ๋ฐ๊ฟ
- play ํด๋ณด๋ฉด ์ด๋ฒ์๋ ๋ฐ์ดํฐ๊ฐ ์ฝ์ ๋ ๊ฒ์ ํ์ธํ ์ ์์.
๐ถ Potal
- ํฌํธ์์ ์ง์ ๋ฐ์ดํฐ๋ฅผ ๋ฃ์ ์ ์์
- Add Entity
- Value ๊ธฐ์
- Add Entity
- ๋ฐ์ดํฐ๊ฐ ์ถ๊ฐ๋ ๊ฒ ํ์ธ
'Unity > Database' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
04. Azure CosmosDB - Delete (0) | 2021.09.06 |
---|---|
03. Azure CosmosDB - Read (0) | 2021.08.29 |
01. Azure CosmosDB Settings (0) | 2021.08.21 |
00. Azure ์ฒดํ๊ณ์ ํ์ฑํ (0) | 2021.08.20 |
06. Firebase Realtime Database - ์ฌํ (1) | 2021.08.15 |