• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

C++ db->escapeString( ... ) returns '' instead of existing value

Snavy

Bakasta
Senator
Joined
Apr 1, 2012
Messages
1,249
Solutions
71
Reaction score
621
Location
Hell
I have compiled 0.4 sources (possibly modified) for a friend and it compiled successfully.
when I started the TFS an error occured saying that database in config was empty:
The database you have specified in config.lua is empty, please import schemas/<engine>.sql to the database.

I have triple checked the database information in config.lua and that was OK.
then I looked into the sources and traced it to databasemanager.cpp
C++:
bool DatabaseManager::isDatabaseSetup()
{
        Database* db = Database::getInstance();
        switch(db->getDatabaseEngine())
        {
                case DATABASE_ENGINE_MYSQL:
                {

                        DBQuery query;

                        query << "SELECT `TABLE_NAME` FROM `information_schema`.`tables` WHERE `TABLE_SCHEMA` = " << db->escapeString(g_config.getString(ConfigManager::SQL_DB)) << ";";

                        // DEBUG LINES

                        // THE FOLLOWING LINE GIVES:
                        // SELECT TABLE_NAME FROM information_schema.`tables` WHERE TABLE_SCHEMA = '';
                        std::cout << query.str() << std::endl;

                        // This line gives the correct value of sqlDatabase variable which is "evolera" (in config)
                        std::cout << "sqlDatabase -- " << g_config.getString(ConfigManager::SQL_DB) << std::endl;

                        // This line gives '' (empty string)
                        std::cout << "sqlDatabase -- " << db->escapeString(g_config.getString(ConfigManager::SQL_DB)) << std::endl;

                        DBResult* result;
                        if(!(result = db->storeQuery(query.str())))
                                return false;
                        result->free();
                        return true;
                }

                case DATABASE_ENGINE_SQLITE:
                        //a pre-setup sqlite database is already included
                        return true;

                case DATABASE_ENGINE_POSTGRESQL:
                        //TODO: PostgreSQL support
                        return true;
                default: break;
        }
        return false;
}

after adding debug code I noticed db->escapeString was returning an empty string instead of the expected value.
In the databasemysql.h header file I found that it was using this escapeBlob method :

databasemysql.cpp
C++:
std::string DatabaseMySQL::escapeBlob(const char* s, uint32_t length)
{
        if(!m_connected || *s == '\0')
                return "''";

        char* output = new char[(length << 1) + 1];
        mysql_real_escape_string(m_handle, output, s, length);

        std::string res = "'";
        res += output;
        res += "'";

        delete[] output;
        return res;
}

Im not sure why escapeBlob is returning '' instead of the expected value ...

OS: Linux, Debian 8
 
Last edited:
are you sure server is connected to db? try cout m_connected

I believe it is; I have tried printint m_connected and got '1' as a result (which i belive is "true").

What I don't understand here is why I am not able to use std::cout inside the escapeString function that is being called. I have tried printing in both `escapeString`and escapeBlob.

I have also splitted if(!m_connected || *s == '\0') to test both parts but no luck. It is as if the function is not being called. What could be wrong?
 
UPDATE; I have tried executing the query manually by typing it directly in the source code. It returns 0 (false).
When I tested it on phpmyadmin, the result was true
 
Back
Top