{"id":47,"date":"2013-02-25T16:56:01","date_gmt":"2013-02-25T08:56:01","guid":{"rendered":"http:\/\/fineyi.com\/?p=47"},"modified":"2013-02-25T16:56:01","modified_gmt":"2013-02-25T08:56:01","slug":"linux%e4%b8%8b%e7%94%a8c%e8%af%ad%e8%a8%80%e8%bf%9e%e6%8e%a5mysql%e5%ae%9e%e7%8e%b0%e5%a2%9e%e5%88%a0%e6%94%b9%e6%9f%a5","status":"publish","type":"post","link":"http:\/\/www.fineyi.com\/?p=47","title":{"rendered":"linux\u4e0b\u7528C\u8bed\u8a00\u8fde\u63a5mysql\u5b9e\u73b0\u589e\u5220\u6539\u67e5"},"content":{"rendered":"<p>\t\t\t\t\u516c\u53f8\u5185\u90e8\u57f9\u8bad\uff0c\u9886\u5bfc\u8981\u6c42\u5728linux\u73af\u5883\u4e0b\u7528c\u8bed\u8a00\u8fde\u63a5mysql\u6570\u636e\u5e93\uff0c\u5b9e\u73b0\u589e\u5220\u6539\u67e5\u529f\u80fd\uff0c\u505a\u4e00\u4e2ademo\u3002<\/p>\n<p>\u73af\u5883\uff1aRed Hat Enterprise Linux 4<\/p>\n<p>\u2460\u5b89\u88c5mysql<\/p>\n<p>\u2461\u521b\u5efa\u6570\u636e\u5e93\u8868\uff1abbb<\/p>\n<p>CREATE TABLE `bbb` (<br \/>\n`id` int(11) NOT NULL auto_increment,<br \/>\n`name2` varchar(20) default NULL,<br \/>\nPRIMARY KEY (`id`)<br \/>\n) ENGINE=MyISAM \u00a0DEFAULT CHARSET=utf8<\/p>\n<p>C\u7684\u4ee3\u7801\uff1a<\/p>\n<pre>#include \n#include \n#include \n#include  \n\/*\u5b9a\u4e49\u6570\u636e\u5e93\u8fde\u63a5\u9700\u8981\u7684\u5b8f*\/\n#define HOST \"localhost\"\n#define USERNAME \"root\"\n#define PASSWORD \"ictspace\"\n#define DATABASE \"test\"\n\n\/*\u6267\u884c\u4f20\u5165\u7684sql\u8a9e\u53e5*\/\nvoid exe_sql(char* sql) {\n\tMYSQL my_connection; \/*\u8fd9\u662f\u4e00\u4e2a\u6570\u636e\u5e93\u8fde\u63a5*\/\n\tint res; \/*\u6267\u884csql\u8a9e\u53e5\u540e\u7684\u8fd4\u56de\u6807\u5fd7*\/\n\t\/*\u521d\u59cb\u5316mysql\u8fde\u63a5my_connection*\/\n\tmysql_init(&amp;my_connection);\n\tif (mysql_real_connect(&amp;my_connection, HOST, USERNAME, PASSWORD,DATABASE, 0, NULL, 0)) {\n\t\t\/*\u8fde\u63a5\u6210\u529f*\/\n\t\tprintf(\"\u6570\u636e\u5e93\u6267\u884cexe_sql\u8fde\u63a5\u6210\u529f\uff01n\");\n\t\tmysql_query(&amp;my_connection, \"set names utf8\");\n\t\t\/*mysql_query\u51fd\u6570\u6267\u884c\u4f20\u5165\u7684sql\u8a9e\u53e5\uff0c\u8fd4\u56de\u4e00\u4e2aint\u503c\uff0c\u5982\u679c\u4e3a0\uff0c\u6267\u884c\u6210\u529f*\/\n\t\tres = mysql_query(&amp;my_connection, sql);\n\t\tif (res) {\/*\u6267\u884c\u5931\u8d25\u4e86*\/\n\t\t\tprintf(\"Error\uff1a mysql_query !\\n\");\n\t\t\tmysql_close(&amp;my_connection);\n\t\t} else {\/*\u6267\u884c\u6210\u529f\u4e86*\/\n\t\t\t\/*mysql_affected_rows\u8fd4\u56de\u6267\u884csql\u540e\u5f71\u54cd\u7684\u884c\u6570*\/\n\t\t\tprintf(\"%d \u884c\u53d7\u5230\u5f71\u54cd\uff01\\n\", mysql_affected_rows(&amp;my_connection));\n\t\t\tmysql_close(&amp;my_connection);\n\t\t}\n\t} else {\n\t\t\/*\u6570\u636e\u5e93\u8fde\u63a5\u5931\u8d25*\/\n\t\tprintf(\"\u6570\u636e\u5e93\u6267\u884cexe_sql\u8fde\u63a5\u5931\u8d25\uff01\\n\");\n\t}\n}\n\n\/*\u6267\u884c\u4f20\u5165\u7684sql\u8a9e\u53e5\uff0c\u5e76\u6253\u5370\u51fa\u67e5\u8be2\u7d50\u679c*\/\nvoid query_sql(char* sql) {\n\tMYSQL my_connection; \/*\u8fd9\u662f\u4e00\u4e2a\u6570\u636e\u5e93\u8fde\u63a5*\/\n\tint res; \/*\u6267\u884csql\u8a9e\u53e5\u540e\u7684\u8fd4\u56de\u6807\u5fd7*\/\n\tMYSQL_RES *res_ptr; \/*\u6307\u5411\u67e5\u8be2\u7ed3\u679c\u7684\u6307\u9488*\/\n\tMYSQL_FIELD *field; \/*\u5b57\u6bb5\u7ed3\u6784\u6307\u9488*\/\n\tMYSQL_ROW result_row; \/*\u6309\u884c\u8fd4\u56de\u7684\u67e5\u8be2\u4fe1\u606f*\/\n\tint row, column; \/*\u67e5\u8be2\u8fd4\u56de\u7684\u884c\u6570\u548c\u5217\u6570*\/\n\tint i, j; \/*\u53ea\u662f\u63a7\u5236\u5faa\u73af\u7684\u4e24\u4e2a\u53d8\u91cf*\/\n\t\/*\u521d\u59cb\u5316mysql\u8fde\u63a5my_connection*\/\n\tmysql_init(&amp;my_connection);\n\tif (mysql_real_connect(&amp;my_connection, HOST, USERNAME, PASSWORD,DATABASE, 0, NULL, 0)) {\/*\u8fde\u63a5\u6210\u529f*\/\n\t\tprintf(\"\u6570\u636e\u5e93\u67e5\u8be2query_sql\u8fde\u63a5\u6210\u529f\uff01\\n\");\n\t\tmysql_query(&amp;my_connection, \"set names utf8\");\n\t\t\/*\u6267\u884c\u4f20\u5165\u7684sql\u8a9e\u53e5\uff0c\u8fd4\u56de\u4e00\u4e2aint\u503c\uff0c\u5982\u679c\u4e3a0\uff0c\u8bc1\u660e\u8a9e\u53e5\u6267\u884c\u6210\u529f*\/\n\t\tres = mysql_query(&amp;my_connection, sql);\n\t\tif (res) { \/*\u6267\u884c\u5931\u8d25*\/\n\t\t\tprintf(\"Error\uff1a mysql_query !\\n\");\n\t\t\tmysql_close(&amp;my_connection);\n\t\t} else { \n\t\t\t\/*\u6267\u884c\u6210\u529f\u4e86*\/\n\t\t\t\/*\u5c06\u67e5\u8be2\u7684\u7d50\u679c\u7ed9res_ptr*\/\n\t\t\tres_ptr = mysql_store_result(&amp;my_connection);\n\t\t\t\/*\u5982\u679c\u7ed3\u679c\u4e0d\u4e3a\u7a7a\uff0c\u5c31\u628a\u7ed3\u679cprint*\/\n\t\t\tif (res_ptr) {\n\t\t\t\t\/*\u53d6\u5f97\u7d50\u679c\u7684\u884c\u6570\u548c*\/\n\t\t\t\tcolumn = mysql_num_fields(res_ptr);\n\t\t\t\trow = mysql_num_rows(res_ptr);\n\t\t\t\tprintf(\"\u67e5\u8be2\u5230 %lu \u884c \\n\", row);\n\t\t\t\t\/*\u8f93\u51fa\u7d50\u679c\u7684\u5b57\u6bb5\u540d*\/\n\t\t\t\tfor (i = 0; field = mysql_fetch_field(res_ptr); i++)\n\t\t\t\tprintf(\"%s\\t\", field-&gt;name);\n\t\t\t\tprintf(\"\\n\");\n\t\t\t\t\/*\u6309\u884c\u8f93\u51fa\u7d50\u679c*\/\n\t\t\t\tfor (i = 1; i &lt; row; i++) {\n\t\t\t\t\tresult_row = mysql_fetch_row(res_ptr);\n\t\t\t\t\tfor (j = 0; j &lt; column; j++)\n\t\t\t\t\tprintf(\"%s\\t\", result_row[j]);\n\t\t\t\t\tprintf(\"\\n\");\n\t\t\t\t}\n\t\t\t}\n\t\t\tmysql_close(&amp;my_connection);\n\t\t}\n\t}\n}\n\nint main(int argc, char *argv[]) {\n\tchar *query;\n\t\/*\u63d2\u5165\u64cd\u4f5c*\/\n\tchar *exe = \"insert into bbb values('','eee');\";\n\t\/*\u66f4\u65b0\u64cd\u4f5c*\/\n\t\/\/char *exe = \"update `bbb` set `name2` = 'zzz' where `id` = 10\";\n\t\/*\u5220\u9664\u64cd\u4f5c*\/\n\t\/\/char *exe = \"delete from bbb where `name2` = 'eee'\";\n\texe_sql(exe);\n\t\/*\u6d4b\u8bd5\u67e5\u8be2*\/\n\tquery=\"select * from bbb;\";\n\tquery_sql(query);\n\treturn 0;\n}<\/pre>\n<p>\u7f16\u8bd1\u547d\u4ee4\uff1a<br \/>\ngcc-o c_mysql_demo -g c_mysql_demo.c -I \/usr\/include\/mysql\/ -L \/usr\/lib\/ -l mysqlclient<\/p>\n<p>\u53c2\u6570\u8bf4\u660e\uff1a<\/p>\n<p>-I\uff08\u5927\u5199\u7684i\uff09 \u8868\u793a\u8981\u8fde\u63a5\u7684\u5934\u6587\u4ef6\u76ee\u5f55\uff0c\u56e0\u4e3a\u4f7f\u7528\u4e86&lt;mysql.h&gt; \uff0c-L\u8868\u793a\u8981\u8fde\u63a5\u7684\u5e93\u6587\u4ef6\u76ee\u5f55 -l\uff08\u5c0f\u5199\u7684L\uff09 \u8868\u793a\u8981\u8fde\u63a5\u7684\u5177\u4f53\u7684\u5e93\u540d\u79f0\uff0c\u5728\/usr\/lib\/ \u4e0b\uff0c\u6709\u53eb\u505alibmysqlclient.so\u7684\u5e93\uff0c\u6307\u5b9a\u5177\u4f53\u7684\u5e93\u7684\u540d\u5b57\u65f6\uff0c\u9ed8\u8ba4\u53bb\u6389\u5934\u5c3e\u7684lib\u548c.so\u76f4\u63a5\u4f7f\u7528\u4e2d\u95f4\u7684mysqlclient<br \/>\n\u5982\u679c\u4e0d\u7528-I -L \uff0c\u53ef\u80fd\u5bfc\u81f4\u627e\u4e0d\u5230\u5934\u6587\u4ef6 \u6216\u8005 \u51fd\u6570\u672a\u5b9a\u4e49\u7684\u9519\u8bef<br \/>\n<a href=\"http:\/\/fineyi.com\/wp-content\/uploads\/2013\/02\/by.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-55\" alt=\"by\" src=\"http:\/\/fineyi.com\/wp-content\/uploads\/2013\/02\/by.jpg\" width=\"866\" height=\"36\" srcset=\"http:\/\/www.fineyi.com\/wp-content\/uploads\/2013\/02\/by.jpg 866w, http:\/\/www.fineyi.com\/wp-content\/uploads\/2013\/02\/by-300x12.jpg 300w, http:\/\/www.fineyi.com\/wp-content\/uploads\/2013\/02\/by-768x32.jpg 768w\" sizes=\"auto, (max-width: 866px) 100vw, 866px\" \/><\/a><br \/>\n\u65e0\u62a5\u9519\uff0c\u7f16\u8bd1\u6210\u529f\u3002<\/p>\n<p>\u67e5\u770b\u8868\uff1a<br \/>\n<a href=\"http:\/\/fineyi.com\/wp-content\/uploads\/2013\/02\/select.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-56\" alt=\"select\" src=\"http:\/\/fineyi.com\/wp-content\/uploads\/2013\/02\/select.jpg\" width=\"218\" height=\"267\" \/><\/a><\/p>\n<p>\u6267\u884c\u7a0b\u5e8f\uff1a<br \/>\n<a href=\"http:\/\/fineyi.com\/wp-content\/uploads\/2013\/02\/exe.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-57\" alt=\"exe\" src=\"http:\/\/fineyi.com\/wp-content\/uploads\/2013\/02\/exe.jpg\" width=\"346\" height=\"241\" srcset=\"http:\/\/www.fineyi.com\/wp-content\/uploads\/2013\/02\/exe.jpg 346w, http:\/\/www.fineyi.com\/wp-content\/uploads\/2013\/02\/exe-300x209.jpg 300w\" sizes=\"auto, (max-width: 346px) 100vw, 346px\" \/><\/a><\/p>\n<p>\u518d\u6b21\u67e5\u770b\u8868bbb:<br \/>\n<a href=\"http:\/\/fineyi.com\/wp-content\/uploads\/2013\/02\/select_2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-58\" alt=\"select_2\" src=\"http:\/\/fineyi.com\/wp-content\/uploads\/2013\/02\/select_2.jpg\" width=\"217\" height=\"276\" \/><\/a><\/p>\n<p>\u6570\u636e\u63d2\u5165\u6210\u529f\uff01<\/p>\n<p>\u66f4\u65b0\u3001\u5220\u9664\u64cd\u4f5c\u7c7b\u4f3c\uff0c\u53ea\u9700\u6539\u4e00\u4e0bsql\u8bed\u53e5\u5373\u53ef\u3002\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u516c\u53f8\u5185\u90e8\u57f9\u8bad\uff0c\u9886\u5bfc\u8981\u6c42\u5728linux\u73af\u5883\u4e0b\u7528c\u8bed\u8a00\u8fde\u63a5mysql\u6570\u636e\u5e93\uff0c\u5b9e\u73b0\u589e\u5220\u6539 &hellip; <a href=\"http:\/\/www.fineyi.com\/?p=47\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a> <a href=\"http:\/\/www.fineyi.com\/?p=47\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"footnotes":""},"categories":[2,5,6],"tags":[18,30,35],"class_list":["post-47","post","type-post","status-publish","format-standard","hentry","category-cc","category-linux","category-mysql","tag-c","tag-linux","tag-mysql-2"],"aioseo_notices":[],"_links":{"self":[{"href":"http:\/\/www.fineyi.com\/index.php?rest_route=\/wp\/v2\/posts\/47","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.fineyi.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.fineyi.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.fineyi.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.fineyi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=47"}],"version-history":[{"count":0,"href":"http:\/\/www.fineyi.com\/index.php?rest_route=\/wp\/v2\/posts\/47\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.fineyi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=47"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.fineyi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=47"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.fineyi.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=47"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}