ubuntu 12.04x下c++l连接MySQL入门,

来源:互联网 发布:金山数据恢复 收费 编辑:程序博客网 时间:2024/06/03 05:06

内容如下

1: 安装mysql

2:    安装mysql 的C++ connector

3:    最小的实现可能要到的命令,

4: 编译方式,

5: 一个完整的例子。

6: 官方文档链接;

一:安装MySQL;

   1.1,方式1:

                 去官网下载原文件啊,还有文档,文档里有很详细的说明。原文件地址:http://dev.mysql.com/downloads/mysql/。,参考文件地址http://dev.mysql.com/doc/;

   1.2, 方式2

                 这个是转载的,看不懂参考文档的话看这里:

                 卸载mysql


第一步

 1
 sudo apt-get autoremove --purge mysql-server-5.0
2
sudo apt-get remove mysql-server
3
sudo apt-get autoremove mysql-server
4
sudo apt-get remove mysql-common (非常重要)
第二步:清理残留数据
1
dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P
第三步:安装 mysql

1
sudo apt-get install mysql-server
2
sudo apt-get install mysql-client
3
sudo apt-get install php5-mysql(安装php5-mysql 是将php和mysql连接起来 )
一旦安装完成,MySQL 服务器应该自动启动。您可以在终端提示符后运行以下命令来检查 MySQL 服务器是否正在运行:

1
sudo netstat -tap | grep mysql
当您运行该命令时,您可以看到类似下面的行:


tcp 0 0 localhost.localdomain:mysql *:* LISTEN -


如果服务器不能正常运行,您可以通过下列命令启动它:

1
sudo /etc/init.d/mysql restart
第四步:配置管理员密码
进入mysql

1
$mysql -uroot -p 管理员密码
配置 MySQL 的管理员密码:

1
sudo mysqladmin -u root password newpassword;


二 :安装mysql c++ connector:

    安装connector, 要先安装boost库,终端命令输入:sudo apt-get install libboost-dev libboost-doc bcp libboost-*;这个可能提示选择了很多包,但是没有真正的安装,可以选择只安装boost库,  sudo  apt-get install libboost-dev;

    然后安装mysqlclient库:  sudo apt-get install  libmysqlclient-dev;

    然后安装mysqlcppconn库:sudo apt-get install  libmysqlcppconn-dev;

   最后还要下载connector-c++的必要头文件:http://dev.mysql.com/downloads/connector/cpp/。下载后解压缩,把里边的头文件和cppconn(整个文件夹)文件夹 放到/usr/include中,把lib文件夹里边的库文件复制到/usr/lib中。就可以了。其实下载的connector-c++文件中有安装说明,但是我总是弄不好。。。。。



三:  最小实现要用到的类,命令;sql::mysql::MySQL_Driver *driver;   //驱动加载
                                                       sql::Connection *con;               //连接
                                                       sql::Statement *stmt;               //报表


                                                       driver = sql::mysql::get_mysql_driver_instance();   //获取驱动
                                                       con = driver->connect("tcp://127.0.0.1:3306", "user", "password"); //登陆


                                                         stmt = con->createStatement();    //返回报表
                                                         stmt->execute("USE " EXAMPLE_DB);  //执行查询语句
                                                         stmt->execute("DROP TABLE IF EXISTS test");  //
                                                         stmt->execute("CREATE TABLE test(id INT, label CHAR(1))");
                                                         stmt->execute("INSERT INTO test(id, label) VALUES (1, 'a')");

四 :编译的使用,要包含使用到的头文件的目录:-I/usr/iclude/cppconn;注意,第一个字母是大写的i;不是小写的l;

                               要包含用到的库: -lmysqlcppconn, 可能还要包含库: -lmysqlclient;

                               多线程的时候,记得加上线程库: -pthread;

    和起来像这样: g++ -g  -std=c++11 -o main   main.cpp  -lmysqlcppconn   -lmysqlclient  -I/usr/include/cppconn;



五:   一个完整的例子:这是官方文档第八章第第一个例子加上第三个例子的内容;http://dev.mysql.com/doc/connector-cpp/en/index.html,

       文档下载地址:http://dev.mysql.com/doc/index-connectors.html

建立一个 cpp 文件,比如是main.cpp, 内容如下:



#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <stdexcept>
/* uncomment for applications that use vectors */
/*#include <vector>*/


#include "mysql_connection.h"


#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>


#define EXAMPLE_HOST "localhost"
#define EXAMPLE_USER "worlduser"
#define EXAMPLE_PASS "worldpass"
#define EXAMPLE_DB "world"


using namespace std;


int main(int argc, const char **argv)
{
  string url(argc >= 2 ? argv[1] : EXAMPLE_HOST);
  const string user(argc >= 3 ? argv[2] : EXAMPLE_USER);
  const string pass(argc >= 4 ? argv[3] : EXAMPLE_PASS);
  const string database(argc >= 5 ? argv[4] : EXAMPLE_DB);


  cout << "Connector/C++ tutorial framework..." << endl;
  cout << endl;


  try {
        sql::Driver* driver = get_driver_instance();
std::auto_ptr<sql::Connection> con(driver->connect(url, user, pass));
 std::cout<<"hahaha\n";
con->setSchema(database);
std::auto_ptr<sql::Statement> stmt(con->createStatement());


stmt->execute("CALL get_pop('Uganda', @pop)");


std::auto_ptr<sql::ResultSet> res(stmt->executeQuery("SELECT @pop AS _reply"));
while (res->next())
  cout << "Population of Uganda: " << res->getString("_reply") << endl;


stmt->execute("CALL get_pop_continent('Asia', @pop)");


res.reset(stmt->executeQuery("SELECT @pop AS _reply"));
while (res->next())
  cout << "Population of Asia: " << res->getString("_reply") << endl;


stmt->execute("CALL get_pop_world(@pop)");


res.reset(stmt->executeQuery("SELECT @pop AS _reply"));
while (res->next())
  cout << "Population of World: " << res->getString("_reply") << endl;


  } catch (sql::SQLException &e) {
    /*
      MySQL Connector/C++ throws three different exceptions:


      - sql::MethodNotImplementedException (derived from sql::SQLException)
      - sql::InvalidArgumentException (derived from sql::SQLException)
      - sql::SQLException (derived from std::runtime_error)
    */
    cout << "# ERR: SQLException in " << __FILE__;
    cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
    /* what() (derived from std::runtime_error) fetches error message */
    cout << "# ERR: " << e.what();
    cout << " (MySQL error code: " << e.getErrorCode();
    cout << ", SQLState: " << e.getSQLState() << " )" << endl;


    return EXIT_FAILURE;
  }


  cout << "Done." << endl;
  return EXIT_SUCCESS;
}


编译命令如下:g++ -o main   main.cpp   -I/usr/include/cppconn -lmysqlcppconn  




                                                                                           


     

0 0
原创粉丝点击