开发者

How to convert a C++ structure into a database

开发者 https://www.devze.com 2023-02-21 00:47 出处:网络
I have a software which use differents structures for its configuration. Each time i save the configuration the structures are dump into a binary file and each time i load the config the files are rea

I have a software which use differents structures for its configuration. Each time i save the configuration the structures are dump into a binary file and each time i load the config the files are read and structures are populated with the data.

For some reason i would like to change this and use a database for storing the software's configuration. Unfortunately i dont really know how to d开发者_开发百科esign the table.

Let's say i have the following structure :

typedef struct StructUser {
    BYTE readAccess;    
    BYTE writeAccess;
    BYTE passLength;
    BYTE nameLength;
    char pass[32];  
    char name[32];
}

How would you build the database for having the best flexibility (adding or removing some elements) and the best performance (quick fetch of the configuration) ?

I thought at first to a table per structure with many columns as elements in the structure but it's not seems very easy to maintain.

Thanks


There are quite some things you need to take into account.

First of all, do you want your database to have a readable structure or not? Or can you live with quite unreadable data in the database, but better suited for mapping to your structure?

  • If you want a readable database structure (preferably in a 3th or 4th normal form) give your database tables columns that correspond to the logical fields in your structure. In many cases this means one column per structure field, but in some cases you might want to map multiple fields into one column, or split one field over multiple columns. Consider using an ORM (Object-Relational Mapping) tool to help you with it (e.g. for Java you have Hibernate, for C/C++ there are probably similar tools).
  • If you can live with unreadable data in your database, you could serialize your structure into one string, and save this string in the database. When reading from the database, just read the string and deserialize it. Your tables then only need one column, which is this serialized string.

Although the serialized-string-approach might give you a quicker initial solution, and may make it handier for changing your structure without changing the database, you probably run into problems in the longer term. Having a clear database structure means that also other tools can make use of your database (e.g. RAD tools like Access). Therefore consider investigating in an ORM tool.

Second, your application probably needs to save the data to database as well. But how are you going to do that?

  • Will you empty your database and refill it (just like you recreate a file)?
  • Will you selectively update records in the database (but then you need a key in your table)?

You probably want multiple applications or users to store their configuration in the database. Therefore, emptying the database and refilling it isn't probably an option (although there are tricks to solve that, like storing the username in a second column, and cleaning everything of that user). Therefore, you need to store the original database key together with your structure, so that you know which records you need to update later. Again, ORM tools can help you with that.

Going from file to database may seem simple for you, but it isn't. Consider beforehand what you want to do with your database, and foresee enough time for designing your database and writing the code to access the database.

0

精彩评论

暂无评论...
验证码 换一张
取 消