linux-mysql 連線1
阿新 • • 發佈:2018-12-30
//simple_mysql_api.c #include <stdlib.h> #include <stdio.h> #include <mysql.h> int main(int argc, char *argv[]){ MYSQL* conn_ptr; conn_ptr = mysql_init(NULL); if(!conn_ptr){ fprintf(stderr, "mysql init failed"); return EXIT_FAILURE; } const char* host = "localhost"; const char* user = "root"; const char* password = "pswd"; const char* dbname = "chat"; conn_ptr = mysql_real_connect(conn_ptr,host, user, password,dbname,0,NULL,0); if(conn_ptr){ printf("Connect mysql succeed!\n"); }else{ printf("Connect mysql failed!\n"); } mysql_close(conn_ptr); return 0; }
編譯:
$ g++ -o mysql-test simple_mysql_api.c -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient
$./mysql-test