{"id":4466,"date":"2023-09-21T05:15:49","date_gmt":"2023-09-21T05:15:49","guid":{"rendered":"https:\/\/truelogic.org\/wordpress\/?p=4466"},"modified":"2023-09-21T05:15:49","modified_gmt":"2023-09-21T05:15:49","slug":"working-with-mysql-in-vc","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2023\/09\/21\/working-with-mysql-in-vc\/","title":{"rendered":"Working with MySQL in VC++"},"content":{"rendered":"\n<p><strong>OVERVIEW<\/strong><\/p>\n\n\n\n<p>One of the chief advantages of using Mysql databases in VC++ is that it is incredibly fast. Since almost every application which uses MySQL uses a server language or a high-level language which goes through several layers of the software stack to work with MySQL, the resulting execution is not really fast. In fact, it is well known that database operations are among the slowest parts of any application. But since modern hardware is cheap and fast and so are network speeds, users and developers never really feel that the database is slow and in most cases, the speed is perfectly fine for most use-cases. But the first time you run an SQL query in C++, you find out how fast the database can actually be. Queries which would take a few seconds to run on a server, execute almost instantly when using VC++. <\/p>\n\n\n\n<p><strong>SETTING IT UP<\/strong><\/p>\n\n\n\n<p>As with all things related to C++ , setting up the whole MySQL Connector library is a little messy and it took me a couple of days to get it right, because there is so much conflicting info on the net regarding this. <\/p>\n\n\n\n<p>Download in the C++ connector library from<a href=\"https:\/\/dev.mysql.com\/downloads\/connector\/cpp\/\" target=\"_blank\" rel=\"noreferrer noopener\"> https:\/\/dev.mysql.com\/downloads\/connector\/cpp\/<\/a><\/p>\n\n\n\n<p>Choose the Windows (x86, 64-bit) ZIP Archive for Release builds or Windows (x86, 64-bit) ZIP Archive Debug Binaries for Debug builds. This post assumes that we are using the Debug version. The only difference between the two zip files is that the file and folders will be slightly different. The process will remain the same.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/MySQL-Download-Connector-C--940x429.png\" alt=\"\" class=\"wp-image-4469\" style=\"width:840px;height:383px\" width=\"840\" height=\"383\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/MySQL-Download-Connector-C--940x429.png 940w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/MySQL-Download-Connector-C--620x283.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/MySQL-Download-Connector-C--300x137.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/MySQL-Download-Connector-C--768x350.png 768w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/MySQL-Download-Connector-C-.png 1349w\" sizes=\"auto, (max-width: 840px) 100vw, 840px\" \/><\/figure>\n\n\n\n<p>Unzip the file into a folder. This will contain all the include,lib and DLL files required to build applications for MySQL. Assume the folder is <em>d:\\mysql-connector-c++-8.1.0-winx64<\/em>. You can have your own folder, Just use your own folder name wherever it is mentioned below.<\/p>\n\n\n\n<p>Now we will create an Empty C\/C++ project for Console. Create an empty file called main.cpp. We will add the code later. The most important thing is to set up the correct folders and libraries. Note that we will be setting up the MySQL Connector library for static linking and not dynamic linking. That means we will link with the lib files. But during runtime we still need the supporting DLL files. The content below assumes you are doing a Debug build. Just change the folders as required for a Release Build.<\/p>\n\n\n\n<p>Go to <strong>Project-&gt;Properties-&gt;VC++ Directories<\/strong> and add a new entry to <strong>Include Directories<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"791\" height=\"537\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/include1.png\" alt=\"\" class=\"wp-image-4473\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/include1.png 791w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/include1-620x421.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/include1-300x204.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/include1-768x521.png 768w\" sizes=\"auto, (max-width: 791px) 100vw, 791px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"734\" height=\"472\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/include2.png\" alt=\"\" class=\"wp-image-4474\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/include2.png 734w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/include2-620x399.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/include2-300x193.png 300w\" sizes=\"auto, (max-width: 734px) 100vw, 734px\" \/><\/figure>\n\n\n\n<p>Go to <strong>Project-&gt;Properties-&gt;C++-&gt;Preprocessor<\/strong> and add the following entry to <strong>Preprocessor Definitions<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"784\" height=\"534\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/pre1.png\" alt=\"\" class=\"wp-image-4476\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/pre1.png 784w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/pre1-620x422.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/pre1-300x204.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/pre1-768x523.png 768w\" sizes=\"auto, (max-width: 784px) 100vw, 784px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"771\" height=\"484\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/pre2.png\" alt=\"\" class=\"wp-image-4477\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/pre2.png 771w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/pre2-620x389.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/pre2-300x188.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/pre2-768x482.png 768w\" sizes=\"auto, (max-width: 771px) 100vw, 771px\" \/><\/figure>\n\n\n\n<p>In <strong>Project-&gt;Properties-&gt;C++-&gt;Code Generation <\/strong>make sure <strong>Runtime Library<\/strong> is set to (\/MDd) for Debug builds or (\/MD) for Release builds.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"786\" height=\"538\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/code.png\" alt=\"\" class=\"wp-image-4480\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/code.png 786w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/code-620x424.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/code-300x205.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/code-768x526.png 768w\" sizes=\"auto, (max-width: 786px) 100vw, 786px\" \/><\/figure>\n\n\n\n<p>Go to <strong>Project-&gt;Properties-&gt;Linker-&gt;General <\/strong>and add a new entry in <strong>Additional Library Directories<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"784\" height=\"547\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/general1.png\" alt=\"\" class=\"wp-image-4484\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/general1.png 784w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/general1-620x433.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/general1-300x209.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/general1-768x536.png 768w\" sizes=\"auto, (max-width: 784px) 100vw, 784px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"778\" height=\"513\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/general2.png\" alt=\"\" class=\"wp-image-4485\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/general2.png 778w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/general2-620x409.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/general2-300x198.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/general2-768x506.png 768w\" sizes=\"auto, (max-width: 778px) 100vw, 778px\" \/><\/figure>\n\n\n\n<p>Go to <strong>Project-&gt;Properties-&gt;Linker-&gt;Input <\/strong>and add a new entry in <strong>Additional Dependencies<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"785\" height=\"546\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/add1.png\" alt=\"\" class=\"wp-image-4487\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/add1.png 785w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/add1-620x431.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/add1-300x209.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/add1-768x534.png 768w\" sizes=\"auto, (max-width: 785px) 100vw, 785px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"768\" height=\"519\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/add2.png\" alt=\"\" class=\"wp-image-4488\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/add2.png 768w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/add2-620x419.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2023\/09\/add2-300x203.png 300w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/figure>\n\n\n\n<p><strong>TESTING WITH CODE<\/strong><\/p>\n\n\n\n<p>In main.cpp, use the sample code below. You will have to change the mysql server connection details and the sql query as required for your database. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdlib.h&gt;\n#include &lt;iostream&gt;\n\nusing namespace std;\n\n\n\n#include &lt;jdbc\/mysql_connection.h&gt;\n#include &lt;jdbc\/cppconn\/connection.h&gt;\n#include &lt;jdbc\/cppconn\/driver.h&gt;\n#include &lt;jdbc\/cppconn\/resultset.h&gt;\n#include &lt;jdbc\/cppconn\/statement.h&gt;\n#include &lt;jdbc\/cppconn\/prepared_statement.h&gt;\n\n\nconst std::string HOST = &quot;localhost&quot;;\nconst std::string DB = &quot;test-db&quot;;\nconst std::string USER = &quot;root&quot;;\nconst std::string PWD = &quot;password&quot;;\n\nint main(int argc, char** argv) {\n\t\n\tcout &lt;&lt; &quot;Starting..&quot; &lt;&lt; endl;\n\n\ttry {\n\t\tsql::Driver *driver = NULL;\n\t\tsql::Connection* conn = NULL;\n\t\tsql::Statement* stmt = NULL;\n\t\tsql::ResultSet* rst = NULL;\n\n\t\tdriver = get_driver_instance();\n\t\tstd::string connString = &quot;tcp:\/\/&quot; + HOST + &quot;:3306&quot;;\n\t\tconn = driver-&gt;connect(connString, USER, PWD);\n\t\tcout &lt;&lt; &quot;Database server connected&quot; &lt;&lt; endl;\n\n\t\tconn-&gt;setSchema(DB);\n\t\tstmt = conn-&gt;createStatement();\n\t\trst = stmt-&gt;executeQuery(&quot;select * from Graph&quot;);\n\t\twhile (rst-&gt;next()) {\n\t\t\tcout &lt;&lt; &quot;ID=&quot; &lt;&lt; rst-&gt;getInt(&quot;id&quot;) &lt;&lt; &quot;,&quot; &lt;&lt; rst-&gt;getString(&quot;name&quot;) &lt;&lt; endl;\n\t\t}\n\n\t\tdelete rst;\n\t\tdelete stmt;\n\t\tdelete conn;\n\t\tcout &lt;&lt; &quot;Closed connection&quot; &lt;&lt; endl;\n\t}\n\tcatch (sql::SQLException&amp; ex) {\n\t\tcout &lt;&lt; &quot;Error:&quot; &lt;&lt; ex.what() &lt;&lt; &quot; Code: &quot; &lt;&lt; ex.getErrorCode() &lt;&lt; endl;\n\n\t\treturn EXIT_FAILURE;\n\t}\n\tcout &lt;&lt; &quot;Ending&quot; &lt;&lt; endl;\n\n\treturn EXIT_SUCCESS;\n\n\n<\/pre><\/div>\n\n\n<p><strong>CONCLUSION<\/strong><\/p>\n\n\n\n<p>If everything is fine, the code will compile and run. You can find several code samples in <a href=\"https:\/\/dev.mysql.com\/doc\/connector-cpp\/1.1\/en\/connector-cpp-examples-results.html\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/dev.mysql.com\/doc\/connector-cpp\/1.1\/en\/connector-cpp-examples-results.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW One of the chief advantages of using Mysql databases in VC++ is that it is incredibly fast. Since almost every application which uses MySQL <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2023\/09\/21\/working-with-mysql-in-vc\/\" title=\"Working with MySQL in VC++\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":1087,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[302,278],"tags":[],"class_list":["post-4466","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cc","category-mysql-2"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4466","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=4466"}],"version-history":[{"count":24,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4466\/revisions"}],"predecessor-version":[{"id":4500,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4466\/revisions\/4500"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/1087"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=4466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=4466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=4466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}