国产探花免费观看_亚洲丰满少妇自慰呻吟_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語言程序設計有所幫助。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 白银市| 遵化市| 四会市| 怀仁县| 梓潼县| 西安市| 铁岭市| 库伦旗| 林口县| 乳源| 永州市| 建瓯市| 堆龙德庆县| 大关县| 平顺县| 淅川县| 宜都市| 平凉市| 乌鲁木齐县| 唐河县| 开化县| 乳山市| 丽江市| 平阳县| 台东县| 个旧市| 天峨县| 泗洪县| 从江县| 绥芬河市| 峨山| 莒南县| 上栗县| 博兴县| 漳州市| 泉州市| 甘南县| 永昌县| 建昌县| 比如县| 靖宇县|