C + + operation MySQL Method summary (2) [reprint]

Source: Internet
Author: User

C + + operation MySQL Method summary (2)

C + + uses ODBC and two ways to operate MySQL via MFC ODBC

Operation with vs2013 and 64-bit mSQL 5.6.16

The database name and table data used in the project are described in the summary of the C + + operation MySQL Method (1)

Now that you're using ODBC, you need to add a data source.

User dsn-> Add, data source, management tools, control Panel

Fill in the relevant database connection configuration and use of the database, you can click test for testing, to see if the connection is successful, click OK when the settings are complete

Where the data Source name is MYSQLODBC we will use when connecting to the database

At this point, the data source has been added

I. Operation via ODBC

Open Database Interconnect (CONNECTIVITY,ODBC) is a set of canonical interfaces provided by Microsoft for database access, and most databases provide ODBC drivers

1. Create a new empty project

2. If you are using MySQL that is 64-bit, you need to change the project's solution platform from Win32 to x64

At this point, all the relevant configurations are complete

Program code

Main.cpp

#include <Windows.h>#include <iostream>#include <iomanip>#include <sql.h>#include <odbcss.h>#include <sqlext.h>#define Maxbuflen 255#define Maxnamelen 20#import"C:\Program Files\Common Files\system\ado\msado15.dll"No_namespace Rename ("Eof","Adoeof") sqlhenv henv = sql_null_henv;//Defines the environment handle SQLHDBC HDBC1 = SQL_NULL_HDBC;//Define database connection handle sqlhstmt HSTMT1 = sql_null_hstmt;//To define a statement handleUsingNamespaceStdIntMain () {RETCODE RETCODE;//Error return code Retcode = Sqlallochandle (sql_handle_env, NULL, &HENV);if (Retcode <0)//Error handling{cout <<"Allocate ODBC environment handle errors."<<EndlReturn-1; } Retcode =SQLSetEnvAttr (Henv, Sql_attr_odbc_version, (Sqlpointer) SQL_OV_ODBC3, Sql_is_integer);if (Retcode <0)//Error handling{cout <<"The ODBC is not version3.0"<<EndlReturn-1; } Retcode = Sqlallochandle (Sql_handle_dbc, henv, &HDBC1);if (Retcode <0)//Error handling{cout <<"Allocate ODBC connection handle errors."<<EndlReturn-1; }char* SZDSN ="Mysqlodbc";//When you add a data source, the name of thechar* Szuid ="Root";char* Szauthstr ="123456"; Retcode =SQLConnect (HDBC1, (sqlchar*) Szdsn, (SWORD) strlen (SZDSN), (sqlchar*) Szuid, (SWORD) strlen (Szuid), (sqlchar*) Szauthstr, (SWORD) strlen (SZAUTHSTR));if (Retcode <0)//Error handling{cout <<"Connect to ODBC DataSource errors."<<EndlReturn-1; } Retcode = Sqlallochandle (sql_handle_stmt, HDBC1, &HSTMT1);if (Retcode <0) {cout <<"Allocate ODBC statement handle errors."<<EndlReturn-1; } Retcode = SQLExecDirect (HSTMT1, (sqlchar*)"SElECT * FROM book", sql_nts);if (Retcode <0) {cout <<"Executing statement throught ODBC errors."<<EndlReturn-1; } SQLCHAR Bookname[maxnamelen +1]; SQLCHAR Size[maxnamelen +1]; Sqllen Columnlen =0; Retcode = SQLBindCol (HSTMT1,2, Sql_c_char, BookName, Maxnamelen, &Columnlen); Retcode = SQLBindCol (HSTMT1,3, Sql_c_char, size, Maxnamelen, &Columnlen); While ((Retcode = SQLFetch (HSTMT1)) = sql_no_data) {cout <<setw (9) <<"bookname:" & lt;< bookname << endl cout << setw (9) << "size:" << size <<  Endl } system ("pause");}              

Run results

First, the operation through the MFC ODBC

MFC provides an encapsulation of ODBC, making it easy to use MFC to create ODBC applications

1. Create a new Win32 Console application

2. Make the following changes

Use of MFC: Using MFC in Shared DLLs

Character set: Using the Unicode character set

3. If you are using MySQL that is 64-bit, you need to change the project's solution platform from Win32 to x64

At this point, all the relevant configurations are complete

Program code

Main.cpp

#include"StdAfx.h"#include"Afxdb.h"#include <iostream>#include <iomanip>UsingNamespaceStdint _tmain (int argc, _tchar*Argv[]) {CDatabase db;if (!db. IsOpen ())//Determine if the database has been opened{BOOL bflag= db. Open (NULL, False, False, _t ("ODBC;DSN=MYSQLODBC; Uid=root; pwd=123456"));if (!Bflag) {cout <<"ERROR"<<Endl }} CRecordset RS (&DB); CString str; Str. Format (L"SELECT * FROM book");Try{Rs. Open (CRecordset::forwardOnly, (L"%s", str));Short Nfields =Rs. Getodbcfieldcount ();while (!Rs. IsEOF ()) {cdbvariant varvalue; Rs. GetFieldValue (L"BookName", Varvalue); cout << SETW (9) <<"BookName:"<< *varvalue.m_pstringa << Endl; Rs. GetFieldValue (L "size "" size: Endl; Rs. MoveNext (); }} catch (...) {cout <<  "errpr" << Endl;} db. Close (); System ( "pause" );} 

Run results

C + + operation MySQL Method summary (2) [reprint]

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.