Database in a C++ Program - c++

I've got a C++ program that needs to deal with a lot of typical database problems - looking at tables, inserting and deleting values, searching for records. All of the database information has to be stored locally. Let me emphasise that - I don't want to communicate with a server, I want the information to be stored on the user's computer.
Are there any libraries that can easily implement all this functionality, preferably in a SQL style syntax? Or what are some ways to easily and robustly implement this functionality?

You can use embedded DB.
I think SQLite is one of the more popular ones.

My personal preference would be SOCI, with a SQLite backend.
http://soci.sourceforge.net/
http://soci.sourceforge.net/doc/backends/sqlite3.html
http://www.sqlite.org/

Related

Design of data storage C++ application (maybe relational database)

I need to store and load some data in a C++ application. This data is basically going to end up as a set of tables as per a relational database.
I write the data to tables using something like csv format, then parse them myself and apply the database logic I need in my C++ code. But it seems stupid to reinvent the wheel with this and end up effectively writing my own database engine.
However, using something like a MySQL database seems like massive overkill for what is going to be a single user local system. I have tried setting up a MySQL daemon on my Windows system and I found it rather complex and possibly even impossible without admin privileges. It would be a serious obstacle to deployment as it would need each user's system to have MySQL set up and running.
Is there a reasonable middle ground solution? Something that can provide me with a simple database, accessible from C++, without all the complexities of setting up a full MySQL install?
NB. I have edited this question such that I hope it satisfies those who have voted to close the question. I am not asking for a recommendation for a tool, or someone's favourite tool or the best tools. That would be asking which database engine should I use. I am asking for what tools and design patterns are available to solve a specific programming problem - i.e. how can I get access to database like functionality from a C++ program, without writing my own database engine, nor setting up a full database server. This is conceptually no different to asking e.g. How do I print out the contents of a vector? - it's just a bigger problem. I have described the problem and what has been done so far to solve it. My understanding from the On Topic Page is that this is within scope.
You can try sqlite.
Here are some simple code examples: https://www.tutorialspoint.com/sqlite/sqlite_c_cpp.htm

Which are suitable local database (easy to handle with C++) for a small data set

The computer has already been installed ORACLE. But I didn't try ORACLE before, I just use sqlite...So, for now I want to create a database locally, and just insert one table. I want to use a test C++ program to read and write in this database. Is there something useful for using API about this? BTW, in the computer I saw SQL plus and SQL developer, but I didn't find the DBCA, which I know can be used to create table...help me, thanks a lot!!
BTW, my supervisor wants to me to test different kinds of database, which will be written in my report. So I want to test different kinds of databases, for the data is quite small, just 100 lines in a table is enough, but it will be applied in a big program, so I need to try different kinds of database locally, and not difficult for me to use C++ API...Because I just know little about database. I need some suggestions, thank a lot!
While I wouldn't recommend Oracle for handling small data sets, I do have a pointer to C++ API documentation.
http://www.oracle.com/pls/db112/portal.portal_db?selected=5&frame=
... and scroll to the bottom of the page for links to further information.
There's a choice of either using C++ with Oracle OCI API, or using C++ with Oracle Pro*C precompiler. The precompiler actually does produce code that uses the OCI API.
I have done some work with the Pro*C precompiler (using C as the implementation language, though, not C++), and it wasn't too bad. OCI tends to be quite low-level, but apparently writing direct OCI code has its uses, too.

How to create offline database to be used with c++

I saw already some questions with similar topic but my one is more of a beginner question.
I have already some experience with C++ and Microsoft Access but never came across how to actualy create or (if its not possible) than how to use an offline database similar to Access.
For example:
MS Access has its own tables etc... so I am about to build a program that will need offline database.
I think that to use simple txt files would not be the right way... What is my next option? Is there a way how all other programmers do it? Or would I just have to use one of the database providers like MySQL?
I think some people use SQLite for this purpose. I don't know that much about it, but you can learn more here:
http://www.sqlite.org/about.html
If you are on a Microsoft stack, then you may want to give a try to SQL Server Compact.
Alternatively, you may want to use SQLite; it is not so integrated in the Microsoft ecosystem, but it is Open Source, and is widely used.
You can use the ODBC driver and link it with your database source file. Just read this:
http://msdn.microsoft.com/en-us/library/ca6axakh%28v=vs.80%29.aspx
After setup your source use odbc api in your c++ code in order to make your queries. Also you can try to use ORM solutions in order to access your database.
http://en.wikipedia.org/wiki/List_of_object-relational_mapping_software
+1 for sqlite
If you're not actually interested in doing SQL-like queries, you could look at
Serialization (e.g. boost)
Boost Property Tree (focuses on structured config data, AFAICT)
LevelDB (highperformance key-value store by google)
Cf. also hints on memory-mapping standard containers here:
LevelDB vs. std::map

Interacting with a simple database in c++?

I'm making a game in c++ that will need to store and retrieve information about players (name, email, high score) etc. I thought of trying to just do it myself with XML but I think a real database (maybe SQL?) would do a better job since over time there may be thousands of users.
Are there libraries to do simple interactions with databases like queries, retrieving information, and storing information?
Thanks
Yes, SQLite will do exactly this. It stores the database as a local file, so if you want an online database server then this is perhaps not the best option.
It sounds like sqlite might be a good fit for you. It doesn't need a constantly running database server, and it does things smarter than hand-rolled xml-serialization.
Sqlite has documentation.

Sqlite as a replacement for fopen()?

On an official sqlite3 web page there is written that I should think about sqlite as a replacement of fopen() function.
What do you think about it? Is it always good solution to replece application internal data storage with sqlite? What are the pluses and the minuses of such solution?
Do you have some experience in it?
EDIT:
How about your experience? Is it easy to use? Was it painful or rather joyful? Do you like it?
It depends. There are some contra-indications:
for configuration files, use of plain text or XML is much easier to debug or to alter than using a relational database, even one as lightweight as SQLite.
tree structures are easier to describe using (for example) XML than by using relational tables
the SQLite API is quite badly documented - there are not enough examples, and the hyperlinking is poor. OTOH, the information is all there if you care to dig for it.
use of app-specific binary formats directly will be faster than storing same format as a BLOB in a database
database corruption can mean the los of all your data rather than that in a single bad file
OTOH, if your internal data fits in well with the relational model and if there is a a lot of it, I'd recommend SQLite - I use it myself for one of my projects.
Regarding experience - I use it, it works well and is easy to integrate with existing code. If the documentation were easier to navigate I'd give it 5 stars - as it is I'd give it four.
As always it depends, there are no "one size fits all" solutions
If you need to store data in a stand-alone file and you can take advantage of relational database capabilities of an SQL database than SQLite is great.
If your data is not a good fit for a relational model (hierarchical data for example) or you want your data to be humanly readable (config files) or you need to interoperate with another system than SQLite won't be very helpful and XML might be better.
If on the other hand you need to access the data from multiple programs or computers at the same time than again SQLite is not an optimal choice and you need a "real" database server (MS SQL, Oracle, MySQL, PosgreSQL ...).
The atomicity of SQLite is a plus. Knowing that if you half-way write some data(maybe crash in the middle), that it won't corrupt your data file. I normally accomplish something similar with xml config files by backing up the file on a successful load, and any future failed load(indicating corruption) automatically restores the last backup. Of course it's not as granular nor is it atomic, but it is sufficient for my desires.
I find SQLite a pleasure to work with, but I would not consider it a one-size-fits-all replacement for fopen().
As an example, I just wrote a piece of software that's downloading images from a web server and caching them locally. Storing them as individual files, I can watch them in Windows Explorer, which certainly has benefits. But I need to keep an index that maps between a URL and the image file in order to use the cache.
Storing them in a SQLite database, they all sit in one neat little file, and I can access them by URL (select imgdata from cache where url='http://foo.bar.jpg') with little effort.