[C++] Should i use a database?

Need help with programming? Feel free to ask anything. Answers are welcome as well!
  • Author
  • Message
Offline
User avatar

chickendude

Staff Member

Topic Starter

[C++] Should i use a database?

Post29 October 2011, 22:19

In the other post that is in this subforum (:P) i mentioned that i was going to try my hand at a language learning application in C++ (using Qt to handle all the hard stuff). Today i finally started coding the actual framework, well basically the buttons and getting some tabs to work, and i've gotten to the point where i need to finally store data. The program i'm writing is based off another program written in PHP which uses MySQL to handle all the data. I'm pretty comfortable with MySQL and wouldn't mind using it, but i'm not sure if it would be very fast and if maybe it'd be better to try to organize my data in another way.

Basically, you have a text in, let's say, Spanish. You open it up and it shows the following:
"Hola. ¿Cómo estás? Estoy bien, gracias, ¿y tú? Estoy bien."
You click on the words (or phrases) you don't know and add those words into the database along with their definition and a few other details. The definition to words you've already entered appear when hovering on top of that word.

Do you think i should bother with a database or just go with a tab-separated text file?
Offline
User avatar

NanoWar

Site Admin

Re: [C++] Should i use a database?

Post29 October 2011, 23:34

Would you want to install MYSQL in order to use your application? I don't think so. Start with the easiest things first, I would say. XML maybe?
Offline
User avatar

chickendude

Staff Member

Topic Starter

Re: [C++] Should i use a database?

Post30 October 2011, 00:57

I thought of that later, i guess 'cuz i've already got it installed here it didn't cross my mind. I've never actually used XML, would i have to write an interpreter to handle it? Reading through the Qt documentation it's surprising what is covered, it's even got a web engine built in! I'm reading up now on ways Qt can handle data, something called MVC architecture?
http://doc.trolltech.com/4.7/qstandarditemmodel.html
Offline
User avatar

NanoWar

Site Admin

Re: [C++] Should i use a database?

Post30 October 2011, 01:06

I liked MVVM: http://en.wikipedia.org/wiki/Model_View_ViewModel, it's the only one I used at a larger scale.
Offline
User avatar

Buckeye Dude

Staff Member

Re: [C++] Should i use a database?

Post30 October 2011, 09:36

I think you're overcomplicating your problem, first have you actually implemented anything in your code to handle this data. If you haven't you can simply define a class to store the require data, and then create a Serialize method in side there that deals with persisting it some how. You can then keep a list (if you're storing a lot then a hashtable) of all the words you need and on exiting call a persist function that iterates through that list and saves them somehow. As nano suggested XML is good, almost everything has built in readers and writers and human readable is very good. If not, you can do some sort of binary representation, maybe just write all your values to a file in order you iterate through.

The reason the original code used a database was because databases are very good of keeping track of relational data, which when the server handles all the data there can be a lot of. When its being stored per user (ie a desktop app), the database adds a lot of uncessary overhead
Offline
User avatar

chickendude

Staff Member

Topic Starter

Re: [C++] Should i use a database?

Post30 October 2011, 12:12

I'm sure i'm overcomplicating things (and if i could, i'd much rather write the program in z80 asm ;)). I've got a simple class set up to store the data and a form set up to input that data. When i create a new instance of that class (ie. add a new "text") should i create the text file in the "prototype" (constructor?)? That is, initialise the variables of the class and store them to the file in one fell swoop? Structuring C++ programs seems like it'll take a while for me to get used to. You kind of lost me in the rest of your post. I'll have to look up what a "serialize method" and "hashtable" are, maybe something in Qt will serve my purpose on that end.

As for a database, i figured it'd probably be a bit overkill. I don't know if you've ever used Qt, but i've currently got the information flowing nicely into my Text class:
Text.h
Code: Select all
class Text : public QMainWindow
{
      Q_OBJECT

      public:
            Text(QLineEdit *language, QLineEdit *title,QTextEdit *text,QLineEdit *tags,QLineEdit *audio);

      private slots:

      private:
//text properties
            QLineEdit *language;
            QLineEdit *title;
            QTextEdit *text;
            QLineEdit *tags;
            QLineEdit *audio;
};

Text.cpp
Code: Select all
Text::Text(QLineEdit *language, QLineEdit *title, QTextEdit *text, QLineEdit *tags, QLineEdit *audio)
{
      QMessageBox::information(this, "Langage et titre du texte", language->displayText() + title->displayText());
}


...calling it something like this: "Text newText(language,title,text,tags,audio);"

So from here, i'm thinking i should create a separate method to store the text into a file, and another to retrieve the text from a file. Also, it seems that everyone uses pointers and i'm not sure why/what advantage that provides over passing by reference.

C++ is fun, but way different from everything i've done before. In asm i always wrote all my own routines, so having the majority of things in C++ already written for me is a bit weird.
Offline
User avatar

chickendude

Staff Member

Topic Starter

Re: [C++] Should i use a database?

Post30 October 2011, 14:09

Here's the text file the program generates:
Code: Select all
<text>
  <language>English</language>
  <title>Lorem ipsum</title>
  <text>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</text>
  <tags>Lorem</tags
  <audio>ipsum</audio>
</text>


Now i've gotta figure some way of handling the data. I've been reading a bit online but it might be good practice just trying it on my own.
And a little ss just because:
screenshot.jpg
screenshot.jpg (65.54 KiB) Viewed 9936 times
Offline
User avatar

Buckeye Dude

Staff Member

Re: [C++] Should i use a database?

Post30 October 2011, 22:10

OK so seeing what you have is looking pretty good. Theres one thing I would change about it, and thats get rid of the dependency on QT inside the actual text class itself. You'll want to get in the habit of coding so that your interface code and your backend code (all the stuff that you need to run to make your program a program) are separated. For this project it probably doesn't matter, but its a good habit to be in. For each of those you're really just storing a string, so store the string and then if necessary, create another class that has a Text member and contains all the QT class members.
As for saving stuff to a text file in the constructor I would advise against it. Two reasons, writing to disk is a time consuming operation and you dont need to waste time doing it in your constructor. Two it doesn't need to be there. You can do this at any time while your program is running, no reason to do it immediately. As I suggested in my first post, I'd save them all to one file when the user exits the program.

Some clarifications on the last part of my last post. A hashtable is like an large lookup table, allowing things to be looked up in constant time. This is great if you have a lot of data and don't want to spend a lot of time iterating through an array or linked list looking for it. The serialize method would just be something like this:
Code: Select all
void Serialize(XML *writer) {
    writer->write(text);
    writer->write(title);
    writer->write(whatever);
}
 

And have one of these methods on each text class. Now the user has requested to quit the program you can loop through your big list of Text classes and on each one, pass them a writer and say write your data here. They write their data and then return and when you've called it on each one of them, you've got a text file with all the stuff written to it nicely. The advantage of having a Serialize method like this, is the class itself takes care of saving what it needs to save. So you could have 20 different types of text classes, and each one takes care of writing their own data rather than you having to figure out which one needs to save which member.

So from here, i'm thinking i should create a separate method to store the text into a file, and another to retrieve the text from a file. Also, it seems that everyone uses pointers and i'm not sure why/what advantage that provides over passing by reference.

Pointers are essentially passing by reference (in C thats the only way to do so), if you pass by reference the compiler will convert it to a pointer for you anyway. One of the most important things about pointer is for allocating dynamic memory. If you need to persist a text class, your call
Code: Select all
Text newText(language,title,text,tags,audio);

would create the item on the local stack frame, meaning it would only be accessible to whatever created it and any functions that method calls. To create an object that can be accessed from any method you'd have to do
Code: Select all
Text *newText = new Text(language, title, text, tags, audio);

Now you have a pointer to the new memory allocated for newText, and whenever you pass that pointer anywhere it is always refering to the correct memory location. Another plus for pointers is if you do something like
Code: Select all
//parent is just a parent class that contains a Text class
Text newText = parent->textClass;
 

then the structure textClass will be copied to newText. I was dinged on this when doing some Wabbitemu stuff the other day, I was copying a class when all I really needed what a reference to it, but the reverse is true to. You can use a pointer to refer to the same class in memory, or use a local var to create a copy of it which you can manipulate.

Let me know if any of that makes sense, I feel like I'm rambling now :/
Offline
User avatar

chickendude

Staff Member

Topic Starter

Re: [C++] Should i use a database?

Post03 December 2011, 17:43

Ok, it's been quite a while and i haven't really touched this, but i've decided it's time to get my feet wet again and see if i can figure out how to move about in all this C++ mess. It seems like pretty much all the difficult work is already done for me by Qt, i just need to figure out how to take advantage of it. It's even got an XML parser. Initially, i thought of saving every text individually, but now i'm not sure if one larger XML file would be better. I've just started work on opening files, once i get that sorted through it'll be time to actually do something with the texts i suppose.

I just reread through everything you said, and thanks a lot for explaining it all to me, Buckeye Dude. Right now, a lot of it just doesn't make sense, i should look for some more tutorials that talk about more than different types of loops, data types, etc.

EDIT: Wow, more than a month! Now that i'm going to be moving a bit more slowly with Monopoly, i'll probably be splitting my time 50/50 between these two projects.
Offline
User avatar

chickendude

Staff Member

Topic Starter

Re: [C++] Should i use a database?

Post04 December 2011, 17:01

So i got opening a text (and parsing the XML) done, now i don't really know what i need to start on. I guess i need to figure out a way to break down a text into individual words and be able to display info about each one. The Qt Editor (the actual program) basically does everything i'd like my program to do, so that makes me think that with Qt everything i want to do should be a lot easier if i can figure out how to do it. I found a couple Qt tutorials for beginners online, i really need to get better with the C++ structure, classes, vectors, pointers/references, etc. is all sort of blowing my mind at the moment.
Attachments
lwt1.jpg
add/open text
lwt1.jpg (59.19 KiB) Viewed 9879 times
Offline
User avatar

NanoWar

Site Admin

Re: [C++] Should i use a database?

Post04 December 2011, 17:09

Yeah, QT is a blast. I tried it once, and for a C++ environment it looked really suitable. I didn't manage to code anything meaningful, since I'm just not so much into C++.

XML is probably the way to go. Remember you can add stuff like <text language="english"> and so on.
Offline
User avatar

chickendude

Staff Member

Topic Starter

Re: [C++] Should i use a database?

Post05 December 2011, 09:36

Yeah, it's really pretty cool and handles a lot that i would otherwise have no idea how to do. And is cross platform. Now i'm a little unsure how to structure everything, should i store all the texts in one big file? Or each text as a separate file (like it is now). Also, the wordlist with definitions, etc. And i have no real idea how to go about breaking up the text into individual words that display information about themselves (a little pop-up or something). I'll have to look into this later, probably the weekend.

XML looks pretty, and now that i've got it parsing nicely, it should be here to stay :)

Return to Programming Help

Who is online

Users browsing this forum: No registered users and 0 guests

cron