VC ++ connects to MySQL through APIS, and vcapi connects to mysql

Source: Internet
Author: User
Tags mysql commands

VC ++ connects to MySQL through APIS, and vcapi connects to mysql

1, first install MySQL database server, this article is installed with the mysql-installer-community-5.6.10.1.msi version, as to the version of what is different, not here.

The following is the default installation directory structure:


2. Configure the environment variables to make it easier to use mysql commands on the command line. For example

You can configure the following settings. Some installation packages have automatically added this path. You only need to check the path.

 

3. Create a data samp_db under the command line and create the table students. Both database creation and table creation are relatively simple, as shown below.

The command for creating a database is as follows:

>Createdatabase samp_db character set gbk;

Use Database samp_db

>Usesamp_db;

Create a data table

>Createtable students

(

Id int unsigned not null auto_increment primary key,

Name char (8) not null,

Sex char (4) not null,

Age tinyint unsigned not null,

Tel char (13) null default "-",

Address char (60 ),

ModifyDateTime char (30)

);

 

 

4. Set the server to allow network access, and set a host to the wildcard % to allow hosts on the network to access the server.

> Use mysql;

> Select 'host' from user whereuser = 'root ';
> Update user set host = '%' where user = 'root ';
> Flush privileges;

 

 

 

5. Using MySQL APIs to access databases can be divided into initialization, connection, setting, and operations.

Add header file

# Include "mysql. h"

Add a library to the program dependency additional item.

Libmysql. lib

The specific implementation code is as follows:

 

Void CTestMysqlUseAPIDlg: OnBnClickedOk () {// TODO: add the control notification handler code MYSQL * con; MYSQL_RES * res; MYSQL_ROWrow; // configure chartmp [400] in the database; chardbuser [30] = "root"; // database username chardbpasswd [30] = ""; // password chardbip [30] = "192.168.1.101 "; // Database Server IP address intnport = 3306; // database server port number chardbname [30] = "samp_db"; // database name chartablename [30] = "students "; // table name char * query = NULL; unsignedint t; intcount = 0; intrt; // return va Lue // 1. The MYSQL object con = mysql_init (MYSQL *) 0) will be initialized ); // 2. Execute mysql-h 192.168.1.101-u root if (con! = NULL & mysql_real_connect (con, dbip, dbuser, dbpasswd, dbname, nport, NULL, 0) {// 3. Selecting a database is equivalent to executing mysql> use samp_db if (! Mysql_select_db (con, dbname) {con-> reconnect = 1; // 3. Used to set character sets, mainly for the Chinese query = "set names \ 'gbk \ '" saved to the database; rt = mysql_real_query (con, query, strlen (query); if (rt) {printf ("Error making query: % s !!! \ N ", mysql_error (con);} // 4. Set the returned character set. This parameter must be set. Otherwise, other character sets are returned, which are all garbled characters. Query = "SET CHARACTER_SET_RESULTS = \ 'utf8 \ '"; rt = mysql_real_query (con, query, strlen (query); if (rt) {printf ("Error making query: % s !!! \ N ", mysql_error (con) ;}} else {MessageBoxA (NULL," failed to connect to the database. Check the configuration! "," ", NULL); return;} // insert the chartimeBuff [32] = {0}; charbuff [256] = {0}; SYSTEMTIMEst; getLocalTime (& st); sprintf (timeBuff, "% 04d-% 02d-% 02d % 02d: % 02d: % 02d", st. wYear, st. wMonth, st. wDay, st. wHour, st. wMinute, st. wSecond); sprintf (buff, "insert into studentsvalues (NULL, \" % s \ ", \" % s \ ", % d, \" % s \", \ "% s \", \ "% s \"); "," niu XiaoBei "," female ", 28," 135321432 "," Changsha City ", timeBuff ); rt = mysql_real_query (con, buff, strlen (buff); if (rt ){ CStringsError; sError. format (L "% s", mysql_error (con); charbuff [128] = {0}; WideCharToMultiByte (CP_ACP, 0, sError,-1, buff, 127, NULL, FALSE); MessageBox (L "inserterror .! "+ SError, L" ", NULL); return;} // select Operation sprintf (tmp," select * from % s where id> 260; ", tablename ); rt = mysql_real_query (con, tmp, strlen (tmp); if (rt) {MessageBoxA (NULL, "selecterror .! "," ", NULL); return;} // process the result set res = mysql_store_result (con); CStringstr; while (row = mysql_fetch_row (res )) {for (t = 0; t <mysql_num_fields (res); t ++) {wchar_tTmpBuf [128] = {0}; MultiByteToWideChar (CP_UTF8, 0, row [t], -1, TmpBuf, 127); charbuff [127]; WideCharToMultiByte (CP_ACP, 0, TmpBuf,-1, buff, 127, NULL, FALSE ); str + = L "+ CString (buff);} str + = L" \ r \ n ";} // write the result set to GetDlgItem (IDC_EDIT1) in EDIT) -> SetWindowText (str); // release the result set mysql_free_result (res); // close the connection mysql_close (con); return ;}


 

This is a simple result interface.


 

There are many MySQL APIs and you need to learn more.

The link to this document is as follows:

Http://download.csdn.net/detail/cabmail/8852297

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.