SQL itself is not complicated, and I actually feel that deploying MySQL is the most complex environment.
The centos6.5 system is used here to deploy the environment.
First you need to install the database and database development environment, as well as the compilation environment
Yum install MySQL mysql-server mysql-devel gcc gcc-c++-y
Then run the database
Service mysqld Start
If you are using a system with more than CENTOS7, MySQL will be replaced by MARIADB for command execution.
Now enter MySQL directly from the console to access the database system
Here, the data is imported using the database's own test, and if you do not import it, create a database directly, try to build a simple database, use the following command
1 Create DatabaseMySQL2(SnoChar( -)Primary Key,3SnameChar( -) not NULL,4Sagesmallint,5SgdenerChar(2)Check(Sgenderinch'M','F')6);
View Code
OK, this creates a simple database, now import our simple program on the database, note that the program here and the newly created table using the code is not the same.
The extra note is that sprintf is used here to implement queries for SQL variables.
The headache is that in the process of compiling is always prompted to find the database header file, which is used in this compilation command to execute
g++ sql2.cpp ' mysql_config--cflags--libs '-O sql2
Here is the main part of the program
#include <cstdio>#include<mysql.h>#include<iostream>#include<string.h>using namespacestd;//This program is tested in centos6.5, if you want to test, import 20 million data data into the database named Test and then try to//This is a compile command, you can copy and paste directly//g++ sql2.cpp ' mysql_config--cflags--libs '-O sql2//Install Mysql-devel Library before running//CREATE DATABASE Test (name Vchar, numb int);intMain () {MYSQL conn; intRes; Charsql_insert[ $]; int*name; intnumb; Mysql_res*res_ptr; Mysql_row Result_row; Mysql_field*field; intRow; intcolumn; inti,j; stringname; //Cin >>name;Cin>>numb; CIN>>name; sprintf (Sql_insert,"SELECT distinct * from Cdsgus where ctfid=%d name= '%s '", Numb,name); Mysql_init (&conn); if(Mysql_real_connect (&conn,"localhost","Root","","Test",0, Null,client_found_rows)) {printf ("Connect success!\n"); //mysql_query (&conn, "INSERT into test values (' user ', ' 123456 ')");Res=mysql_query (&Conn, Sql_insert); if(RES)return(0); Else{res_ptr= Mysql_store_result (&conn); if(res_ptr) {column=Mysql_num_fields (RES_PTR); Row= Mysql_num_rows (res_ptr) +1; printf ("there is%lu line \ n", Row); for(i =0; field = Mysql_fetch_field (res_ptr); i++) printf ("%s\t", field->name); printf ("\ n"); } for(i =1; i < row; i++) {Result_row=mysql_fetch_row (RES_PTR);//Here Res_ptr actually corresponds to a cursor for(j =0; J < column; J + +) printf ("%s\t", Result_row[j]); printf ("\ n"); }}} mysql_close (&conn); return 0;}
Implementation of MySQL development environment deployment and database program under Linux