Home > Database > Mysql Tutorial > linux(ubuntu)下C++访问mysql数据库_MySQL

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

WBOY
Release: 2016-06-01 13:11:46
Original
1370 people have browsed it

Ubuntu

  1. Ubuntu安装msyql

    1. 安装mysql数据库

      sudo apt-get install mysql-server
      Copy after login
    2. 安装mysql客户端

      sudo apt-get install mysql-client
      Copy after login
  2. C API的基本类型

    1. MYSQL

      1. 该结构代表1个数据库连接的句柄.

      2. 几乎所有的MySQL函数均使用它.

      3. 不应尝试拷贝MYSQL结构, 不保证这类拷贝结果会有用.

    2. MYSQL_RES

      1. 该结构代表返回行的查询结果

    3. MYSQL_ROW

      1. 这是1行数据的“类型安全”表示。它目前是按照计数字节字符串的数组实施的。

      2. 行是通过调用mysql_fetch_row()获得的。

    4. 官方文档地址

      1. http://dev.mysql.com/doc/refman/5.1/zh/apis.html#c
        Copy after login
  3. 代码

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

      #include <iostream>#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,			//一般为NULL        0			//一般为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//验证    //成功
      Copy after login
    2. 更新表内容

      #include <iostream>#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;}
      Copy after login
    3. 调用存储过程

      #include <iostream>#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 = "call myPorc();";	int ret = mysql_query( &mysql, sql.c_str() );	//      debug info//	cout << mysql_error( &mysql );//	cout << ret << endl;	mysql_close(&mysql);	return 0;}
      Copy after login
    4. 查询数据表的内容

      #include <iostream>#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 = "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;}
      Copy after login
    5. 得到指定数据库test中的所有表

      #include <iostream>#include <string>#include <vector>#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 = "show tables;";		mysql_query( &mysql, sql.c_str() ); 	MYSQL_RES *result = NULL;	result = mysql_store_result( &mysql );	//得到查询出来所有数据记录的数量	vector<string> allTable;	MYSQL_ROW row = mysql_fetch_row( result );	while( NULL != row )	{		allTable.push_back( row[0] );		row = mysql_fetch_row( result );	}	for(vector<string>::const_iterator cit = allTable.begin(); cit != allTable.end(); ++cit )	{		cout << *cit << "/t";	}	cout << endl;		mysql_free_result( result );		mysql_close( &mysql );		return 0;}
      Copy after login



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template