Apt-Get install libmysqlclient-Dev
MySQL uses XAMPP and needs to specify sock
Source code: Main. c
# If Defined (_ Win32) | defined (_ win64) // To support Compilation on Windows # Include <Windows. h> # Endif # Include <Stdio. h> # Include <Stdlib. h> # Include <MySQL/MySQL. h> // On my machine, the file is in/usr/local/include/MySQL. // Define the macros for database operations, or write them directly after they are not definedCode # Define Select_query "select username from test where userid = % d" Int Main ( Int Argc, Char ** Argv) // Char ** argv is equivalent to char * argv [] {MySQL, * Sock; // Defines the database connection handle, which is used by almost all MySQL functions. Mysql_res * res; // Query Result set, structure type Mysql_field * FD; // Structure Containing Field Information Mysql_row row; // String Array that stores the query results of a row Char Qbuf [ 160 ]; // Stores query SQL statement strings If (Argc! = 2 ){ // Check input parameters Fprintf (stderr, " Usage: mysql_select <userid> \ n " ); Exit ( 1 );} Mysql_init ( & MySQL ); If (! (Sock = mysql_real_connect (& MySQL, " Localhost " , " Root " , "" ," Test " , 3306 , " /Opt/lampp/var/MySQL. Sock " , 0 ) {Fprintf (stderr, " Couldn't connect to engine! \ N % s \ n " , Mysql_error (& MySQL); perror ( "" ); Exit ( 1 );} Sprintf (qbuf, select_query, atoi (argv [ 1 ]); If (Mysql_query (sock, qbuf) {fprintf (stderr, " Query failed (% s) \ n " , Mysql_error (sock); exit ( 1 );} If (! (RES = Mysql_store_result (sock) {fprintf (stderr, " Couldn't get result from % s \ n " , Mysql_error (sock); exit ( 1 );} Printf ( " Number of fields returned: % d \ n " , Mysql_num_fields (RES )); While (ROW = Mysql_fetch_row (RES) {printf ( " Ther userid # % d's username is: % s \ n " , Atoi (argv [ 1 ]), (Row [ 0 ] = NULL )&&(! Strlen (row [ 0 ])? " Null " : Row [ 0 ]); Puts ( " Query OK! \ N " );} Mysql_free_result (RES); mysql_close (sock); exit ( 0 ); Return 0 ; // . To be compatible with most compilers, add this line }
Compile:
Gcc-O mysql_select./Main. C-lmysqlclient
Simple class Encapsulation
Test. SQL
Mysql> use test;
Mysql> source ~ /Test. SQL;
Drop Table If Exists 'Test '; /* ! 40101 set @ saved_cs_client = @ character_set_client */ ; /* ! 40101 set character_set_client = utf8 */ ; Create Table 'Test' ('id' Int ( 11 ) Not Null Auto_increment, 'value' Text , Primary Key ('Id') Engine = InnoDB auto_increment = 6 Default Charset = Latin1; /* ! 40101 set character_set_client = @ saved_cs_client */ ; ---- Dumping data for table 'test' -- Lock tables 'test' write; /* ! 40000 alter table 'test' disable keys */ ; Insert Into 'Test' Values ( 1 , ' Hxl ' ),( 2 , ' SQLite ' ),( 3 , ' Test ' ),( 4 , ' For ' ),( 5 ,' Linux ' ); /* ! 40000 alter table 'test' enable keys */ ; Unlock tables; /* ! 40103 set time_zone = @ old_time_zone */ ; /* ! 40101 set SQL _mode = @ old_ SQL _mode */ ; /* ! 40014 set foreign_key_checks = @ old_foreign_key_checks */ ; /* ! 40014 set unique_checks = @ old_unique_checks */ ; /* ! 40101 set character_set_client = @ old_character_set_client */ ; /* ! 40101 set character_set_results = @ old_character_set_results */ ; /* ! 40101 set collation_connection = @ old_collation_connection */ ; /* ! 40111 set SQL _notes = @ old_ SQL _notes */ ; -- Dump completed on 23:52:14
Main. CC
# Include <cstdlib> # Include <Fstream> # Include <Iomanip># Include <Iostream> Extern " C " {# Include <MySQL/MySQL. h> # Include < String . H> } Using Namespace STD; Class Mydb { Private : MySQL, * Sock; // Defines the database connection handle, which is used by almost all MySQL functions. Mysql_res * res; // Query Result set, structure type Mysql_field * FD; // Structure Containing Field Information Mysql_row row; // String Array that stores the query results of a row Public : Mydb () {cout < " Nothing " < Endl;} mydb ( Char * IP, Char * User, Char * Passwd, Char * Db_name, Int Port, Char * Socket) {mysql_init ( & MySQL ); If (! (Sock = mysql_real_connect (& MySQL, IP, user, passwd, db_name, port, socket, 0 ) {Fprintf (stderr, " Couldn't connect to engine! \ N % s \ n " , Mysql_error (& MySQL); perror ( "" ); Exit ( 1 );}} ~ Mydb () {mysql_free_result (RES); mysql_close (sock); cout < " Connect destoryed " < Endl ;} Void Get ( Char * Str ){ If (Mysql_query (sock, STR) {fprintf (stderr, " Query failed (% s) \ n " , Mysql_error (sock); exit ( 1 );} If (! (RES = Mysql_store_result (sock) {fprintf (stderr, " Couldn't get result from % s \ n " , Mysql_error (sock); exit ( 1 );} Printf ( " Number of fields returned: % d \ n " , Mysql_num_fields (RES )); While (ROW =Mysql_fetch_row (RES) {printf ( " Ther username is: % s \ n " , (Row [ 0 ] = NULL )&&(! Strlen (row [ 0 ])? " Null " : Row [ 0 ]); Puts ( " Query OK! " );}}}; Int Main ( Int Argc, Char * Argv []) {mydb test ( " Localhost " , " Root " , "" , " Test " , 3306 , " /Opt/lampp/var/MySQL. Sock " ); Test. Get ( " Select value from test where id = 1 " ); Return Exit_success ;} // ---------- End of function main ----------
Compile:
G ++-g-o mysql_select./MySQL. CC-lmysqlclient
End