国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C語言實現訪問及查詢MySQL數據庫的方法

2020-05-23 13:31:36
字體:
來源:轉載
供稿:網友

本文實例講述了C語言實現訪問及查詢MySQL數據庫的方法。分享給大家供大家參考,具體如下:

1、添加頭文件路徑(MySQL安裝路徑中的include路徑)
2、添加庫文件(直接從MySQL安裝路徑中copy libmysql.lib即可)
3、編程操作數據庫

代碼

// AccessToMySQL.cpp : 定義控制臺應用程序的入口點。//#include "stdafx.h"#include <Windows.h>#include <mysql.h>#pragma comment(lib,"libmysql.lib")MYSQL mysql;MYSQL_RES* result;MYSQL_ROW row;int main(void){  //init the mysql parameter  mysql_init(&mysql);  //connect the database  if(!mysql_real_connect(&mysql,"127.0.0.1","root","111","mytest",3306,NULL,0))  {    printf(mysql_error(&mysql));    printf("/nCannot access to the database!!!/n");    system("pause");    exit(-1);  }  //construct the query SQL statements  char* sql="select * from student where name='";  char dest[100]={""};  strcat(dest,sql);  printf("Please enter the student name:");  char name[10]={""};  gets(name);  strcat(dest,name);  strcat(dest,"'");  //excute the SQL statements  if(mysql_query(&mysql,dest))  {    printf("Cannot access the database with excuting /"%s/".",dest);    system("pause");    exit(-1);  }  //deal with the result  result=mysql_store_result(&mysql);  if(mysql_num_rows(result))  {    while((row=mysql_fetch_row(result)))    {      printf("%s/t%s/t%s/n",row[0],row[1],row[2]);    }  }  //release the resource  mysql_free_result(result);  mysql_close(&mysql);  system("pause");  return 0;}

運行效果:

C語言,訪問,查詢,MySQL數據庫

希望本文所述對大家C語言程序設計有所幫助。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南和县| 周口市| 武平县| 锡林浩特市| 察隅县| 沾化县| 武山县| 疏附县| 尉氏县| 杭锦后旗| 崇左市| 民和| 西畴县| 长顺县| 连平县| 江华| 博湖县| 呈贡县| 林州市| 石泉县| 二连浩特市| 集贤县| 宁南县| 澜沧| 通榆县| 遂川县| 云梦县| 永平县| 南涧| 英山县| 监利县| 泰和县| 武安市| 扶风县| 自治县| 潞城市| 舞钢市| 明光市| 淮南市| 九江县| 淮南市|