C ++ connects to the local Mysql database. the configuration follows the instructions on the Internet: [cc+3use vs2013to connect to the mysqldata database (www.2cto.comdatabase201411354316.html & quot;). The database access code is also sent online; # include # includeusingnamespacestd; # pragmacomment (lib, & quot; ws2_32.lib & quot;) # pragmacomm mysql database
C ++ connects to the local Mysql database, and the configuration comes from the Internet:
C/c ++ use VS2013 to connect to the mysql database
The database access code also comes from the Internet:
# Include
# Include
# Include
# Include
Using namespace std; # pragma comment (lib, "ws2_32.lib") # pragma comment (lib, "libmysql. lib ") // Single-step execution. comment out if you do not want to perform one-step execution # define STEPBYSTEPint main () {cout <"*********************************** * ***** "<endl; # ifdef STEPBYSTEP system ("pause"); # endif // A required data structure MYSQL mydata; // initialize the database if (0 = mysql_library_init (0, NULL, NULL )) {cout <"mysql_library_init () succeed" <endl;} else {cout <"mysql_library_in It () failed "<endl; return-1 ;}# ifdef STEPBYSTEP system (" pause "); # endif // initialize the data structure if (NULL! = Mysql_init (& mydata) {cout <"mysql_init () succeed" <endl ;}else {cout <"mysql_init () failed" <endl; return-1 ;}# ifdef STEPBYSTEP system ("pause"); # endif // you can set many additional connection options before connecting to the database, set the character set here. Otherwise, the Chinese if (0 = mysql_options (& mydata, MYSQL_SET_CHARSET_NAME, "gbk") {cout <"mysql_options () succeed" <endl;} cannot be processed ;} else {cout <"mysql_options () failed" <endl; return-1 ;}# ifdef STEPBY STEP system ("pause"); # endif // connect to the database if (NULL! = Mysql_real_connect (& mydata, "localhost", "root", "test", "test", 3306, NULL, 0) // address, user name, password, the port can be changed to {cout <"mysql_real_connect () succeed" <endl ;}else {cout <"mysql_real_connect () failed" <endl; return-1 ;}# ifdef STEPBYSTEP system ("pause"); # endif // SQL string sqlstr; // CREATE a TABLE sqlstr = "CREATE TABLE IF NOT EXISTS user_info "; sqlstr + = "("; sqlstr + = "user_id INT UNSIGNED Not null AUTO_INCREMENT primary key comment 'unique User ID', "; sqlstr + =" user_name VARCHAR (100) character set gb2312 COLLATE gb2312_chinese_ci null comment 'name Of user ',"; sqlstr + = "user_second_sum int unsigned not null default 0 COMMENT 'The Summation Of Using time'"; sqlstr + = ");"; if (0 = mysql_query (& mydata, sqlstr. c_str () {cout <"mysql_query () create table succeed" <endl;} else {Cout <"mysql_query () create table failed" <endl; mysql_close (& mydata); return-1 ;}# ifdef STEPBYSTEP system ("pause "); # endif // INSERT data to the table sqlstr = "insert into user_info (user_name) VALUES ('Company name'), ('Level-1 authorization'), ('Level-2 authorization '), ('development group'), ('name'); "; if (0 = mysql_query (& mydata, sqlstr. c_str () {cout <"mysql_query () insert data succeed" <endl ;}else {cout <"mysql_query () insert data failed" <endl; m Ysql_close (& mydata); return-1 ;}# ifdef STEPBYSTEP system ("pause"); # endif // display the inserted data sqlstr = "SELECT user_id, user_name, user_second_sum FROM user_info "; MYSQL_RES * result = NULL; if (0 = mysql_query (& mydata, sqlstr. c_str () {cout <"mysql_query () select data succeed" <endl; // Obtain the dataset result = mysql_store_result (& mydata) at a time ); // Obtain and print the number of rows int rowcount = mysql_num_rows (result); cout <"row count: "<Rowcount <endl; // Obtain and print the name of each field 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" ;}cout <endl; // print all rows MYSQL_ROW row = NULL; row = mysql_fetch_row (result); while (NULL! = Row) {for (int I = 0; I <fieldcount; I ++) {cout <row [I] <"\ t ";} cout <endl; row = mysql_fetch_row (result) ;}} else {cout <"mysql_query () select data failed" <endl; mysql_close (& mydata ); return-1 ;}# ifdef STEPBYSTEP system ("pause"); # endif // delete the created TABLE sqlstr = "drop table user_info "; if (0 = mysql_query (& mydata, sqlstr. c_str () {cout <"mysql_query () drop table succeed" <endl ;}else {cout <"mysql_query () drop table failed" <endl; mysql_close (& mydata); return-1;} mysql_free_result (result); mysql_close (& mydata); mysql_server_end (); system ("pause"); return 0 ;}
However, mysql_options always reports an error: heap damage.
Mysql_real_connect always reports an error: access memory conflict
Swollen?