C語言訪問MYSQL數據庫的完整的代碼例子


1、手寫安裝帶mysql sdk 的mysql
2、新建控制台項目,項目屬性中把
C:\Program Files\MySQL\MySQL Server 5.5\include 加入“包含目錄”
把C:\Program Files\MySQL\MySQL Server 5.5\lib 加入“庫目錄”

3、stdafx.h中加入
#include <winsock.h> //注意順序,要放在mysql.h前
#include <mysql.h>//控制台項目中要在mysql.h之前include <winsock.h>

//注意lib添加到“庫目錄”中,而不是“引用目錄”中
#pragma comment(lib, "libmysql.lib")

4、把libmysql.dll放到生成的exe目錄下

5、主要的幾個例子:
//執行基本查詢
void test1()
{
 MYSQL *pConn;
 pConn = mysql_init(NULL);
 //第2、3、4、5參數的意思分別是:服務器地址、用戶名、密碼、數據庫名,第6個為mysql端口號(0為默認值3306)
 if(!mysql_real_connect(pConn,"localhost","root","root","test",0,NULL,0))
 {  
  printf("無法連接數據庫:%s",mysql_error(pConn));
  return;
 }
 mysql_query(pConn,"set names gbk");//防止亂碼。設置和數據庫的編碼一致就不會亂碼
 //SET NAMES x 相當於 SET character_set_client = x;SET character_set_results = x;SET character_set_connection = x;
 //寫set character set gbk;查詢不會亂碼,但是參數化插入會報錯。而set names gbk則都不會亂碼


 //mysql_real_query比mysql_query多了個參數: 字符串query的長度, 所以適合有二進制數據的query, 而mysql_query的字符串query不能包含二進制,因為它以\0為結尾
 //mysql_query() 不能傳二進制BLOB字段,因為二進制信息中的\0會被誤判為語句結束。 mysql_real_query() 則可以。
 if(mysql_query(pConn,"select * from persons"))
 {
  printf("查詢失敗:%s",mysql_error(pConn));
  return;
 }

 //mysql_store_result是把查詢結果一次性取到客戶端的離線數據集,當結果比較大時耗內存。
 //mysql_use_result則是查詢結果放在服務器上,客戶端通過指針逐行讀取,節省客戶端內存。但是一個MYSQL*連接同時只能有一個未關閉的mysql_use_result查詢
 MYSQL_RES *result = mysql_store_result(pConn);
 MYSQL_ROW row;
 while(row = mysql_fetch_row(result))
 {
  printf("%s %s\n",row[1],row[2]);
 }

 mysql_free_result(result);
 mysql_close(pConn);
}

//獲得更新行數
void test2()
{
 MYSQL *pConn;
 pConn = mysql_init(NULL);
 if(!mysql_real_connect(pConn,"127.0.0.1","root","root","test",0,NULL,0))
 {
  printf("無法連接數據庫:%s",mysql_error(pConn));
  return;
 }
 if(mysql_query(pConn,"update persons set Age=Age+1"))
 {
  printf("執行失敗:%s",mysql_error(pConn));
  return;
 }
 printf("更新成功,共更新完成%d條",mysql_affected_rows(pConn));
 mysql_close(pConn);
}

//獲得自增id
void test3()
{
 MYSQL *pConn;
 pConn = mysql_init(NULL);
 if(!mysql_real_connect(pConn,"127.0.0.1","root","root","test",0,NULL,0))
 {
  printf("無法連接數據庫:%s",mysql_error(pConn));
  return;
 }
 mysql_query(pConn,"set names gbk");
 if(mysql_query(pConn,"insert into persons(Name,Age) values('如鵬網',100)"))
 {
  printf("執行insert失敗%s",mysql_error(pConn));
  return;
 }
 printf("執行insert成功,新id=%d",mysql_insert_id(pConn));
 mysql_close(pConn);

}

//參數化查詢
void test4()
{
 MYSQL* pConn;
 pConn = mysql_init(NULL);
 if(!mysql_real_connect(pConn,"127.0.0.1","root","root","test",0,NULL,0))
 {
  printf("數據庫連接失敗:%s",mysql_error(pConn));
  return;
 }

 mysql_query(pConn,"set names gbk");
 MYSQL_STMT    *stmt;   
    MYSQL_BIND    bind[2];  
 memset(bind,0,sizeof(bind));//把is_null、length等字段默認值設置為NULL等默認值,否則執行會報錯

 stmt = mysql_stmt_init(pConn); 
 char* insertSQL="insert into persons(Name,Age) values(?,?)";
    if (mysql_stmt_prepare(stmt, insertSQL, strlen(insertSQL)))   
    {   
        fprintf(stderr, " mysql_stmt_prepare(), INSERT failed,%s\n",mysql_error(pConn));
        return;   
    }   
 bind[0].buffer_type= MYSQL_TYPE_STRING;   
    bind[0].buffer= "黑馬";   
    bind[0].buffer_length= strlen("黑馬"); //如果設定了buffer_length,則可以不試用length
   
    int age=3;

 bind[1].buffer_type= MYSQL_TYPE_LONG;   
    bind[1].buffer= &age; 
 bind[1].buffer_length = sizeof(age);
    
    if (mysql_stmt_bind_param(stmt, bind))   
    {   
        fprintf(stderr, " mysql_stmt_bind_param() failed %s\n", mysql_stmt_error(stmt));   
        return;
    }   
   
    if (mysql_stmt_execute(stmt))   
    {   
        fprintf(stderr, " mysql_stmt_execute(), failed %s\n", mysql_stmt_error(stmt));   
        return;  
    }   
 mysql_stmt_close(stmt);
 mysql_close(pConn); 
 printf("參數化執行SQL結束");
}

如鵬網.Net培訓班正在報名,有網絡的地方就可以參加如鵬網的學習,學完就能高薪就業,點擊此處了解

 

    三年前只要懂“三層架構”就可以說“精通分層架構”;現在則需要懂IOC(AutoFac等)、CodeFirst、lambda、DTO等才值錢;

    三年前只要會SQLServer就可以說自己“精通數據庫開發”;現在則需還需要掌握MySQL等開源數據庫才能說是“.Net開源”時代的程序員;

    三年前只要會進行用戶上傳內容的安全性處理即可;現在則需要熟悉雲存儲、CDN等才能在雲計算時代游刃有余;

    三年前只要掌握Lucene.Net就會說自己“熟悉站內搜索引擎開發”;現在大家都用ElasticSearch了,你還用Lucene.Net就太老土了;

    三年前發郵件還是用SmtpClient;現在做大型網站發郵件必須用雲郵件引擎;

    三年前緩存就是Context.Cache;現在則是Redis、Memcached的天下;

    如鵬網再次引領.Net社區技術潮流!點擊此處了解如鵬網.Net最新課程


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM