![]() |
---|
![]() | |
---|---|
Michael Colombo - July 5, 2020 - Read time: 6 min
Organizing Data
Last time we talked about input and recording information as variables, and we created a very limited player profile for a player through the console window. As mentioned before, there are other ways to manage information, there are various database types that can be used. If you ever tried creating a website before, you might have seen the term SQL or MySQL pop out now and then. SQL is a language made specifically to handle information inside a database file in dynamic ways, while MySQL is an interface that can be used to create, read and modify SQL-based databases. MySQL is also used in the most famous WoW server emulator "MaNGOS". Since we’re just getting started with data management, we can use something much simpler than SQL.
CSV Files
Information, also called data, can be stored as Comma-Separated Values in a plain text file. Although CSV is a very basic database format, it’s still a very useful format.
Let’s make a CSV database for all information that we recorded so far. We should start by creating a folder to store all files that our program needs to use or create:
System.IO.Directory.CreateDirectory("Data");
This instruction calls a function of the System module to create a new directory called Data, it looks longer than other instructions but what it does is pretty simple. The System module has a sub-module dedicated to reading and writing files, or bytes, and this sub-module is called IO. Inside this sub-module, there is another subordinate module called Directory which contains all functions dedicated to managing directories (or folders). One of those functions is CreateDirectory which does what the name suggests. We’ll find the new directory called "Data" inside the "...bin\Debug" folder (or "...bin\Release" folder) of our current Visual Studio project.
Now we should decide a name for the database. The database needs to be inside the Data folder:
string dbFileName = "Data\\Players.csv";
The "\" character is used to write special characters inside text quotes, to write the character itself we need to type it twice like above. After deciding a name, we can proceed to create our CSV database file:
System.IO.File.WriteAllText(dbFileName, "#;pLevel;pHeight;pName;pSurname;pRace;pClass;pBio;"+Environment.NewLine);
This instruction creates a new "Players.csv" file inside the Data folder. The file will contain a line of text that will be used for column titles. It will also contain an empty line. If a file with the same name already exists, it will be overwritten.
Although CSV stands for "Comma-Separated Values" format, we can use semicolons as well, the concept is the same. There will be times when the comma symbol may be needed for other uses than separating values, for example, commas can be used in a player’s biography.
It would be a good idea to keep track of how many lines there are in our database. We can use a simple int variable to remember the line count:
int allLines = 1;
CSV files can be easily viewed and edited with LibreOffice Calc:
Once that our database is created and ready to be used, we can add lines to it with a slightly different instruction:
System.IO.File.AppendAllText(@dbFileName, quot;001;{pLevel};{pHeight};{pName};{pSurname};{pRace};{pClass};{pBio}"+Environment.NewLine);
allLines++;
This will write the content of our variables on the empty line created before, everything always separated by a semicolon. It also adds a new empty line to the file and increases the value of "allLines" variable by one ("++" means to increase by 1). We can verify that our player data was written correctly using something like this:
Console.Write("What player profile do you want to check? (There is 1). Enter a number: ");
int lineToCheck = 0;
lineToCheck = Convert.ToInt32(Console.ReadLine());
string lineContent = System.IO.File.ReadLines(dbFileName).Skip(lineToCheck--).Take(1).First();
Console.WriteLine(quot;The content of the profile is: {lineContent}");
Console.ReadLine();
First, in this code, you’ll notice that we’re using "Convert.ToInt32" instead of "Convert.ToDouble" like last time, the reason is that this time we need the input from the user to be stored as an int type variable.
Second, we’re using a new feature of IO.File which is called ReadLines. This function allows us to read lines from a named file, in this case, the file name is fetched from the "dbFileName" variable that we created before. To get the content of a line we want to read, we have to use Skip, Take, and First. Since "Skip" and "Take" are features of the Linq sub-module of System, to make it work we have to add a new line to the code’s configuration, at the top of the code:
using System.Linq;
In our example, the function "Skip" is used to skip a number of lines equal to "lineToCheck" minus 1. The two minus characters "--" when used this way they mean to subtract 1, we do this because we don’t need to count the first line of the database file, that’s just the titles of the columns. By the way, Skip only accepts integers (that’s why we needed "ToInt32"). After Skip, we use "Take(1)" together with "First()" to get the immediate next line after the ones that were skipped. Take can be used to get multiple lines at once, in this case, using "(1)" makes Take get only one line. However, even if we used "(7)" or "(99)" the result would be the same because "First()" always gets the first line from Take. The only difference is that the memory usage would be higher, because Take still had to read all the 7 or 99 lines even if it wasn’t necessary.
In summary, what we have done so far:
Everything should be working properly. If you found problems or unclear points, message me and I will try to help. Next time we will use "if", "then", and "else" to make basic error handlers to prevent input and database mistakes.
Code used in this article: