-
Insert (delete) a piece of data into the table
# Include
# Include
Using namespace std; int main (int argc, char * argv []) {// prepare the mysql access structure MYSQL mysql; mysql_init (& mysql); mysql_real_connect (& mysql, "192.168.16.114", // IP address of the database to be accessed "root", // username "root", // password "test", // 3306 of the database to be accessed, // The database port is NULL, // generally NULL 0 // generally 0); // insert string SQL = "insert into student value (1, 'jput ', 24, 'gzjd ') "; // delete // string SQL =" delete from student where id = 33 "; // execute the SQL statement mysql_query (& mysql, SQL. c_str (); // Close the database connection mysql_close (& mysql); return 0 ;}// compile // g ++ file. cpp-o target-lmysqlclient // execute //. /target // verify // success
-
Update table content
#include
#include
using namespace std;int main(int argc, char* argv[]){MYSQL mysql;mysql_init( &mysql );mysql_real_connect(&mysql,"192.168.16.114","root","root","test",3306,NULL,0);string sql = "update student set name = 'pj' where id = 2";mysql_query( &mysql, sql.c_str() ); mysql_close(&mysql); return 0;}
-
Call stored procedure
#include
#include
using namespace std;int main(int argc, char* argv[]){MYSQL mysql;mysql_init( &mysql );mysql_real_connect(&mysql,"192.168.16.114","root","root","test",3306,NULL,0);string sql = "call myPorc();";int ret = mysql_query( &mysql, sql.c_str() );// debug info//cout << mysql_error( &mysql );//cout << ret << endl;mysql_close(&mysql);return 0;}
-
Query data table content
# Include
# Include
Using namespace std; int main (int argc, char * argv []) {MYSQL mysql; mysql_init (& mysql); mysql_real_connect (& mysql, "192.168.16.114", "root ", "root", "test", 3306, NULL, 0); string SQL = "select * from student"; mysql_query (& mysql, SQL. c_str (); MYSQL_RES * result = NULL; result = mysql_store_result (& mysql); // you can obtain the number of entries of all data queried. int row_count = mysql_num_rows (result ); cout <"all data number:" <row_count <end L; // Obtain the number of fields and the field name int field_count = mysql_num_fields (result); cout <"field count:" <field_count <endl; // obtain the names of all fields MYSQL_FIELD * field = NULL; for (int I = 0; I <field_count; ++ I) {field = mysql_fetch_field_direct (result, I ); cout <field-> name <"/t" ;}cout <endl; // displays all data in the table MYSQL_ROW row = NULL; row = mysql_fetch_row (result ); while (NULL! = Row) {for (int I = 0; I <field_count; ++ I) {cout <row [I] <"/t" ;}cout <endl; row = mysql_fetch_row (result);} mysql_free_result (result); mysql_close (& mysql); return 0 ;}
-
Obtain all tables in the specified database test.
# Include
# Include
# Include
# Include
Using namespace std; int main (int argc, char * argv []) {// define a database connection handle MYSQL; // initialize the data handle mysql_init (& mysql ); // connect to the database mysql_real_connect (& mysql, "192.168.16.114", "root", "root", "test", 3306, NULL, 0 ); // query the database string SQL = "show tables;"; mysql_query (& mysql, SQL. c_str (); MYSQL_RES * result = NULL; result = mysql_store_result (& mysql); // Obtain the number of all data records queried by vector
AllTable; MYSQL_ROW row = mysql_fetch_row (result); while (NULL! = Row) {allTable. push_back (row [0]); row = mysql_fetch_row (result);} for (vector
: Const_iterator cit = allTable. begin (); cit! = AllTable. end (); ++ cit) {cout <* cit <"/t" ;}cout <endl; mysql_free_result (result); mysql_close (& mysql ); return 0 ;}