2、.h>#include“mysql.h”intmain(intargc,char*argv[]){MYSQL*conn_ptr;conn_ptr=mysql_init(NULL);if(!conn_ptr){fprintf(stderr,“mysql_initfailed”);returnEXIT_FAILURE;}conn_ptr=mysql_real_connect(conn_ptr,“localhost”,“neusoft”,“neusoft”,“neusoftdb”,0,NULL,0);if(conn_ptr){
3、printf(“Connectionsuccess”);}else{printf(“Connectionfailed”);}mysql_close(conn_ptr);returnEXIT_SUCCESS;}编译命令为$gcc–I/usr/include/mysqlconnect1.c–lmysqlclient–oconnect1执行:$./connect1Connectionsuccess$2.反馈连接失败错误例程2:#include#include#include“mysql
4、.h”intmain(intargc,char*argv[]){MYSQLmy_connection;mysql_init(&my_connection);if(mysql_real_connect(&my_connection,“localhost”,“neusoft”,““,“neusoftdb”,0,NULL,0)){printf(“Connectionsuccess”);mysql_close(&my_connection);}else{fprintf(stderr,“Connectionfailed”);i
5、f(mysql_errno(&my_connection)){fprintf(stderr,“Connectionerror%d:%s”,mysql_errno(&my_connection),mysql_error(&my_connection));}}returnEXIT_SUCCESS;}执行:$./connect2ConnectionfiledConnectionerror1045:Accessdeniedforuser:‘neusoft@localhost’(Usingpassword:YES)$3.插入数据操
6、作#include#include#include“mysql.h”intmain(intargc,char*argv[]){MYSQLmy_connection;intres;mysql_init(&my_connection);if(mysql_real_connect(&my_connection,“localhost”,“neusoft”,“neusoft”,“neusoftdb”,0,NULL,0)){printf(“Connectionsuccess”);res=mysq
7、l_query(&my_connection,“INSERTINTOchildren(fname,age)VALUES(‘Ann’,3)”);if(!res){printf(“Inserted%lurows”,(unsignedlong)mysql_affected_rows(&my_connection));}else{fprintf(stderr,“Inserterror%d:%s”,mysql_errno(&my_connection),mysql_error(&my_connection));}mysql_c
8、lose(&my_connection);}else{fprintf(stderr,“Connectionfailed”);if(mysql_errno(&my_connection)){fprintf(stderr,“Connectionerror%d:%s”,mysql_err