C語言訪問MySQL資料庫的方法

來源:互聯網
上載者:User

標籤:res   base   dea   eal   sso   路徑   error   windows   style   

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 parametermysql_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 statementschar* 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 statementsif(mysql_query(&mysql,dest)){printf("Cannot access the database with excuting \"%s\".",dest);system("pause");exit(-1);}//deal with the resultresult=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 resourcemysql_free_result(result);mysql_close(&mysql);system("pause");return 0;}


C語言訪問MySQL資料庫的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.