VC串連Mysql及開發詳細指南

來源:互聯網
上載者:User

最近要用VC串連WEB上的MYSQL,在網上找了很多資料.都不是非常的詳細。
所以今天,我就把我自已怎樣一步步串連MYSQL的步驟,以及語句等的寫法一起寫出來。
一是為了自已備忘,同時也希望能對需要的朋友有協助.
準備工作:
建議:以下最好都用方法1,簡單易用,也可以防止以後使用麻煩。
一、mysql.h標頭檔問題。
因為新在VC工程,在裡面要包含這樣的標頭檔
#include "winsock.h"
#include "mysql.h"
所以:
要把mysql.h複製到vc的安裝目錄的include目錄下
方法1:
mysql.h在你mysql的安裝目錄下的include裡面如:mysql\include
方法2:
把這個標頭檔添加到你的目錄裡面,或者在VC(我用的是Visual c++6.0)的功能表列
點擊progect-->setting-->在出現的對話方塊裡面選擇c/c++屬性頁面-->選擇preprocessor
--->在下面的additional include directories文字框裡面填入你的mysql.h目錄如:
D:\DataBase\mysql\include
建議:
把mysql.h和其他在mysql\include的所以.h的檔案都複製到VS6的安裝目錄裡,如我的是:
我visula studio是安裝在D:\Program Files\下
下面是詳細的目錄位址:
D:\Program Files\Microsoft Visual Studio\VC98\Include
二、libmysql.lib連結庫問題:
方法1:
把libmysql.lib(在mysql的安裝目錄下,搜尋下就能找到)也複製到這個目錄下(D:\Program Files\Microsoft Visual Studio\VC98\Include),要不串連會出錯。
如果編譯串連時還是出錯。就把libmysql.lib複製到你來源程式的目錄中
方法2:
libmysql.lib 在安裝目錄下的lib\debug目錄下
點擊progect-->setting-->在出現的對話方塊裡面選擇link屬性頁面-->選擇input選項
-->在Object/library modules下面添加libmysql.lib
-->在下面的additional include directories文字框裡面輸入你的lib的目錄
如:\DataBase\mysql\lib\debug
建議使用方法1
下面是做樣本工程。
一、建立一個VC-MFC(exe)工程,如取名為:odbc,
第一步選對話方塊,
第二第三步及以下選預設的選項一直到結束。
二、在ODBCDlg.h中加入標頭檔。

#include "winsock.h"
#include "mysql.h"

三、建立一個按鈕button1。新增成員變數,及單擊的訊息。
在ODBCDlg.cpp如下代碼:
void CODBCDlg::OnButton1()
{

  // TODO: Add your control notification handler code here
char *query;
int t,r;
MYSQL_RES *res;
MYSQL_ROW row;
MYSQL *m_mysql;

m_mysql = mysql_init(NULL);
if (!mysql_real_connect(m_mysql,"localhost","databasename",
"yourdatabase-password","yourtable",0,NULL,0))
{
CString errors =mysql_error(m_mysql);
MessageBox(errors,"?í?ó");
}
query="select text from yourtable";
t=mysql_real_query(m_mysql,query,(unsigned int) strlen(query));
if(t)
{
MessageBox(mysql_error(m_mysql));
}
res=mysql_use_result(m_mysql);
row=mysql_fetch_row(res);
CString sumstr;
for(r=0;r<=mysql_field_count(m_mysql);r++)
{
if(row<0)
{
MessageBox("error");
}
for(t=0;t<mysql_num_fields(res);t++)
{
CString Query = row[t];
sumstr+=Query+" ";
}
}
MessageBox(sumstr);
mysql_close(m_mysql);
}

編譯運行。
以下是網上找的另一段串連代碼,沒有測試過是否可用。
挺久的了,隨便寫的測試

 #include "winsock.h"
#include "mysql.h"
int main(int argc, char* argv[])
{
MYSQL * con = mysql_init((MYSQL*) 0);
LPSTR host = "***";
LPSTR user,psw,dbname,;
if ( con !=NULL
&& mysql_real_connect(con,host,user,psw,dbname,3306,NULL,0) )
{
if (!mysql_select_db(con,dbname))
{
printf("Select successfully the database!\n");
con ->reconnect = 1;
if (!mysql_query(con,"select * from table_name"))
{
printf("Show table---orders:\n");
MYSQL_RES * recordSet = mysql_store_result(con);
long j = mysql_num_fields(recordSet);
long i = (long)mysql_num_rows(recordSet);
//get RecordSet of fields
MYSQL_FIELD * field = mysql_fetch_fields(recordSet);
for(int l=0;l<j;l++)
{
printf("%s ",field[l].name);
}
printf("\n");
// get all the row of table---boothinfo
MYSQL_ROW row;
while( row = mysql_fetch_row(recordSet))
{
//get the number of fields
for(int l=0 ; l< j;l++)
{
if(row[l]==NULL || !strlen(row[l]))
printf("NULL ");
else
printf("%s ",row[l]);
}
printf("\n");
}
}
}
else
{
printf("Unable to select the databas!\n");
return 0;
}
return 1;
}
else
{
printf("can't connect to the mysql server!\n");
printf("Errors: %s",mysql_error(con));
return -1;
}
mysql_close(con);
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.