linux(ubuntu)下C++访问mysql数据库

Ubuntu安装msyql

安装mysql数据库

sudo apt-get install mysql-server

安装mysql客户端

sudo apt-get install mysql-client


C API的基本类型
MYSQL
该结构代表1个数据库连接的句柄.
几乎所有的MySQL函数均使用它.
不应尝试拷贝MYSQL结构, 不保证这类拷贝结果会有用.
MYSQL_RES
该结构代表返回行的查询结果
MYSQL_ROW
这是1行数据的“类型安全”表示。它目前是按照计数字节字符串的数组实施的。
行是通过调用mysql_fetch_row()获得的。
官方文档地址
http://dev.mysql.com/doc/refman/5.1/zh/apis.html

向表中插入(删除)一条数据

#include#include <mysql/mysql.h>using namespace std;int main(int argc, char* argv[]){//准备mysql的访问结构MYSQL mysql;mysql_init( &mysql );mysql_real_connect(&mysql,“192.168.16.114”, //要访问数据库的IP地址“root”, //用户名“root”, //密码“test”, //要访问的数据库3306, //该数据库的端口NULL, //一般为NULL0 //一般为0);//插入string sql = “insert into student value(1, ‘jp’, 24, ‘gzjd’)”;//删除//string sql = “delete from student where id = 33”;//执行sql语句mysql_query( &mysql, sql.c_str() );//关闭数据库连接mysql_close( &mysql );return 0;}//编译//g++ file.cpp -o target -lmysqlclient//执行//./target//验证//成功


更新表内容

#include#include <mysql/mysql.h>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;}


调用存储过程
#include#include &lt;mysql/mysql.h&gt;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;}


查询数据表的内容

#include#include &lt;mysql/mysql.h&gt;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 );//得到查询出来所有数据的条数int row_count = mysql_num_rows( result );cout << “all data number: ” << row_count << endl;//得到字段的个数和字段的名字int field_count = mysql_num_fields( result );cout << “field count : ” << field_count << endl;//得到所有字段的名字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;//显示表中的所有数据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;}

得到指定数据库test中的所有表:

#include#include#include#include &lt;mysql/mysql.h&gt;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 = “show tables;”;mysql_query( &mysql, sql.c_str() );MYSQL_RES *result = NULL;result = mysql_store_result( &mysql );//得到查询出来所有数据记录的数量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;}

发表评论 / Comment

用心评论~