Home > Database > Mysql Tutorial > body text

Linux下通过C++语言代码来操作MySQL数据库

WBOY
Release: 2016-06-07 16:53:32
Original
991 people have browsed it

代码:#include lt;cstdlibgt;#include lt;stringgt;#include quot;/usr/local/include/mysql/mysql.hquot; //安装的mys

代码:

#include
#include
#include "/usr/local/include/mysql/mysql.h"   //安装的mysql的头文件所在的位置
using namespace std;
string host="数据库地址";
string user="用户名";
string pwd="密码";
string dbname="数据库";
string sql="查询语句";
unsigned int port=3309;#端口号
int status;
int main(){
MYSQL *mysql;
mysql=mysql_init(0);
MYSQL_RES *result;
MYSQL_ROW row;
if(mysql_real_connect(mysql,host.c_str(),user.c_str(),pwd.c_str(),dbname.c_str(),port,NULL,CLIENT_FOUND_ROWS)==NULL){
cout return EXIT_FAILURE;
}else{
cout }
mysql_set_character_set(mysql,"gbk");
status=mysql_query(mysql,sql.c_str());
if(status !=0 ){
cout }
cout result=mysql_store_result(mysql);
while(row=mysql_fetch_row(result)){
cout }
mysql_free_result(result);
mysql_close(mysql);
}

编译:

g++ -o test test.cpp -lmysqlclient -I/usr/local/include/mysql/ -L/usr/local/lib/mysql

后面的那些用来指定mysql安装的时候包含路径和库文件路径,具体与你机器上的mysql安装的路径有关。

linux

Related labels:
c++
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