setup environment:
https://www.digitalocean.com/community/tutorials/a-basic-mysql-tutorial
construct database xxx
and set table potluck
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <mysql/mysql.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 5 int main() { 6 MYSQL *conn; 7 MYSQL_RES *res; 8 MYSQL_ROW row; 9 10 char *server = "localhost"; 11 char *user = "root"; 12 char *password = "******"; 13 char *database = "test"; 14 15 conn = mysql_init(NULL); 16 17 /* Connect to database */ 18 if (!mysql_real_connect(conn, server, 19 user, password, database, 0, NULL, 0)) { 20 fprintf(stderr, "%s ", mysql_error(conn)); 21 exit(1); 22 } 23 24 /* send SQL query */ 25 if (mysql_query(conn, "show tables")) { 26 fprintf(stderr, "%s ", mysql_error(conn)); 27 exit(1); 28 } 29 30 res = mysql_use_result(conn); 31 32 /* output table name */ 33 printf("MySQL Tables in mysql database: "); 34 while ((row = mysql_fetch_row(res)) != NULL) 35 printf("%s ", row[0]); 36 37 /* close connection */ 38 mysql_free_result(res); 39 mysql_close(conn); 40 41 return 0; 42 }
gcc test.c -o test `mysql_config --include --libs`
(my_env) moonx@moonx:mysql$ ldd test
linux-vdso.so.1 => (0x00007ffc2379b000)
libmysqlclient.so.20 => /usr/lib/x86_64-linux-gnu/libmysqlclient.so.20 (0x00007f5ce9ee7000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f5ce9b1d000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f5ce9903000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f5ce96ff000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f5ce94e2000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f5ce9160000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f5ce8e57000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f5ce8c41000)
/lib64/ld-linux-x86-64.so.2 (0x00007f5cea4f8000)
(my_env) moonx@moonx:mysql$ ./test
MySQL Tables in mysql database:
potluck
https://dev.mysql.com/doc/refman/5.6/en/c-api-function-overview.html