Alternative to storing data in DB's

Hi,

I don’t know much about any type of DB modules on any game development platform, I’m looking for a way to simply store a string somewhere, and then have 7 more strings related to it. So for example, if I have 10 words, and I want to store a bunch of synonyms for each word, and have them be displayed to the user depending on what they click on, what would I use to accomplish this?

The user would also, themselves be able to add and remove their own words from the list, create new lists of words… etc…

Thoughts?

Thanks!

So you want to have a synonym lookup table? The easiest way is to use a Dictionary of a List of strings.

public class SynonymDB
{
    private Dictionary<string, List<string>> db = new Dictionary<string,List<string>>();
    private List<List<string>> lists = new List<List<string>>();
    public void AddWord(string aWord, string aRelated)
    {
        var list = FindSynonyms(aRelated);
        if (!list.Contains(aWord))
            list.Add(aWord);
        for(int i = 0; i < list.Count; i++)
        {
            if (!db.ContainsKey(aWord))
                db.Add(aWord, list);
        }
    }
    public List<string> FindSynonyms(string aWord)
    {
        List<string> list;
        if (!db.TryGetValue(aWord, out list))
        {
            list = new List<string>();
            lists.Add(list);
            list.Add(aWord);
            db.Add(aWord, list);
        }
        return list;
    }
    public void RemoveWord(string aWord)
    {
        List<string> list;
        if (db.TryGetValue(aWord, out list))
        {
            list.Remove(aWord);
            db.Remove(aWord);
            if (list.Count == 0)
                lists.Remove(list);
        }
    }
    public void ReadFromStream(System.IO.Stream aStream)
    {
        db.Clear();
        lists.Clear();
        using(var reader = new System.IO.StreamReader(aStream))
        {
            string text = reader.ReadLine();
            var data = text.Split(',');
            var list = new List<string>();
            list.AddRange(data);
            for(int i = 0; i < data.Length; i++)
            {
                db.Add(data*, list);*

}
}
}
public void WriteToStream(System.IO.Stream aStream)
{
using (var writer = new System.IO.StreamWriter(aStream))
{
StringBuilder sb = new StringBuilder();
foreach(var i in lists)
{
if (i.Count == 0)
continue;
sb.Length = 0;
sb.Append(i[0]);
for (int n = 1; n < i.Count; n++)
sb.Append(“,”).Append(i[n]);
writer.WriteLine(sb.ToString());
}
}
}
}
This is a quick and dirty solution. You shouldn’t add or remove elements to the lists inside the dictionary manually. Always use the methods this class provides. You can save and load the “database” with WriteToStream / ReadFromStream.

Thank you for that. Although as a beginner its tough to follow, I’ll have to break it down piece by piece.

How would one print out the actual key name on the screen? I know how to print the value of the key in the console but not the key itself.