How to fix 'error: invalid conversion from ‘int’ to ‘const char*’ [-fpermissive]' in C++?

How to fix 'error: invalid conversion from ‘int’ to ‘const char*’ [-fpermissive]' in C++?

我尝试连接 mysql 和 c++。 这是尝试将输入的值放入数据库的过程。 g++编译出错

我在 visual studio 2017 和 CentOS7 中使用 c++98 进行编码。 Mysql 使用的连接器是 Mysql++ ver.8.0.

#include "[~ PATH]/mysql++/include/mysql++/cmdline.h"
#include "[~ PATH]/mysql++/include/mysql++/mysql++.h"
#include "[~ PATH]/mysql++/examples/printdata.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <stdio.h>
#include <cstdlib>

using namespace std;

int main(int argc, char *argv[])
{
        char buffer[256];
        string name;
        string id;
        string password;
        string department;

        cout << ">> Sign Up <<\n" << endl;
        cout << "1.Name: "; cin >> name;
        cout << "2.ID: "; cin >> id;
        cout << "3.PASSWORD: "; cin >> pw;
        cout << "4.Department: "; cin >> dpt;

        mysqlpp::Connection conn(false);
        if (conn.connect("member_list", "localhost", "user", "password1234"))
        {
                **mysqlpp::Query query 
                 = conn.query(sprintf(buffer,"INSERT INTO signup_member (name,id,password,department) 
                   VALUES ('%s','%s','%s','%s')",name.c_str(),id.c_str(),password.c_str(),department.c_str()));**
                if (mysqlpp::StoreQueryResult res = query.store()) {
                        cout << "Sign up SUCCESS!" << endl;
                }
                else {
                        cerr << "Sign up FAIL. Try again." << '\n' << query.error() << endl;
                        return 1;
                }
                return 0;
        }
        else {
                cerr << "DB connection failed: " << conn.error() << endl;
        }
}

我搜索的时候说用'sprintf'把值存到buffer里,然后存到DB里。我希望它运作良好,但事实并非如此。 sign_up.cpp:30:175: error: invalid conversion from ‘int’ to ‘const char*’ [-fpermissive]

sprintf return 一个代表

的整数

On success, the total number of characters written is returned. This count does not include the additional null-character automatically appended at the end of the string. On failure, a negative number is returned.

& 不是你在其中写入数据的 char 数组,所以基本上你应该这样做:

sprintf(buffer,"INSERT INTO signup_member (name,id,password,department) 
                   VALUES ('%s','%s','%s','%s')",name.c_str(),id.c_str(),password.c_str(),department.c_str());
mysqlpp::Query query 
                 = conn.query(buffer);