- Home /
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!
Answer by Bunny83 · Nov 09, 2015 at 02:22 PM
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[i], 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.
The additional "lists" variable usually isn't needed, but it simplifies the saving of the whole structure. The save file would look like this:
word1,synonym,synonym,synonym
word2,synonym
word3,synonym,synonym,synonym,synonym
word4,synonym,synonym,synonym
word5
So in each line you have one word group which belong together. The words are seperated by commas. It's also possible to have words without synonyms. See "word5".
FindSynonyms will always return a List. Even when the word doesn't exist yet in the database, it will add it. If you want / need a way to check if a word exists, you might want to add another method for that.
public bool Contains(string aWord)
{
return db.Contains$$anonymous$$ey(aWord);
}
Answer by Fexception · Nov 11, 2015 at 10:53 AM
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.
Your answer
Follow this Question
Related Questions
Database (SQLite) Setup for Unity 0 Answers
Sq lite Date base in unity 3d? 1 Answer
Data Storage 5 Answers
Storing Constant data in a mobile game 0 Answers
Database entegration or data storage 0 Answers