C語(yǔ)言實(shí)現(xiàn)訪問(wèn)及查詢MySQL數(shù)據(jù)庫(kù)的方法
本文實(shí)例講述了C語(yǔ)言實(shí)現(xiàn)訪問(wèn)及查詢MySQL數(shù)據(jù)庫(kù)的方法。分享給大家供大家參考,具體如下:
1、添加頭文件路徑(MySQL安裝路徑中的include路徑)
2、添加庫(kù)文件(直接從MySQL安裝路徑中copy libmysql.lib即可)
3、編程操作數(shù)據(jù)庫(kù)
代碼
// AccessToMySQL.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。 // #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; }
運(yùn)行效果:
希望本文所述對(duì)大家C語(yǔ)言程序設(shè)計(jì)有所幫助。
相關(guān)文章
Cocos2d-x中使用CCScrollView來(lái)實(shí)現(xiàn)關(guān)卡選擇實(shí)例
這篇文章主要介紹了Cocos2d-x中使用CCScrollView來(lái)實(shí)現(xiàn)關(guān)卡的選擇實(shí)例,本文在代碼中用大量注釋講解了CCScrollView的使用,需要的朋友可以參考下2014-09-09C++程序中main(int argc, char *argv[])函數(shù)的參數(shù)意義
這篇文章主要介紹了C++程序中main(int argc, char *argv[])函數(shù)的參數(shù)意義,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09C語(yǔ)言編程之動(dòng)態(tài)內(nèi)存與柔性數(shù)組的了解
本文是C語(yǔ)言編程篇,這篇文章主要為大家介紹了C語(yǔ)言編程中動(dòng)態(tài)內(nèi)存的函數(shù)與柔性數(shù)組的特點(diǎn),有需要的朋友可以借鑒參考下,希望可以有所幫助2021-09-09使用mmap實(shí)現(xiàn)多進(jìn)程對(duì)大文件拷貝
這篇文章主要介紹了使用mmap實(shí)現(xiàn)多進(jìn)程對(duì)大文件拷貝,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10C++中vector類的一些簡(jiǎn)單實(shí)現(xiàn)
C++中的std::vector是一個(gè)動(dòng)態(tài)數(shù)組(也被稱為可變大小數(shù)組)的容器類,它是C++標(biāo)準(zhǔn)庫(kù)提供的其中一種容器類,提供了方便的操作和管理動(dòng)態(tài)數(shù)組的功能,本文就給大家介紹了C++中vector類的簡(jiǎn)單實(shí)現(xiàn)代碼,需要的朋友可以參考下2023-08-08