Getting started with SQL express from a c++ console application - c++

Right now I am writing a little c++ console program using VS2010 Express. The next thing I want to do is to access a SQL database from within this program.
I've downloaded SQL server 2008 express. I've managed to set up a little db using the db gui.
My question now is, how do I access this db from within my program. Which header files do I need, how do I connect? About the basic tempering with the db itself I have found many tutorials, but this tiny bit that closes the gap between a program and the connection to the db drives me nuts... If any one has a nice tip to a tutorial or book, please let me know.
I've also tried to start a new project in VS, and was hoping to find some kind of setup wizard for a "sql project" that would get me one the way but did not find such a wiz...

Microsoft doesn't provide much for SQL connectivity from C++ console applications. There are a number of libraries such as SOCI and DTL that can help though.

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 create a database in Clion using C++?

I have a already created game and want to include a login system using a real database, no files. The game is written in C++ with SFML in Clion. I am also on Linux Mint.
I am new to this so I am wondering how to go about it. What kind of tools would i use? Hope this question is not to close asking for recommendation. I just don't know where to start.
Easiest way is to use SQLite. You can read about it here - https://www.sqlite.org/capi3ref.html
But if you want "real" SQL database, you can go with MySQL - http://dev.mysql.com/doc/refman/5.7/en/c-api-function-overview.html

C++ qt embedded mysql server

I'm using QT 5 with MingW 4.7 and i'm trying to start a program with an embedded mysql server.
My current program uses a mysql server, but the server has to be manually started (so out of the program). This isn't user friendly of-course.
I did some research and I need to use the libmysqld library . But I don't know how.
Can anyone give me an example how to use this?
You can use an embedded mysql server within Qt. There are no that many difficulties with that at all. If you could point any you faced I can help you with that, indeed you should link your application with libmysqld and then just start a mysql process within your application (there are plenty documentation available about that). But as was pointed in comments - are you sure you want to do that. There are many nicer embedded solutions available like SQLLite (included in Qt)...
Documentation and examples
You probably will need to have a look on mysql driver supplied with Qt and may be fork it for embedded version, but that needs a bit closer look. But again I don't see really issues with that

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.

Connecting to a SQL Server Compact Edition (.sdf) from an MFC application

I'm building an MFC app in Visual Studio 2008 which classifies textures and I need some sort of lightweight database to hold the characteristics (just some doubles and strings) which can be:
Carried around with the app on different computers
Be able to perform queries on it from the app (searches , updates ,inserts ,etc)
Currently I'm looking into SQL Server Compact Edition because it was very easy to create from Visual Studio (I also need only one table). But I;m having a hard time connecting and updating the database from C++.
This is what I've found on MSDN regarding C++ and SQLCE:
public:
void createSqlCeConnection(){
SqlCeConnection* myConnection = new SqlCeConnection();
myConnection->ConnectionString = "DataSource = blabla.sdf";
MessageBox::Show(String::Format( S"Connection State: {0}", __box(myConnection->State)));
}
Unfortunately my experience with .NET apps is pretty limited.
Hopefully you bright minds could tell me if I'm on the right path and what links and includes should I add for this to work with an C++ MFC projects.
For C++ applications, you're going to want to use the OLE DB Provider for SQL CE. For example, take a look here for a code snippet on initializing a Session (you might have to explicitly click the C++ tab in the Examples section).