#include <mysql/mysql.h>
#include <stdio.h>
#include <stdlib.h>
int main () {
MYSQL *conn;
Mysql_res *res;
Mysql_row ROW;
char *server = "localhost";
Char *user = "root";
char *password = "[email protected] $RFVyl";
Char *database = "Mymysql";
conn = Mysql_init (NULL);
/* Connect to Database */
if (!mysql_real_connect (conn, server,
User, password, database, 0, NULL, 0)) {
fprintf (stderr, "%s\n", MYSQL_ERROR (conn));
Exit (1);
}
/* Send SQL Query */
if (mysql_query (conn, "Show Tables")) {
fprintf (stderr, "%s\n", MYSQL_ERROR (conn));
Exit (1);
}
res = MYSQL_USE_RESULT (conn);
/* Output Table name */
printf ("MySQL Tables in MySQL database:\n");
while (row = mysql_fetch_row (res)) = NULL)
printf ("%s \ n", row[0]);
/* Close Connection */
Mysql_free_result (RES);
Mysql_close (conn);
return 0;
}
GCC ' mysql_config--cflags ' mysql-1.c ' mysql_config--libs '-O test
[Email protected]:~/projects/test$ ldd Test
Linux-vdso.so.1 = (0x00007ffcc1111000)
libmysqlclient.so.20 =/usr/lib/x86_64-linux-gnu/libmysqlclient.so.20 (0x00007f2e8a3a9000)
libc.so.6 =/lib/x86_64-linux-gnu/libc.so.6 (0x00007f2e89fdf000)
Libz.so.1 =/lib/x86_64-linux-gnu/libz.so.1 (0x00007f2e89dc4000)
libdl.so.2 =/lib/x86_64-linux-gnu/libdl.so.2 (0x00007f2e89bc0000)
libpthread.so.0 =/lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f2e899a3000)
libstdc++.so.6 =/usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f2e89620000)
libm.so.6 =/lib/x86_64-linux-gnu/libm.so.6 (0x00007f2e89317000)
Libgcc_s.so.1 =/lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f2e89101000)
/lib64/ld-linux-x86-64.so.2 (0x000055dcebb91000)
[Email protected]:~/projects/test$./test
MySQL Tables in MySQL database:
Data
Tx
Wallet
This article from the "write poetry with C + +" blog, declined reprint!
MySQL C API programming (i)