简体   繁体   English

使用OTL库将数据从C ++程序插入mysql表

[英]Insert data into mysql table from c++ program using OTL library

I want to insert int and string value into mysql table from c++ program. 我想将intstring值从c ++程序插入mysql表。

for (std::map < int, std::vector < std::string > >::iterator hit = three_highest.begin(); hit != three_highest.end(); ++hit) {
    //std::cout << hit->first << ":";

    for (std::vector < std::string >::iterator vit = (*hit).second.begin(); vit != (*hit).second.end(); vit++) {
        std::cout << hit->first << ":";
        std::cout << *vit << "\n";

I want to insert hit->first and *vit . 我想插入hit->first*vit

  1. I saw example here : OTL Example for mysql . 我在这里看到了示例: OTL mysql示例 Will it work for me? 对我有用吗?

    And how to set OTL environment in my system? 以及如何在系统中设置OTL环境? Do I need to install it or what? 我需要安装它还是什么?

  2. Here is very simple example in another way. 这是另一种非常简单的示例。 But it directly include <mysql.h> Which I could not find in my system. 但是它直接include <mysql.h>这在我的系统中找不到。 What environment it using? 它使用什么环境?

I am using Linux - Ubuntu environment. 我正在使用Linux-Ubuntu环境。

Here I got how to connect c++ with mysql: 在这里,我了解了如何将c ++与mysql连接:

first compiled the c++ connector library from source but after a while I thought maybe I did something wrong so I then just used apt to get it with: 首先从源代码编译了c ++连接器库,但过了一会儿我以为我做错了什么,所以我就使用apt来获取它:

sudo apt-get install  libmysqlcppconn-dev

So I have now had this problem for a week now and I became very frustrated with it as well. 所以我现在已经有一个星期的问题了,对此我也感到非常沮丧。 I just now was able to finally build a program that does nothing except login to mysql and I literally squealed with joy. 我现在终于能够构建一个程序,除了登录mysql以外什么都不做,而我确实高兴极了。 Here is what I have and I hope it helps. 这是我所拥有的,希望对您有所帮助。

I first compiled the c++ connector library from source but after a while I thought maybe I did something wrong so I then just used apt to get it with: 我首先从源代码编译了c ++连接器库,但过了一会儿我以为可能是我做错了,所以我只用apt来获取它:

sudo apt-get install libmysqlcppconn-dev 须藤apt-get install libmysqlcppconn-dev

And here is my simple tester source file "tester.cpp" 这是我简单的测试器源文件“ tester.cpp”

#include <stdlib.h>
#include <iostream>
#include <mysql_connection.h>
#include <driver.h>
#include <exception.h>
#include <resultset.h>
#include <statement.h>

using namespace sql;
int main(void){
  sql::Driver *driver;
  sql::Connection *con;

  driver = get_driver_instance();
  con = driver->connect("tcp://127.0.0.1:3306","root","YOURPASSWORD");

  return 0;
}

And finally g++ compile command: 最后是g ++编译命令:

sudo g++ -Wall -I/usr/include/cppconn -o testapp tester.cpp -L/usr/lib -lmysqlcppconn

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM