dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 31

Thread: Working with a database

  1. #1
    Join Date
    Jun 2010
    Posts
    17

    Working with a database

    Firstly, forgive me - I'm pretty much a noob at C++, and only began re-learning it last week.

    I'm trying to make a simple game (mostly as practice). I want to access a database so that I can read the definition of an item directly from there. If given the unique ID of an item (short int), I need to be able to use that to look up various statistics about the item, without having to load the entire list of items into the memory.

    Thing is, I only need to read from the database - I don't need to write anything to it, so maybe there's a way other than databases that I could do this? My original idea was to use an XML file, but I figured a database might be better.

    I'm using Microsoft visual C++ 2010 as my IDE/compiler. I know how to work with classes, arrays, structs, and other basics.

    Any advice or links would be much appreciated. Thanks in advance!

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,555

    Re: Working with a database

    Depends on what database you're using, but you may find the CRecordset class useful.

  3. #3
    Join Date
    Jun 2010
    Posts
    17

    Re: Working with a database

    How would I go about using CRecordset?

  4. #4
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Working with a database

    here you go.

  5. #5
    Join Date
    Jun 2010
    Posts
    17

    Re: Working with a database

    Mkay, well knowing how to access a database is fine and dandy, but how do I create a database? Everywhere I look, the program is built almost entirely for use with a server, but I don't need that. I need something simple.

    Or, here's a better question. Since I don't intend to actually write anything to the database via the program, and I don't need online connectivity, am I better off just defining the items right in the program? I didn't want to initially, because for one, isn't that really costly for memory? For two, that hurts modularity, and I'd like to be able to change item definitions without having to re-compile the whole program.

    What do you guys think is the best way?
    Last edited by candlemaster; June 12th, 2010 at 08:32 PM. Reason: added paragraph

  6. #6
    Join Date
    Nov 2007
    Posts
    613

    Re: Working with a database

    In order to use databases you need a database engine or a full database server. There are some for free.
    Engines: MS Access Jet engine, MSDE.
    Full servers: MS SQL 2005 Express Edition, MySql.

    It's very unusual for a game to install a database engine on a player's computer. No matter how complex is a game it cannot be that complex to make a database engine a vital necessity.

    My suggestion is to use arrays of structures or, for very sophisticated situations, even arrays of class instances.

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,171

    Re: Working with a database

    If you want to use a database and don't want to have to install anything, consider using the MS Access (Jet) db.

    One way to read from it is to use the ATL OLEDB consumer classes. You create the database using Access, but you don't need to have Access installed on the user's machines.

    By default Access files are .mdb but you can rename these to anything you wish.


    __________________________________________________________
    Arjay

    See my latest series on using WCF to communicate between a Windows Service and WPF task bar application.
    Tray Notify - Part I Tray Notify - Part II

    Need a little help with Win32 thread synchronization? Check out the following CG articles and posts:
    Sharing a thread safe std::queue between threads w/progress bar updating
    Simple Thread: Part I Simple Thread: Part II
    Win32 Thread Synchronization, Part I: Overview Win32 Thread Synchronization, Part 2: Helper Classes

    www.iridyn.com


  8. #8
    Join Date
    Jun 2010
    Posts
    17

    Re: Working with a database

    Basically, the inventory I have right now is set up like this:

    Code:
    invItem inventory[30];
    invItem is a struct, defined thusly:
    Code:
    struct invItem
    {
    unsigned short ID;
    char Quantity; //Yes, the maximum you're allowed to carry is 255.  This is intentional.
    };
    Thus, the inventory is composed entirely of ID's. Using said ID, I need to be able to look up additional information about the item, such as it's name and other properties.

    I'm not picky about how the item's definitions are stored. Ideally, they'll be in their own file so that I can look up items without having to load the entire list into memory. I'm not picky about what kind of file they're stored in, or how that file is accessed.

  9. #9
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,602

    Re: Working with a database

    I need to be able to use that to look up various statistics about the item, without having to load the entire list of items into the memory.

    Thing is, I only need to read from the database - I don't need to write anything to it
    Typically "statistics" is implied to be collected somehow, and therefore, updated time to time.

    Besides, I can see "having to load the entire list into memory" thing, twice. I dare say, database access components may use quite a lot of memory. So the question is: are you sure about this kind of "saving"? A golden rule says: do not optimize prematurely.
    Best regards,
    Igor

  10. #10
    Join Date
    Jun 2010
    Posts
    17

    Re: Working with a database

    Quote Originally Posted by Igor Vartanov View Post
    So the question is: are you sure about this kind of "saving"?
    No, no I'm not. That's why I'm here lol.

    So, if I were to do everything in the executable, I *think* that my best bet would be to save the entire item in the array, instead of the item's ID? Or is there still a way that I can use a numeric ID to look up information?
    Last edited by candlemaster; June 13th, 2010 at 05:33 PM.

  11. #11
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,602

    Re: Working with a database

    Array or hash map would do. In case the number of items is less than a thousand, the map would look more attractive, as for me.
    Best regards,
    Igor

  12. #12
    Join Date
    Jun 2010
    Posts
    17

    Re: Working with a database

    Aaaah, I see. So, I could make a really huge array, store every item definition in there, and use the index as the item ID. Makes sense.

    Still open to ideas though, if anybody has a better idea. I just don't want my small, practice game to end up with a minimum requirement of 512mb of ram.

  13. #13
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Working with a database

    I could make a really huge array
    You could, but arrays aren't dynamic. This means they don't grow if you need them to. Allocating a huge array takes memory you don't know if you need it, and, if you need more, you need to reallocate the entire array. You don't want that. If you don't know how much data there is (or will be), a hashmap is a better idea.

  14. #14
    Join Date
    Jun 2010
    Posts
    17

    Re: Working with a database

    Well, right now I'm defining the array at the same time as defining the items. I'm just adding 1 to the array size every time I make another item, so it's only as large as I need it.

    I don't plan to create new item definitions on the fly, so I don't really need it to be dynamic.

    Edit: Alright, I just managed to test out some sizes. The size of a single item (so far) is 72 bytes. If we have 1,000 items, that means that the game will use up about 72kb worth of ram, just for allowing the item list to exist. I guess this isn't too bad, really. If we use a full list, that's still only about 4mb. This can work.
    Last edited by candlemaster; June 13th, 2010 at 08:03 PM.

  15. #15
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Working with a database

    Quote Originally Posted by candlemaster View Post
    Well, right now I'm defining the array at the same time as defining the items. I'm just adding 1 to the array size every time I make another item, so it's only as large as I need it.
    Arrays are not resizable. So what are you really doing?

    Regards,

    Paul McKenzie

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)