C連接Mysql數(shù)據(jù)庫(kù)代碼
更新時(shí)間:2013年11月12日 16:46:45 作者:
使用C語(yǔ)言連接Mysql數(shù)據(jù)庫(kù)的方法,大家可以看看
復(fù)制代碼 代碼如下:
//vc工具中添加E:\WAMP\BIN\MYSQL\MYSQL5.5.8\LIB 路徑
//在工程設(shè)置-》鏈接》庫(kù)模塊中添加 libmysql.lib
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <winsock.h>
#include "E:\wamp\bin\mysql\mysql5.5.8\include\mysql.h"
void main(){
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server ="localhost";
char *user ="root";
char *password="";
char *database="test";
char sql[1024]="select * from chinaren";
conn=mysql_init(NULL);
if(!mysql_real_connect(conn,server,user,password,database,0,NULL,0)){
fprintf(stderr,"%s\n",mysql_error(conn));
exit(1);
}
if(mysql_query(conn,sql)){
fprintf(stderr,"%s\n",mysql_error(conn));
exit(1);
}
res=mysql_use_result(conn);
while((row = mysql_fetch_row(res))!=NULL){
printf("%s\n",row[2]);
}
mysql_free_result(res);
mysql_close(conn);
}
//在工程設(shè)置-》鏈接》庫(kù)模塊中添加 libmysql.lib
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <winsock.h>
#include "E:\wamp\bin\mysql\mysql5.5.8\include\mysql.h"
void main(){
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server ="localhost";
char *user ="root";
char *password="";
char *database="test";
char sql[1024]="select * from chinaren";
conn=mysql_init(NULL);
if(!mysql_real_connect(conn,server,user,password,database,0,NULL,0)){
fprintf(stderr,"%s\n",mysql_error(conn));
exit(1);
}
if(mysql_query(conn,sql)){
fprintf(stderr,"%s\n",mysql_error(conn));
exit(1);
}
res=mysql_use_result(conn);
while((row = mysql_fetch_row(res))!=NULL){
printf("%s\n",row[2]);
}
mysql_free_result(res);
mysql_close(conn);
}
相關(guān)文章
C語(yǔ)言利用cJSON解析JSON格式全過(guò)程
cJSON是用于解析json格式字符串的一套api,非常好用,下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言利用cJSON解析JSON格式的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04解析C語(yǔ)言中空指針、空指針常量、NULL & 0的詳解
本篇文章是對(duì)C語(yǔ)言中空指針、空指針常量、NULL & 0 進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C++之關(guān)于string對(duì)象的大小比較
這篇文章主要介紹了C++之關(guān)于string對(duì)象的大小比較方式,具有很好的 參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11c++報(bào)錯(cuò)問(wèn)題解決方案lvalue required as left opera
這篇文章主要介紹了c++報(bào)錯(cuò):lvalue required as left operand of assignment,出現(xiàn)此錯(cuò)誤原因,是因?yàn)?,等?hào)左邊是不可被修改的表達(dá)式或常量,而表達(dá)式或常量不能作為左值,需要的朋友可以參考下2023-01-01C語(yǔ)言實(shí)現(xiàn)單元測(cè)試的示例詳解
單元測(cè)試(unit testing),是指對(duì)軟件中的最小可測(cè)試單元進(jìn)行檢查和驗(yàn)證。這篇文章主要為大家介紹了C語(yǔ)言實(shí)現(xiàn)單元測(cè)試的方法,需要的可以參考一下2022-09-09C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單猜拳小游戲
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單猜拳小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03