{"id":323,"date":"2011-04-21T21:56:37","date_gmt":"2011-04-21T21:56:37","guid":{"rendered":"http:\/\/www.hakancakiroglu.com\/wordpress\/?p=323"},"modified":"2011-04-25T07:08:21","modified_gmt":"2011-04-25T07:08:21","slug":"linuxunix-uzerinde-c-ile-mysqle-baglanma","status":"publish","type":"post","link":"https:\/\/www.hakancakiroglu.com\/wordpress\/?p=323","title":{"rendered":"Linux\/Unix \u00dczerinde C ile Mysql&#8217;e Ba\u011flanma"},"content":{"rendered":"<p>Linux\/Unix \u00fczerinde C programlama dilini kullanarak Mysql&#8217;e ba\u011flanan bir program yazmak ve bu program\u0131 derlemek istiyorsan\u0131z \u00f6ncelikle a\u015fa\u011f\u0131daki paketlerin sistemde y\u00fckl\u00fc ve yap\u0131land\u0131r\u0131lm\u0131\u015f olmas\u0131 gereklidir.<\/p>\n<ul>\n<li><strong>mysql<\/strong> : MySQL binary ve k\u00fct\u00fcphaneleri<\/li>\n<li><strong>mysqlclient<\/strong> : MySQL client taraf\u0131 i\u00e7in gerekli binary ve k\u00fct\u00fcphaneler<!--more--><\/li>\n<li><strong>mysql-devel<\/strong>: Development i\u00e7in kullan\u0131lan paket (mysql_config i\u00e7in \u015fart)<\/li>\n<li><strong>mysql-server<\/strong>: MySQL sunucunun kendisi (Bu \u00f6rnekte client-server ayn\u0131 makina \u00fczerinde oldu\u011fu i\u00e7in)<\/li>\n<li><strong>gcc, make and other development libs<\/strong>: GNU C Derleyici<\/li>\n<\/ul>\n<p>Yukar\u0131daki paketleri Centos \u00fczerinde yum ile, Ubuntu \u00fczerinde Synaptic ile y\u00fckleyebilirsiniz.<\/p>\n<p>E\u011fer Ubuntu \u00fczerinde mysql_config binarysini bulam\u0131yorsan\u0131z a\u015fa\u011f\u0131daki komutlar ile son mysql-devel paketini bularak y\u00fcklemelesiniz.<\/p>\n<pre>hakan@hakan-laptop:~$ sudo aptitude search libmysqlclient\r\ni\u00a0\u00a0 libmysqlclient<strong>16<\/strong>\r\n<\/pre>\n<p>Yukar\u0131daki \u00e7\u0131kt\u0131y\u0131 ald\u0131ktan sonra libmyclientxx \u00f6rne\u011findeki xx bilgisini alarak a\u015fa\u011f\u0131daki komut \u00e7al\u0131\u015ft\u0131r\u0131lmal\u0131.<\/p>\n<pre>hakan@hakan-laptop:~$ sudo apt-get install libmysqlclient16-dev\r\n<\/pre>\n<p>Evet bu durumda mysql_config binarysi y\u00fcklenmi\u015f olmal\u0131.<\/p>\n<p>&nbsp;<\/p>\n<p>A\u015fa\u011f\u0131da verdi\u011fim \u00f6rnek kodu mysql.c olarak kaydedin.<\/p>\n<pre><tt><em><span style=\"color: #9a1900;\">\/* Simple C program that connects to MySQL Database server*\/<\/span><\/em>\r\n<strong><span style=\"color: #000080;\">#include<\/span><\/strong> <span style=\"color: #ff0000;\">&lt;mysql.h&gt;<\/span>\r\n<strong><span style=\"color: #000080;\">#include<\/span><\/strong> <span style=\"color: #ff0000;\">&lt;stdio.h&gt;<\/span>\r\n\r\n<strong><span style=\"color: #000000;\">main<\/span><\/strong><span style=\"color: #990000;\">()<\/span> <span style=\"color: #ff0000;\">{<\/span>\r\n   MYSQL <span style=\"color: #990000;\">*<\/span>conn<span style=\"color: #990000;\">;<\/span>\r\n   MYSQL_RES <span style=\"color: #990000;\">*<\/span>res<span style=\"color: #990000;\">;<\/span>\r\n   MYSQL_ROW row<span style=\"color: #990000;\">;<\/span>\r\n\r\n   <span style=\"color: #009900;\">char<\/span> <span style=\"color: #990000;\">*<\/span>server <span style=\"color: #990000;\">=<\/span> <span style=\"color: #ff0000;\">\"localhost\"<\/span><span style=\"color: #990000;\">;<\/span>\r\n   <span style=\"color: #009900;\">char<\/span> <span style=\"color: #990000;\">*<\/span>user <span style=\"color: #990000;\">=<\/span> <span style=\"color: #ff0000;\">\"root\"<\/span><span style=\"color: #990000;\">;<\/span>\r\n   <span style=\"color: #009900;\">char<\/span> <span style=\"color: #990000;\">*<\/span>password <span style=\"color: #990000;\">=<\/span> <span style=\"color: #ff0000;\">\"PASSWORD\"<\/span><span style=\"color: #990000;\">;<\/span> <em><span style=\"color: #9a1900;\">\/* yazd\u0131g\u0131n\u0131z kullan\u0131c\u0131 i\u00e7in \u015fifre yaz\u0131n *\/<\/span><\/em>\r\n   <span style=\"color: #009900;\">char<\/span> <span style=\"color: #990000;\">*<\/span>database <span style=\"color: #990000;\">=<\/span> <span style=\"color: #ff0000;\">\"mysql\"<\/span><span style=\"color: #990000;\">;<\/span>\r\n\r\n   conn <span style=\"color: #990000;\">=<\/span> <strong><span style=\"color: #000000;\">mysql_init<\/span><\/strong><span style=\"color: #990000;\">(<\/span>NULL<span style=\"color: #990000;\">);<\/span>\r\n\r\n   <em><span style=\"color: #9a1900;\">\/* Connect to database *\/<\/span><\/em>\r\n   <strong><span style=\"color: #0000ff;\">if<\/span><\/strong> <span style=\"color: #990000;\">(!<\/span><strong><span style=\"color: #000000;\">mysql_real_connect<\/span><\/strong><span style=\"color: #990000;\">(<\/span>conn<span style=\"color: #990000;\">,<\/span> server<span style=\"color: #990000;\">,<\/span>\r\n         user<span style=\"color: #990000;\">,<\/span> password<span style=\"color: #990000;\">,<\/span> database<span style=\"color: #990000;\">,<\/span> <span style=\"color: #993399;\">0<\/span><span style=\"color: #990000;\">,<\/span> NULL<span style=\"color: #990000;\">,<\/span> <span style=\"color: #993399;\">0<\/span><span style=\"color: #990000;\">))<\/span> <span style=\"color: #ff0000;\">{<\/span>\r\n      <strong><span style=\"color: #000000;\">fprintf<\/span><\/strong><span style=\"color: #990000;\">(<\/span>stderr<span style=\"color: #990000;\">,<\/span> <span style=\"color: #ff0000;\">\"%s<\/span><span style=\"color: #cc33cc;\">\\n<\/span><span style=\"color: #ff0000;\">\"<\/span><span style=\"color: #990000;\">,<\/span> <strong><span style=\"color: #000000;\">mysql_error<\/span><\/strong><span style=\"color: #990000;\">(<\/span>conn<span style=\"color: #990000;\">));<\/span>\r\n      <strong><span style=\"color: #000000;\">exit<\/span><\/strong><span style=\"color: #990000;\">(<\/span><span style=\"color: #993399;\">1<\/span><span style=\"color: #990000;\">);<\/span>\r\n   <span style=\"color: #ff0000;\">}<\/span>\r\n\r\n   <em><span style=\"color: #9a1900;\">\/* send SQL query *\/<\/span><\/em>\r\n   <strong><span style=\"color: #0000ff;\">if<\/span><\/strong> <span style=\"color: #990000;\">(<\/span><strong><span style=\"color: #000000;\">mysql_query<\/span><\/strong><span style=\"color: #990000;\">(<\/span>conn<span style=\"color: #990000;\">,<\/span> <span style=\"color: #ff0000;\">\"show tables\"<\/span><span style=\"color: #990000;\">))<\/span> <span style=\"color: #ff0000;\">{<\/span>\r\n      <strong><span style=\"color: #000000;\">fprintf<\/span><\/strong><span style=\"color: #990000;\">(<\/span>stderr<span style=\"color: #990000;\">,<\/span> <span style=\"color: #ff0000;\">\"%s<\/span><span style=\"color: #cc33cc;\">\\n<\/span><span style=\"color: #ff0000;\">\"<\/span><span style=\"color: #990000;\">,<\/span> <strong><span style=\"color: #000000;\">mysql_error<\/span><\/strong><span style=\"color: #990000;\">(<\/span>conn<span style=\"color: #990000;\">));<\/span>\r\n      <strong><span style=\"color: #000000;\">exit<\/span><\/strong><span style=\"color: #990000;\">(<\/span><span style=\"color: #993399;\">1<\/span><span style=\"color: #990000;\">);<\/span>\r\n   <span style=\"color: #ff0000;\">}<\/span>\r\n\r\n   res <span style=\"color: #990000;\">=<\/span> <strong><span style=\"color: #000000;\">mysql_use_result<\/span><\/strong><span style=\"color: #990000;\">(<\/span>conn<span style=\"color: #990000;\">);<\/span>\r\n\r\n   <em><span style=\"color: #9a1900;\">\/* output table name *\/<\/span><\/em>\r\n   <strong><span style=\"color: #000000;\">printf<\/span><\/strong><span style=\"color: #990000;\">(<\/span><span style=\"color: #ff0000;\">\"MySQL Tables in mysql database:<\/span><span style=\"color: #cc33cc;\">\\n<\/span><span style=\"color: #ff0000;\">\"<\/span><span style=\"color: #990000;\">);<\/span>\r\n   <strong><span style=\"color: #0000ff;\">while<\/span><\/strong> <span style=\"color: #990000;\">((<\/span>row <span style=\"color: #990000;\">=<\/span> <strong><span style=\"color: #000000;\">mysql_fetch_row<\/span><\/strong><span style=\"color: #990000;\">(<\/span>res<span style=\"color: #990000;\">))<\/span> <span style=\"color: #990000;\">!=<\/span> NULL<span style=\"color: #990000;\">)<\/span>\r\n      <strong><span style=\"color: #000000;\">printf<\/span><\/strong><span style=\"color: #990000;\">(<\/span><span style=\"color: #ff0000;\">\"%s <\/span><span style=\"color: #cc33cc;\">\\n<\/span><span style=\"color: #ff0000;\">\"<\/span><span style=\"color: #990000;\">,<\/span> row<span style=\"color: #990000;\">[<\/span><span style=\"color: #993399;\">0<\/span><span style=\"color: #990000;\">]);<\/span>\r\n\r\n   <em><span style=\"color: #9a1900;\">\/* close connection *\/<\/span><\/em>\r\n   <strong><span style=\"color: #000000;\">mysql_free_result<\/span><\/strong><span style=\"color: #990000;\">(<\/span>res<span style=\"color: #990000;\">);<\/span>\r\n   <strong><span style=\"color: #000000;\">mysql_close<\/span><\/strong><span style=\"color: #990000;\">(<\/span>conn<span style=\"color: #990000;\">);<\/span>\r\n<span style=\"color: #ff0000;\">}<\/span>\r\n<\/tt><\/pre>\n<p>Art\u0131k mysql.c dosyas\u0131n\u0131 derleyebiliriz. Bunun i\u00e7in sisteminize \u00f6zel olarak mysqlin hangi library ve include dosya<br \/>\nlar\u0131n\u0131 kullanaca\u011f\u0131n\u0131 belirlemek i\u00e7in bize yard\u0131mc\u0131 olmas\u0131 a\u00e7\u0131s\u0131ndan mysql_config program\u0131ndan yararlanaca\u011f\u0131z.<\/p>\n<p>A\u015fa\u011f\u0131daki komut ile mysql apisinin derleme a\u015famas\u0131nda sizin sisteminiz \u00fczerinde hangi librarylere ihtiyac\u0131 oldu\u011funu belirleyebilirsiniz.<\/p>\n<pre>hakan@hakan-laptop:~$<code> mysql_config --libs<\/code>\r\n-L\/usr\/lib64\/mysql -lmysqlclient -lz -lcrypt -lnsl -lm -L\/usr\/lib64 -lssl -lcrypto\r\n<\/pre>\n<p>Ayn\u0131 \u015fekilde a\u015fa\u011f\u0131daki komut ile mysql apisini derleyebilmek  i\u00e7in gerekli include dosyalar\u0131n\u0131 tespit edebilirsiniz<\/p>\n<pre>hakan@hakan-laptop:~<code>$ mysql_config --cflags<\/code>\r\n-I\/usr\/include\/mysql -g -pipe -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 \\\r\n-D_LARGEFILE_SOURCE -fno-strict-aliasing\r\n<\/pre>\n<p>Evet gereken k\u00fct\u00fcphane ve include dosyalar\u0131n\u0131 bildi\u011fimize g\u00f6re derleme komutunu verebiliriz.<\/p>\n<pre>hakan@hakan:~<code><strong>$<\/strong><strong> gcc  -o mysql $(mysql_config --cflags) mysql.c  $(mysql_config --libs)\r\n<\/strong><\/code>mysql.c: In function \u2018mysql\u2019:\r\nmysql.c:23: warning: incompatible implicit declaration of built-in function \u2018exit\u2019\r\nmysql.c:33: warning: incompatible implicit declaration of built-in function \u2018exit\u2019\r\n<\/pre>\n<p>Bu durumda e\u011fer yukar\u0131daki uyar\u0131 mesajlar\u0131ndan ba\u015fka mesaj almad\u0131ysan\u0131z mysql ad\u0131nda bir binary olu\u015fmu\u015f olmal\u0131. Olu\u015fan binary b\u00fcy\u00fck ihtimalle +x hakk\u0131 ile olu\u015facakt\u0131r fakat her ihtimale kar\u015f\u0131 izinlerine +x verelim ve olu\u015fan binaryimizi \u00e7al\u0131\u015ft\u0131ral\u0131m.<\/p>\n<pre>hakan@hakan:~$ chmod +x mysql\r\nhakan@hakan:~$ .\/mysql\r\nMySQL Tables in mysql database:\r\ncolumns_priv\r\ndb\r\nfunc\r\nhelp_category\r\nhelp_keyword\r\nhelp_relation\r\nhelp_topic\r\nhost\r\ntables_priv\r\ntime_zone\r\ntime_zone_leap_second\r\ntime_zone_name\r\ntime_zone_transition\r\ntime_zone_transition_type\r\nuser \r\n\r\n<code><strong>\u00a0<\/strong><strong>\u00a0<\/strong><\/code><\/pre>\n<p>Evet C ile yazd\u0131\u011f\u0131m\u0131z program\u0131m\u0131z mysql ile haberle\u015ferek bize \u00e7\u0131kt\u0131 \u00fcretti. \u00d6rnek kodun i\u00e7erisindeki query \u00fczerinde de\u011fi\u015fikilk yaparak istedi\u011finiz bir ama\u00e7 i\u00e7in program yazabilirsiniz.<\/p>\n<p>Umar\u0131m faydal\u0131 bir payla\u015f\u0131m olmu\u015ftur, yeni bir yaz\u0131da g\u00f6r\u00fc\u015fmek dile\u011fiyle&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linux\/Unix \u00fczerinde C programlama dilini kullanarak Mysql&#8217;e ba\u011flanan bir program yazmak ve bu program\u0131 derlemek istiyorsan\u0131z \u00f6ncelikle a\u015fa\u011f\u0131daki paketlerin sistemde y\u00fckl\u00fc ve yap\u0131land\u0131r\u0131lm\u0131\u015f olmas\u0131 gereklidir. mysql : MySQL binary ve k\u00fct\u00fcphaneleri mysqlclient : MySQL client taraf\u0131 i\u00e7in gerekli binary ve k\u00fct\u00fcphaneler<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,15],"tags":[],"_links":{"self":[{"href":"https:\/\/www.hakancakiroglu.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/323"}],"collection":[{"href":"https:\/\/www.hakancakiroglu.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hakancakiroglu.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hakancakiroglu.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hakancakiroglu.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=323"}],"version-history":[{"count":0,"href":"https:\/\/www.hakancakiroglu.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/323\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.hakancakiroglu.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hakancakiroglu.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hakancakiroglu.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}