Home  >  Article  >  Database  >  How to connect vc to mysql database

How to connect vc to mysql database

藏色散人
藏色散人Original
2020-10-26 09:48:442788browse

How to connect vc to mysql database: first open VC6; then add the include directory path of the locally installed MySQL in the middle list box; then select "Library files" and add the Lib directory path of MySQL; finally perform programming testing That’s it.

How to connect vc to mysql database

Recommended: "mysql video tutorial"

1. MySQL installation

To install MySQL, just go to the official website and download it. . . The latest is version 5.7. .

2. VC6.0 settings

(1) Open VC6. Select 0 Options under the Tools menu of the toolbar , click "Includefiles" in the "Show directories for:" drop-down list on the right side of the Directories tab, and then add the include directory path of your locally installed MySQL in the middle list box. As shown in the picture:

(2) In the "Show directories for:" drop-down list mentioned above Select "Library files" and then add the Lib directory path to your local MySQL installation. As shown in the picture:

**I would like to explain here: careful people will find that my directory is different from the previous picture. Same, this is because of this error: libmysql.lib: fatal error LNK1113: invalid machine Invalid server

This is because vc develops a 32-bit program , and the MySQL database is caused by 64-bit. If you use a 32-bit program to operate a 64-bit database, you will definitely make an error. I will explain in detail how to solve it in the next blog post.

(3) Add "libmysql.lib" in "Project settings->Link:Object/library modules".


(5) It is recommended to copy "libmySQL.lib, libmySQL.dll" to the directory of the project you built.

These two files are in the D:\Mysql\lib directory.

3. Programming implementation

1. A simple small program to see if the connection can be successful. . .

#include <stdio.h>
#include <windows.h>
#include <mysql.h> 

int main()
{

       MYSQL mysql;
       mysql_init(&mysql); //初始化mysql结构

       if(!mysql_real_connect(&mysql,"localhost","myuser","123456","student_db",3306,NULL,0))
              printf("\n连接数据库时发生错误!\n");
       else
              printf("\n连接数据库成功!\n");

       mysql_close(&mysql); //释放数据库
  
       return 0;
}

mysql_real_connect(&mysql,"localhost","myuser","123456","student_db",3306,NULL,0)//myuser is my user name, "123456" is the password , "student_db" is the database, 3306 is the port number

2. Implement the query applet

// test.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
#include <windows.h>
#include "StdAfx.h"

#include <winsock.h>  
#include <iostream>  
#include <string>  
#include <mysql.h>  
using namespace std;  
//不需要单步调试的就注释掉  
//#define STEPBYSTEP  
  
void pause(){  
  
    #ifdef STEPBYSTEP  
        system("pause");  
    #endif  
}  
void writeToFile(const char *s)  
{  
  
     FILE *fp=fopen("info.txt","rw");  
     fprintf(fp,s);  
     fclose(fp);  
  
}  
 /* int main()

{

       MYSQL mysql;
       mysql_init(&mysql); //初始化mysql结构

       if(!mysql_real_connect(&mysql,"localhost","myuser","123456","student_db",3306,NULL,0))
              printf("\n连接数据库时发生错误!\n");
       else
              printf("\n连接数据库成功!\n");

       mysql_close(&mysql); //释放数据库
  
       return 0;
}*/
int main(int argc, char* argv[]){  
  
    cout<<"start...."<<endl;  
    pause();  
    MYSQL mysql;  
    if(0==mysql_library_init(0,NULL,NULL))  
    {  
        cout<<"mysql_library_init succeed"<<endl;  
  
    }else{  
        cout<<"mysql_library_init failed"<<endl;  
        return -1;  
    }  
    pause();  
    if(NULL!=mysql_init(&mysql))  
    {  
  
        cout<<"mysql_init succeed"<<endl;  
    }else{  
        cout<<"mysql_init failed"<<endl;  
        return -1;  
    }  
    pause();  
    if(0==mysql_options(&mysql,MYSQL_SET_CHARSET_NAME,"gb2312"))  
    {  
  
        cout<<"mysql_option succeed"<<endl;  
    }else{  
        cout<<"mysql_option failed"<<endl;  
        return -1;  
    }  
    pause();  
  
    if(NULL!=mysql_real_connect(&mysql,"localhost","myuser","123456","student_db",3306,NULL,0))  
    {  
  
        cout<<"mysql_real_connect succeed"<<endl;  
    }else{  
        cout<<"mysql_real_connect failed"<<endl;  
        return -1;  
    }  
    pause();  
    string sql;  
    
    sql="select * from sgroup";  
    MYSQL_RES *result=NULL;  
    if(0==mysql_query(&mysql,sql.c_str()))  
    {  
  
            cout<<"mysql_query select succeed"<<endl;  
            result=mysql_store_result(&mysql);  
            int rowcount=mysql_num_rows(result);  
            cout<<"row count:"<<rowcount<<endl;  
            unsigned int fieldcount=mysql_num_fields(result);  
            MYSQL_FIELD *field=NULL;  
            for(unsigned int i=0;i<fieldcount;i++)  
            {  
  
                field=mysql_fetch_field_direct(result,i);  
                cout<<field->name<<"\t\t";  
            }  
            cout<<endl;  
            MYSQL_ROW row=NULL;  
            row=mysql_fetch_row(result);  
            while(NULL!=row)  
            {  
  
                for(int i=0;i<fieldcount;i++){  
  
                    cout<<row[i]<<"\t\t";  
  
                }  
                cout<<endl;  
                row=mysql_fetch_row(result);  
  
            }  
    }else{  
  
            cout<<"mysql_query select data failed"<<endl;  
            mysql_close(&mysql);  
            return -1;  
    }  
    pause();  
    /*sql="drop table user_info";  
    if(0==mysql_query(&mysql,sql.c_str()))  
    {  
  
            cout<<"mysql_query drop table succeed"<<endl;  
    }else{  
            cout<<"mysql_query drop table failed"<<endl;  
            mysql_close(&mysql);  
            return -1;  
  
    }  */
    mysql_free_result(result);  
    mysql_close(&mysql);  
    mysql_server_end();  
  
  
    system("pause");  
    return 0;  
}

Running results:

The connection is successful. . Ha ha. .

The above is the detailed content of How to connect vc to mysql database. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn