データベース編2

今日は、WordNetでダウンロードしたindex.adjファイルのデータから5文字以内のファイルをデータベースに入れました。

using UnityEngine;
using System.Data;




namespace DictionaryDatabase
{
    public class DatabaseManager : MonoBehaviour
    {
        private void Start()
        {
            ConnectToDatabase();
            InsertWords();
        }

        private void ConnectToDatabase()
        {
            string databasePath = "URI=file:" + Application.persistentDataPath + "/testdatabase.db";
            IDbConnection connection = new SqliteConnection(databasePath);
            connection.Open();

            if (connection.State == ConnectionState.Open)
            {
                Debug.Log("データベースへの接続に成功しました!");
            }
            else
            {
                Debug.LogError("データベースへの接続に失敗しました!");
            }

            connection.Close();
            Debug.Log(Application.persistentDataPath);
        }

        private void InsertWords()
        {
            string filePath = "Assets/Plugins/dict/index.adj";
            if (CheckFileExists(filePath))
            {
                string[] lines = System.IO.File.ReadAllLines(filePath);

                Debug.Log("データの読み込みが完了しました!行数: " + lines.Length);
                foreach (string line in lines)
                {
                    Debug.Log("行の内容: " + line);
                }

                string databasePath = "URI=file:" + Application.persistentDataPath + "/testdatabase.db";
                IDbConnection connection = new SqliteConnection(databasePath);
                connection.Open();

                foreach (string line in lines)
                {
                    if (!string.IsNullOrEmpty(line))
                    {
                        string[] fields = line.Split(' ');
                        if (fields.Length >= 4) // インデックスが範囲内にあるかを確認
                        {
                            string word = fields[0];
                            string pos = fields[3];

                            if (word.Length <= 5 && IsAlpha(word))
                            {
                                Debug.Log("挿入するデータ: wordEN=" + word + ", pos=" + pos);

                                IDbCommand command = connection.CreateCommand();
                                command.CommandText = $"INSERT INTO word_table (wordEN, pos) VALUES ('{word}', '{pos}')";
                                command.ExecuteNonQuery();
                            }
                        }
                    }
                }

                connection.Close();
                Debug.Log("データの挿入が完了しました!");
            }
            else
            {
                Debug.LogError("ファイルが見つかりません: " + filePath);
            }
        }

        private bool CheckFileExists(string filePath)
        {
            return System.IO.File.Exists(filePath);
        }

        private bool IsAlpha(string word)
        {
            foreach (char c in word)
            {
                if (!char.IsLetter(c))
                    return false;
            }
            return true;
        }
    }
}

ずっとデータの挿入が完了しました!とメッセージが出ているのにデータベース上では見つからず。
1行をカウントしているのかと疑って、一行一行表示させたらめっちゃ重かったけど、ちゃんとインサートされていました。
これはもともとちゃんとインサートされていたのに気づかなかったのかもしれないし、処理が滑ったのかもしれないし、ちょっと分からず。

using Mono.Data.SqliteClient;

ChatGptではusing Mono.Data.Sqliteで表示されるので気を付ける。
明日はGlosbe APIを使って、意味をデータベースに入れていきます。

この記事が参加している募集

この記事が気に入ったらサポートをしてみませんか?