Search code examples
c++mysqlstringresultset

Solved:Query Results (to CString) in C++ using mysql.h


Im currently trying C++ and building a databaseconnection with mysql c api from oracle. In first place it works fine.
If i query for a col like " select username [...] where id=1" I'll get my result:
testuser.
But if i try "select * from..." I'll get:
1 //correct ID
t // only first letter.

Iam looking arround quite a bit, even here but cant figuer out ho to get the complete result:
1
testuser

As always: Thanks for your time and experience.

Nasten

my Code:

#include "mysql.h"

[...]

sendToLog(_T("Abfrage Starten."));
    MYSQL* m_pConn = ConnectToDatabase();
    ASSERT(m_pConn != nullptr);
    MYSQL_RES *m_pResultSet;
    MYSQL_ROW m_mysqlRow;
    
//Query:
    mysql_query(m_pConn, "SELECT * FROM fplaner.user WHERE ll_UserID=1");
    
//Result speichern
    m_pResultSet = mysql_store_result(m_pConn);
    
//Resultset durchgehen
    ASSERT(m_pResultSet != nullptr);                            
    
int m_llResCount = mysql_num_fields(m_pResultSet);          
    if (m_llResCount == 0)                                      
    {
        sendToLog(_T("ResultSet ist NICHT null aber Leer."));
    }
    else
    {
        while ((m_mysqlRow = mysql_fetch_row(m_pResultSet)))    
        {
            for (int i = 0; i < m_llResCount; i++)
            {
                if (m_mysqlRow == NULL)
                {
                    sendToLog(_T("Ungültiges ResultSet erhalten on COunt: "+i));
                }
                else
                {
                    sendToLog(_T("Gültiges ResultSet erhalten."));
                    CString strTest(*m_mysqlRow[i]);
                      //CString strTest(*m_mysqlRow);--> gives correct name when query with select name                     
                                                          from...
                    m_mysqlRow
                    sendToLog((strTest));
                }
            }
        }
    }


Solution

  • Probleme solved: The resultset is an Char pointer on an pointer. By dereferencing it with * it will only provide the first char if the strong. So without it works fine:
    Change "CString strTest(*m_mysqlRow[i]);" to "CString strTest(m_mysqlRow[i]);" .
    and it will work as intended.