{"id":1097,"date":"2013-09-13T10:20:22","date_gmt":"2013-09-13T10:20:22","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=1097"},"modified":"2015-06-09T13:31:38","modified_gmt":"2015-06-09T13:31:38","slug":"setup-mysql-c-access","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/databases-programming-in-c\/mysql\/setup-mysql-c-access","title":{"rendered":"Setup MySQL C Access"},"content":{"rendered":"<p>\nSome of this is based on the excellent guide <a href=\"http:\/\/www.andrewdcox.com\/blog\/2013\/05\/26\/mysql-interactions-with-c-php-and-ajax-on-raspberry-pi-server\/\" target=\"_blank\">here<\/a>.\n<\/p>\n<h4>\nInstalling The MySQL C libraries<br \/>\n<\/h4>\n<p>\nMySQL comes with C-libraries:\n<\/p>\n<pre>\r\n<code>\r\nsudo apt-get update\r\nsudo apt-get upgrade\r\nsudo apt-get install libmysqlclient-dev\r\n<\/code><\/pre>\n<h4>\nUsing In A Geany Project<br \/>\n<\/h4>\n<p>\nInclude the library in your source files\n<\/p>\n<pre>\r\n<code>\r\n#include &lt;mysql\/mysql.h&gt;\r\n<\/code><\/pre>\n<p>\nAdd these to the LIBS and CFLAGS sections of your makefile\n<\/p>\n<pre>\r\n<code>\r\n`mysql_config --libs`\r\n\r\n`mysql_config --cflags`<\/code><\/pre>\n<h4>\nBeing Able To Compile Using&nbsp;NetBeans For Windows<br \/>\n<\/h4>\n<p>\nGo to: Project Properties &gt; Build &gt; Linker &gt; Compilation Line &gt; Additional Options &gt; paste in this:<br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;`mysql_config &#8211;cflags` `mysql_config &#8211;libs`\n<\/p>\n<p>\nNote the single quote marks are correct and should be used as is!\n<\/p>\n<h4>\nBeing Able To Compile Using Eclipse For Windows<br \/>\n<\/h4>\n<p>\nIf you are not coding on the RPi itself, for instance developing using eclipse for windows, you need to copy the mysql library files if your cross compiler toolchain doesn&#39;t have them.\n<\/p>\n<p>\nThe following is not based on any expert knowledge of ours, it is instead the results of our trial and error hitting it with a hammer until it worked&#8230;\n<\/p>\n<h5>\n1 &#8211; Get the header files<br \/>\n<\/h5>\n<p>\nCopy this directory on the RPi:\n<\/p>\n<pre>\r\n<code>\r\n\/usr\/include\/mysql\r\n<\/code><\/pre>\n<p>\nto your projects directory. &nbsp;Use this to include it in your C code file:\n<\/p>\n<pre>\r\n<code>\r\n#include &quot;mysql\/mysql.h&quot;\r\n<\/code><\/pre>\n<h5>\n2 &#8211; Create Object Files<br \/>\n<\/h5>\n<p>\nCreate a file called main.c containing some code which uses the mysql library, e.g. this:\n<\/p>\n<pre>\r\n<code>\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\n#include &lt;mysql.h&gt;\r\n\r\nint main(int argc, char **argv)\r\n{\r\n\tMYSQL *mysql1;\r\n\tmysql1 = mysql_init(NULL);\r\n}\r\n<\/code><\/pre>\n<p>\nSave it in a directory on the RPI, then on the command line go to that directory and use this to compile it:\n<\/p>\n<pre>\r\n<code>\r\ng++ main.c `mysql_config --cflags` `mysql_config --libs`\r\n<\/code><\/pre>\n<p>\nNow use this to find out the directory used:\n<\/p>\n<pre>\r\n<code>\r\nmysql_config --libs\r\n\r\n<\/code><\/pre>\n<p>\nFor us it gives this:\n<\/p>\n<p style=\"margin-left: 40px;\">\n-L\/usr\/lib\/arm-linux-gnueabihf -lmysqlclient -lpthread -lz -lm -lrt -ldl\n<\/p>\n<p>\nSo, from directory:\n<\/p>\n<p style=\"margin-left: 40px;\">\n\/usr\/lib\/arm-linux-gnueabihf\n<\/p>\n<p>\ncopy the files\n<\/p>\n<p style=\"margin-left: 40px;\">\nlibmysqlclient.a<br \/>\nlibmysqlclient.so<br \/>\n(there&#39;s also files&nbsp;libmysqlclient_r.a and&nbsp;libmysqlclient_r.so in there but we&#39;ve not bothered with them)\n<\/p>\n<p>\nPaste them into your windows machine folder:\n<\/p>\n<p style=\"margin-left: 40px;\">\nC:\\cygwin\\opt\\cross\\x-tools\\arm-unknown-linux-gnueabi\\arm-unknown-linux-gnueabi\\sysroot\\usr\\lib\\\n<\/p>\n<p>\nNow in eclipse, right click your project &gt; Properties &gt; C\/C++ Build &gt; Settings &gt; Cygwin C++ Linker &gt; Libraries. &nbsp;Add a new library entry:\n<\/p>\n<p style=\"margin-left: 40px;\">\nmysqlclient\n<\/p>\n<p>\nThis works but building the project fails because the library&nbsp;references other files which need to be added in also. &nbsp;For us files listed in the eclipse build warnings we&#39;re all in this folder:\n<\/p>\n<p style=\"margin-left: 40px;\">\n\/lib\/arm-linux-gnueabihf\/\n<\/p>\n<p>\nBut if you need to search install and use <a href=\"https:\/\/raspberry-projects.com\/pi\/command-line\/searching-for-files\" target=\"_blank\">mlocate to find them<\/a>&nbsp;on the RPi.\n<\/p>\n<p>\nFind each one and copy it into the same&nbsp;cygwin&nbsp;windows library folder. &nbsp;These are the files we needed:\n<\/p>\n<p style=\"margin-left: 40px;\">\nld-linux.so.3<br \/>\nld-linux-armhf.so.3<br \/>\nlibmysqlclient.a<br \/>\nlibmysqlclient.so<br \/>\nlibz.so.1\n<\/p>\n<p>\nThat&#39;s it, for us this worked fine under rasbian&nbsp;and we could access our mysql database no problems. &nbsp;I guess if the&nbsp;mysql library gets updated on the RPi then the files might need to be found and re-copied again so something to bear in mind if problems appear after updating the RPi ever.\n<\/p>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some of this is based on the excellent guide here. Installing The MySQL C libraries MySQL comes with C-libraries: sudo apt-get update sudo apt-get upgrade sudo apt-get install libmysqlclient-dev Using In A Geany Project Include the library in your source files #include &lt;mysql\/mysql.h&gt; Add these to the LIBS and CFLAGS sections of your makefile `mysql_config [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73],"tags":[],"class_list":["post-1097","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/1097","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=1097"}],"version-history":[{"count":22,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/1097\/revisions"}],"predecessor-version":[{"id":2026,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/1097\/revisions\/2026"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=1097"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=1097"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=1097"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}