Windows下用C語言串連Mysql注意問題

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   sp   for   

原文:Windows下用C語言串連Mysql注意問題

環境是:在VS6.0安裝Mysql後,我們需要相應的標頭檔以及lib檔案,所以安裝過程必須是完整安裝。否則不會產生include檔案夾哦~具體步驟如下:1.完整安裝mysql,這樣才能產生include檔案夾。2.在Directories的標 簽頁中右邊的“Show directories for”下拉式清單中選中“Includefiles”,然後在中間列表框中添加你本地安裝MySQL的include目錄路徑。即指明mysql的API介面有哪些函數。我機器的路徑是:C:\Program Files\MySQL\MySQL Server 5.0\include3.告訴編譯器這些API函數的可執行檔在哪兒(libmysql.dll)。選擇Tools菜單下的Options選項,在Directories的標籤頁中右邊的“Showdirectories for”下拉式清單中選中“Libraryfiles”,然後添加你本地安裝MySQL的Lib目錄路徑。C:\Program Files\MySQL\MySQL Server 5.0\lib\debug或者C:\ProgramFiles\MySQL\MySQL Server 5.0\lib\opt下,我的是在第一個下面。根據情況找。4.選擇“Project settings->Link:Object/librarymodules”,添加“libmysql.lib”  或者直接在程式頭添加:#pragma comment(lib, "libmysql")
注意:C:\Program Files\MySQL\MySQL Server5.0\lib\debug下的libmysql.dll和libmysql.lib最好拷貝到開發目錄下,否則可能會拋出執行階段錯誤。
==========下面在附加上一個小例子吧:===========#include "stdafx.h"#include <stdio.h>#include <stdlib.h>#include <winsock2.h>#pragma comment(lib, "ws2_32")#pragma comment(lib, "libmysql")#include "mysql.h"  int main(int argc, char* argv[]){int res,j;    MYSQLmysql;   MYSQL_RES *resultset;   MYSQL_ROW row;     mysql_init(&mysql);// 初始化mysql結構 //串連本機,使用者名稱是root,密碼是hope,資料庫是hope,連接埠是3306    if(!mysql_real_connect(&mysql,"localhost", "root", "hope", "hope", 3306, NULL, 0))    {       printf("\n資料庫連接發生錯誤!");    }   else    {      printf("\n資料庫連接成功!\n"); //插入一條資料到資料庫res = mysql_query(&mysql, "insert intostudent(name,age,rollno) values(‘elisa‘,33,‘3‘)");       if(!res)       {           printf("插入%lu行資料成功!\n",(unsignedlong)mysql_affected_rows(&mysql));       }       else printf("插入資料失敗!\n");             if(mysql_query(&mysql,"select *from student")){printf("資料庫查詢發生錯誤");}else{                         //檢索資料          printf("\n查詢資料為:\n");                  resultset= mysql_store_result(&mysql);// 獲得結果集                 if(mysql_num_rows(resultset) != NULL)          {int numRows = mysql_num_rows(resultset); // 獲得結果集中的記錄數int numFields = mysql_num_fields(resultset);// 獲得表中欄位數printf("共 %d 行記錄,每行 %d 個欄位。", numRows, numFields);j = 1;              while (row= mysql_fetch_row(resultset))              {                 int i = 0;                 printf("\n 第 %d 行:", j);for (i = 0; i < numFields; i++)                 {                    fprintf(stdout, " %s", row[i]); // 列印欄位值                 }                 j++;               }          }          else          {             printf("\n無查詢結果!");         }          mysql_free_result(resultset);  // 釋放結果集      }    }    mysql_close(&mysql); //釋放資料庫連接 fgetchar();return 0;}

Windows下用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.