A very simplistic database

Hello I was wondering about how one should go about making a small database (name, lastname, date of birth for example) with visual studio c++? One which would store the given data in a .txt file, and retrieve it the next time the program is launched. As well as sorting everything in the list in alphabetical order... It'd be nice to just have the basics if it isn't too hard for someone, as I am completely lost right now other than the fact it's probably best to use struct
Last edited on
closed account (48bpfSEw)
ever used : https://www.sqlite.org/
?


https://www.sqlite.org/quickstart.html

Last edited on
The thing is I want to learn how to make such a database... not just copy the code... thats boring
closed account (48bpfSEw)
An idea about a database structure is a book!

A book has a title, table of contents, pages, sections, sentences, words, letters...
and a glossar, a word-index... and some more features. All the things you need to handle the content of this little "database".

How would you order this topics?
Which one is important than the other one?

How can you connect things together (relations)?


Beside of these considerations: Think also about effizience! Storing a block of data is faster than store every single word.

And how about the integrity? What if the data base is corrupt? How can it repair itself? What about multi-user-access? The concepts about Server-Client-Technologie comes up...
And many many other thoughts about this challenging topic!


good luck and I would like see your results here!


PS : Yes, it's boring to copy, but on the other hand: you'll lern a lot if you copy again and again. You have first learn the rules before you can break them! And think about the song of the Beatles "Hey Jude, take a sad song and make it better!!!" ^^


addendum: storing data in a fix structure is not flexible enought! you have to think abstract or in a way of meta!
Last edited on
I know I can learn from copying... but I'm... not very good at learning haha :D Most of the stuff I already know is from copying over and over... but I have to do it in small bits
closed account (48bpfSEw)

Okay after all this theorism blah blah an example for you:

The simplest modell I found to define any type of structure is this:

1
2
3
4
5
6
7
8
9
10
11
class CLASS {
  int iID;
  string strName;
}

class CLASS_CONNECTION {
  int iClassConnectionID;
  int iClassIDFrom
  int iClassIDTo;
  string strName;
}


Examples of CLASS: book, page, section, sentence. (and many more...)

Example of CLASS_CONNECTIONS: book has pages, page has sections, section has sentences, ...


This was the definition of a database!


Now the content:

1
2
3
4
5
6
7
8
9
10
11
class OBJECT {
  int iClassID;
  int iID;  // ObjectID

  string strData;
}

class OBJECT_CONNECTION {
  int iClassConnectionID;
  int iObjectConnectionID;
}


Examples of Objects:


book : "The book about C++"

page : "page 1", "page 2", "page 3" etc.

section : "section 1.1", "section 1.2" etc.

sentence: "this is the first sentence of the first section of the first page of the book c++"
etc.


Now connecting this objects together with there IDs (I do it now for a better understanding with the content):

"the book about c++" has these pages "page 1", "page 2", "page 3" ...
"page 1" has the sections "section 1.1", "section 1.2" etc.
"sentence "this is the first..." has the parent "section 1.1"

etc.


Now when all this done then you have defined the database "the book about c++" with all its pages, sections and sentences.

The query is as simple as A,B,C: you can filter about all CLASSES. To search within the CLASS "sentence" is a bit hairy but possible.

If you would break down the sentence into its words you could create more connectiontypes to other CLASSES! e.g. "referencing to other words, sections etc", "marking points" etc.



Do you want to store the whole book or just the infos like author, title, price etc. ?
I just want a pc-side program which takes information such as above listen author, title, price. And saves them in a .txt file sorted by author alphabetically for example. Then the next time the program is run, I want it to read the file, tell the user how many different books there already are, and potentially add any new ones, putting them in their place, once again sorted alphabetically.
What have you learned so far ?
For a project like this I would recommend to define a class Book and a class BookCollection. You need to know the basics about streams, classes and some stl containers.
How many books do you want to store ?
I have no idea what books are.... I know about streams and some(i would like to think quite a few) class, and never even heard of stl containers.... not to point fingers but my teacher really sucks >.>
closed account (48bpfSEw)
Do you know what a keyboard is? It's an object of the real world. A book too. I can't believ that you don't know what a book is! ^^

That what I was talking about was a demonstration how to pick up an object of the real world and transfer it into a structur that can be used in a programm.

The common databases has of cause the typicly structur of a table:

CLASSES: records, columns

CLASS_CONNECTIONS: record has columns
closed account (48bpfSEw)
http://www.cplusplus.com/doc/tutorial/files/
I know most of those things(now I know that fstream is a thing though), the problem I struggle most would be sorting the list alphabetically, I could manage it with numbers but I want to learn alphabetical aswell.

P.S
I swear I thought a book was some sort of a thing in c++ :DD
Still can't seem to make anything work... How exactly do I make the program read the .txt file, as well as adding additional information into the file without corrupting the old info? And how do I sort alphabetically?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
#include <set>
#include <string>
#include <sstream>
#include <fstream>

struct person
{
    // let us keep it simple for now
    std::string first_name ; // invariant: no embedded spaces eg. "Bjarne"
    std::string last_name ; // invariant: no embedded spaces eg. "Stroustrup"
    std::string date_of_birth ; // invariant: no embedded spaces eg. "30-December-1950"
};

// write the information to an output stream as a single line of text
// first_name<space>last_name<space>date_of_birth
std::ostream& put_person( std::ostream& stm, const person& per )
{ return stm << per.first_name << ' ' << per.last_name << ' ' << per.date_of_birth ; }

// same as above, overloaded stream insertion operator
std::ostream& operator<< ( std::ostream& stm, const person& per )
{ return put_person( stm, per ) ; }

// read the information written with put_person from an input stream into per
std::istream& get_person( std::istream& stm, person& per )
{
    std::string line ;
    if( std::getline( stm >> std::ws, line ) ) // if a non-empty line of text was read successfully
    {
        std::istringstream strstm(line) ; // create a stream that reads from the line of text

        // if first_name<space>last_name<space>date_of_birth<end_of_string> was successfully read, we are done
        if( strstm >> per.first_name >> per.last_name >> per.date_of_birth ) return stm ;
    }

    // handle input failure
    stm.clear( stm.failbit ) ; // put the stream into a failed state
    return stm ; // and return it
}

// same as above, overloaded stream insertion operator
std::istream& operator>> ( std::istream& stm, person& per )
{ return get_person( stm, per ) ; }

// make the type person LessThanComparable
// keep it simple for now: compare only last_name, case-sensitive
bool operator< ( const person& a, const person& b ) { return a.last_name < b.last_name ; }

int main()
{
    const std::string file_name = "heroes.txt" ;

    {
        // create a std::set of persons
        // std::set an associative container that contains a sorted set of unique objects
        //          the default comparison function assumes that the type is LessThanComparable (uses the < operator)
        // http://en.cppreference.com/w/cpp/container/set
        std::set<person> heroes =
        {
            { "Donald","Knuth", "January-10-1938" },
            { "Alan", "Kay", "May-17-1940" },
            { "Dennis", "Ritchie", "September-9-1941" },
            { "Ken", "Thompson", "February-4-1943" },
            { "Alan", "Kay", "May-17-1940" },
            { "Alex", "Stepanov", "November-16-1950" },
        };

       std::ofstream file(file_name) ; // open a file for writing
       // http://www.stroustrup.com/C++11FAQ.html#for
       for( const auto& hero : heroes ) file << hero << '\n' ; // write each person to the file
    }

    {
       std::set<person> heroes ; // create an empty set of persons

       {
           std::ifstream file(file_name) ; // open the file for reading
           person a_hero ;
           while( file >> a_hero ) heroes.insert(a_hero) ; // insert each person read from the file into the set
       }

       // add stroustrup to the set of persons
       // http://en.cppreference.com/w/cpp/container/set/insert
       heroes.insert( { "Bjarne", "Stroustrup", "30-December-1950" } ) ;

       // query on (case-sensitive) last name "Ritchie"
       // http://en.cppreference.com/w/cpp/container/set/find
       const auto iter = heroes.find( { "", "Ritchie", "" } ) ; // specify last name
       if( iter != heroes.end() ) // if found, print last and first name, date of birth
            std::cout << "found: " << iter->last_name << ", " << iter->first_name << " (born " << iter->date_of_birth << ")\n" ;

       // print out the list of persons, sorted on last name
       int n = 0 ;
       std::cout << "\n----------\nheroes\n--------------\n" ;
       for( const auto& hero : heroes ) std::cout << ++n << ". " << hero << '\n' ;

       // write the updated list back to the file
       {
           std::ofstream file(file_name) ; // open a file for writing
           for( const auto& hero : heroes ) file << hero << '\n' ; // write each person to the file
       }

       // print out the contents of the updated file
       std::cout << "\n\n----------\ncontents of file '" << file_name << "'\n--------------\n\n"
                 << std::ifstream(file_name).rdbuf() ;
    }
}

http://coliru.stacked-crooked.com/a/317bf5f10d49f840
Holy... that's... a lot of information to process.... thanks a lot man
Topic archived. No new replies allowed.