首页 / C++ / Linux 上c++ 访问mysql
Linux 上c++ 访问mysql
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Linux 上c++ 访问mysql,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2231字,纯文字阅读大概需要4分钟。
内容图文
1.安装mysql客户端
用命令:
yum install -y mysql-server mysql mysql-devel
此命令包含了安装客户端和服务器
2.访问myslq
在命令行输入:
mysql -h192.168.0.36 -uroot -p123456
出现:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 45099
Server version: 5.5.5-10.0.12-MariaDB MariaDB Server
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners
代表Mysql客户端安装成功!
3.代码访问
#include <iostream> #include <mysql/mysql.h> #include <string> usingnamespace std; int main() { MYSQL mysql; mysql_init(&mysql); mysql_real_connect(&mysql, "192.168.0.36", "root", "123456", "uc", 3306, NULL, 0); string sql = "insert into sysuser (orgid,useraccount) values (1, ‘java1‘);"; mysql_query(&mysql, sql.c_str()); mysql_close(&mysql); return0; }
编译:
g++ -o test main.cpp -lmysqlclient -I/usr/include/mysql/ -L/usr/lib64/mysql
注意:-L的路径在lib64下面
4.查询实例代码
#include <iostream> #include <string> #include <mysql/mysql.h> usingnamespace std; int main() { MYSQL mysql; MYSQL_RES *result = NULL; MYSQL_FIELD *field = NULL; mysql_init(&mysql); mysql_real_connect(&mysql, "192.168.0.36", "root", "123456","uc", 3306,NULL, 0); // mysql_real_connect(&mysql, "localhost", "root", "root", "test", 3306, NULL, 0);string sql = "select id,useraccount from sysuser;"; mysql_query(&mysql, sql.c_str()); result = mysql_store_result(&mysql); int rowcount = mysql_num_rows(result); cout <<"rowcount:"<< rowcount << endl; int fieldcount = mysql_num_fields(result); cout <<"fieldcount:"<< fieldcount << endl; for(int i = 0; i < fieldcount; i++) { field = mysql_fetch_field_direct(result,i); cout << "field name:"<<field->name << "\t\t"; } cout << endl; MYSQL_ROW row = NULL; row = mysql_fetch_row(result); while(NULL != row) { for(int i=0;i<fieldcount; i++) { cout << "row"<<i<<":"<<row[i] << "\t\t"; } cout << endl; row = mysql_fetch_row(result); } mysql_close(&mysql); return0; }
原文:http://www.cnblogs.com/spplus/p/5421373.html
内容总结
以上是互联网集市为您收集整理的Linux 上c++ 访问mysql全部内容,希望文章能够帮你解决Linux 上c++ 访问mysql所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。