I have always wanted to use connector to connect, but because the official MySQL file and the local compilation system is not the same, so you need to add connector
-stdlib=libc++ recompile again. Hah I don't.
What else can I do? Use ODBC! Try using the ODBC administrator of the Mac, but this is too old, and inconvenient (I feel inconvenient) so also did not adopt.
So I finally chose to use the MySQL C API to connect to the database, and here is a very simple example:
#include <iostream>#include<vector>#include<string>#include<mysql.h>using namespacestd;intMain () {Ios::sync_with_stdio (false); MYSQL connection; Mysql_init (&connection); Mysql_real_connect (&connection,"localhost","Root","1234", " World",3306, nullptr,0); //mysql_query is a value-result function intquery_success = mysql_query (&connection,"SELECT * from City LIMIT"); if(Query_success = =0){ //Save Query results to resultmysql_res* result = Mysql_store_result (&connection); if(Result) {Std::cout<<"Number of result:"<< static_cast<size_t> (mysql_num_rows (result)) <<"\ n"; //Get column namemysql_field*field; Vector<string>column; while(field =Mysql_fetch_field (Result))) {Column.emplace_back (field-name); } //or use Mysql_num_fields (result), the number of rows to get the result for(Constauto&col:column) {cout<< Col <<"\ t"; } cout<<"\ n"; Mysql_row ROW; while(row =mysql_fetch_row (Result))) { for(size_t i =0; I! = Column.size (); ++i) {Std::cout<< Row[i] <<"\ t"; } cout<<"\ n"; }} mysql_free_result (Result); } mysql_close (&connection); return 0;}
Make some minor changes:
#include <iostream>#include<vector>#include<string>#include<mysql.h>using namespacestd;intMain () {Ios::sync_with_stdio (false); MYSQL connection; Mysql_init (&connection); Mysql_real_connect (&connection,"localhost","Root","1234", " World",3306, nullptr,0); //mysql_query is a value-result function intquery_success = mysql_query (&connection,"SELECT * from City"); if(Query_success = =0){ //Save Query results to resultmysql_res* result = Mysql_store_result (&connection); if(Result) {cout<<"Number of result:"<< static_cast<size_t> (mysql_num_rows (result)) <<"\ n"; Mysql_field*field; while(field =Mysql_fetch_field (Result))) {cout<< Field->name <<"\ t"; } cout<<"\ n"; Mysql_row ROW; AutoConstField_num =mysql_num_fields (Result); while(row =mysql_fetch_row (Result))) { for(size_t i =0; I! = Field_num; ++i) {Std::cout<< Row[i] <<"\ t"; } cout<<"\ n"; }} mysql_free_result (Result); } mysql_close (&connection); return 0;}
A very simple C + + code to operate MySQL