Making SQL query from C++ - c++

I have connected to an MS Access database from my project. Now I want to make a query, for example to take out data from third and fourth column.
How can I write SQL query in C++/CPP, using visual studio 2008?

If you're using a framework like MFC, Qt, or wxWidgets, you probably want to use the database connection classes they already include.
If you're not using one of those, you probably want to look into another library specifically for the purpose, such as DTL or SOCI.
You can obviously write your own code to talk to the database via something like OLE DB or ODBC, but they both involve quite a lot of boiler-plate, so a library helps out a lot.

Related

Connect an SQL to c++ project in visual studio 2015

I'm writting a tic-tac-toe game in c++ and I want to create a hight scores view. The problem is I'm new to DB and now just theory of SQL.
So, how can I add a simple SQL table to my c++ project?
(id INTEGER PRIMARY KEY, name TEXT, hightScore INTEGER)
Looks like just adding to project SQL script file and creating this table not gonna work. How to connect it with my cpp files? Where and how I'll be able to make an INSERT after each new game was played?
Thanks in advance!
To have a simple table you'll need to have a database to set the table up within. The library you will use to communicate with the database is going to depend on which database you are running.
Setting up the needed libraries usually means installing them and updating your linker. This will require some research and effort on your part to make it work, depending on what your particular configuration is.
some ideas:
(for mysql there is mysql/mysql.h which is a free library. )
How to make #include <mysql.h> work?
(for other SQL databases (not free))
http://www.sqlapi.com/
(possible free alternative to sqlapi )
Free alternative to SQLAPI++?
Hopefully that will get you started in the right direction! Good luck.
I have researched about c++ database connectivity and i could find following ways to connect to SQL server database from c++ project.
Using Microsoft's ODBC connection method. Which is a little bit complicate method and requires thorough research to use further.
Ref: https://www.techhowtos.com/programming/how-to-connect-to-sql-server-from-visual-c-plus-plus/
Using SQLAP++ Library which is very convenient way to connect to a databse . It Requires to purchase one time license to use commercially.
Using CPPCMS project's CppDB library.

How to do an SQL query in C++ to define a variable

I've been searching all day to figure out how to do an SQL query in c++ and then store the result as a variable.
I am using Visual Studio 2012, connected to the SQL database, and ran the following query successfully inside Visual Studio:
SELECT Beta FROM Equity WHERE Ticker = 'AAPL'
I want to be able to run that query in my c++ program and then store the result in a variable so that it can be used in a calculation. How can this be done?
Thanks for the help
You will need to get an SQL library, depending on that you will need to write some set-up code and execute the query.
BY C++ I'm sure you mean a native C++ application, not a managed C++ one. You need to use one of libraries that connect to SQL Server. Choices are:
ODBC, see ODBC database sample
OLE DB, see Sample OLE DB Consumer Application
ADO, a programability extension on top of OLE DB. See ADO Code Examples in Visual C++
My recommendation would be to use ODBC. You have a quite a lot to read and learn before you can actually get an example running with the query you asked. Good luck.

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

Alternatives to writing an ODBC driver

We are storing allot of time series data into our own proprietary "database". In the next version of our system we want to give our users a simple query mechanism to extract the raw data from the database (as a complement to the reports our system can create) by using standard tools.
I have looked at the possibility to write an ODBC driver, but it looks like quite a daunting task, especially when the use will be very simple select statements.
I would be grateful for any tips, ideas and/or recommendation of libraries, that could make this task a bit simpler.
Our platform is Windows and our dev. env. is visual studio 2010 (nativ C++)
A LINQ provider.
It depends on what makes your customers happy. Easier than an writing ODBC driver would seem to provide a library function that can parse simple SQL selects and deliver the result in whatever form desired.
It might be interesting to take a look at OData, exposing your data as an OData producer, you would then be able to allow your users to query it right away using the already available consumers, e.g. Linqpad.