首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我可以在控制台应用程序中连接到数据库,但在该应用程序的快速can版本中不能连接到数据库。

我可以在控制台应用程序中连接到数据库,但在该应用程序的快速can版本中不能连接到数据库。
EN

Stack Overflow用户
提问于 2019-03-14 05:46:05
回答 1查看 47关注 0票数 0

我正在制作一个程序,将客户信息存储在数据库中。我正在使用mariadb的c++客户端马里亚德普。因为我是一个网络世界的新手,我第一次制作了一个控制台应用程序。然后我试着做一个快速的版本的应用程序。

这是代码:

代码语言:javascript
复制
#include <fastcgi++/request.hpp>
#include <fastcgi++/manager.hpp>

//for json
#include <nlohmann/json.hpp>

//for database connection
#include <mariadb++/account.hpp>
#include <mariadb++/connection.hpp>

//for (cryptogrphic) password hashing
//#include <bcrypt/BCrypt.hpp>

//to get mariadb's password
#include <fstream>

class AddUser : public Fastcgipp::Request<char>
{
    bool response(){
        out << "Content-Type: application/json; charset=utf-8\r\n\r\n";

        nlohmann::json json;

        //iterators declared in order to check if the required variables are given
        auto it_name = environment().gets.find("name"),
             it_address = environment().gets.find("address"),
             it_phoneNumber = environment().gets.find("phoneNumber"),
             it_password = environment().gets.find("password");

        /**
         * Returning in case of an error because the following checks are preconditions and it
         * does not make sense to continue processing if these are not met
        */

        if(it_name == environment().gets.end() || it_address == environment().gets.end() ||
           it_phoneNumber == environment().gets.end() || it_password == environment().gets.end()){
            //give an error if anything is missing                
            json["error"] = "Unkown parameters";
            out << json;
            return true;

        }

        //otherwise get the name, address, etc
        std::string name = it_name->second,
                    address = it_address->second,
                    phoneNumber = it_phoneNumber->second,
                    password = it_password->second;

        //check if the phone number is 10 digits long
        if(phoneNumber.size() != 10){
            json["error"] = "Invalid phoneNumber";
            out << json;
            return true;
        }

        //check if the phone number only contains digits and not letters
        for(const char c : phoneNumber){
            if(c < '0' && c > '9'){
                json["error"] = "Invalid phoneNumber";
                out << json;
                return true;
            }
        }

        //get the password
        std::ifstream file("/home/Hemil/add-user.txt");
        std::string acc_password;
        std::getline(file, acc_password);

        mariadb::account_ref acc = mariadb::account::create("localhost", "add-user", acc_password, "Customers");
        mariadb::connection_ref con = mariadb::connection::create(acc);

        /**
        //input the credentials into the database
        mariadb::statement_ref smt = con->create_statement("insert into Info(Name, Address, PhoneNumber, Password) values(?, ?, ?, ?)");
        smt->set_string(0, name);
        smt->set_string(1, address);
        smt->set_string(2, phoneNumber);
        //to hash the password
        //BCrypt bcrypt;
        //smt->set_string(3, bcrypt.generateHash(password));
        smt->set_string(3, password);

        /**
         * Not returning in case of an error because these are errors on our side. 
        */

        /**
        //execute returns the number of rows affected which should be one because we inserted one row
        if(smt->execute() == 1){
            //get the customer id
            auto getCustomerId = con->create_statement("select LAST_INSERT_ID()");
            auto result = getCustomerId->query();

            if(result->next()){
                uint64_t CustomerId = result->get_unsigned64(0);

                json["error"] = nullptr;
                json["CustomerID"] = CustomerId;
            } else {
                json["error"] = "Could not get the Customer ID";
            }

        } else {
            json["error"] = "Could not insert into table";
        }
        */
        out << json;
        return true;
    }
};

int main(){
    Fastcgipp::Manager<AddUser> manager;
    manager.setupSignals();
    manager.listen();
    manager.start();
    manager.join();
}

有个奇怪的问题。在当前情况下( insert注释掉了),它不会像应该输出null那样产生错误,因为nlohnamnn::json从未被分配(null是默认的)。如果我没有注释那个插入,我会得到500个内部服务器错误。这是error_log:

代码语言:javascript
复制
MariaDB Error(1045): Access denied for user 'add-user'@'localhost' (using password: NO)
In function: connect
In file /home/Hemil/Downloads/mariadbpp/src/connection.cpp
On line 109
terminate called after throwing an instance of 'mariadb::exception::connection'
  what():  Access denied for user 'add-user'@'localhost' (using password: NO)
[Thu Mar 14 11:08:38.209250 2019] [fcgid:warn] [pid 8302:tid 139977899874048] [client 127.0.0.1:42144] mod_fcgid: error reading data, FastCGI server closed connection
[Thu Mar 14 11:08:38.209375 2019] [core:error] [pid 8302:tid 139977899874048] [client 127.0.0.1:42144] End of script output before headers: add-user.fcg
[Thu Mar 14 11:08:40.937823 2019] [fcgid:error] [pid 8300:tid 139978589235456] mod_fcgid: process /var/www/cgi-bin/add-user.fcg(8517) exit(communication error), get signal 6, possible coredump generated

有趣的是我用的是密码。

我已经禁用了从网络访问数据库。我认为这是因为Mariadb将localhost计算为一个被禁用的远程请求。

P.S:,我知道把密码传递出去不是个好主意。我应该用邮报的。我会改的。

EN

回答 1

Stack Overflow用户

发布于 2019-03-15 04:16:39

有件奇怪的事。我发现我无法从文件中获得密码。密码是空字符串。这就是MariaDB抱怨没有使用密码的原因。我通过在create方法中输入密码来验证这一点,它可以工作。

但是将密码放入二进制文件并不是一个好的安全实践,所以,我正在寻找为什么我不能打开文件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55155708

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档