<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[MySQL C API Wrapper]]></title><description><![CDATA[<p>Hallo allerseits,</p>
<p>ich weiß, dass es einen MySQL-Connector für C++ gibt, aber ich bin bisher damit noch nicht richtig warm geworden und habe statt dessen einen Wrapper für den MySQL-Connector für C geschrieben.</p>
<p>mysql.h:</p>
<pre><code class="language-cpp">#ifndef MYSQL_HEADER_INCLUDED
#define MYSQL_HEADER_INCLUDED

#include &quot;mysql/mysql.h&quot;

#include &lt;string&gt;
#include &lt;map&gt;

typedef int MySQLQuery;
typedef MYSQL_RES MySQLRes;
typedef std::map&lt;MySQLQuery,MySQLRes*&gt; MySQLResMap;
typedef MYSQL_ROW MySQLRow;

class MySQL
{
	private:
	MYSQL* m_instance;
	MySQLResMap m_resmap;

	public:
	MySQL();
	~MySQL();

	bool Connect(const std::string&amp; host,unsigned int port,const std::string&amp; user,const std::string&amp; pw,const std::string&amp; db);
	MySQLQuery Query(const std::string&amp; query);
	int NumRows(MySQLQuery query) const;
	int NumCols(MySQLQuery query) const;
	MySQLRow FetchRow(MySQLQuery query) const;
	void FreeResult(MySQLQuery query);
	void Free(MySQLQuery query);
	void Close();
	void Destroy();
};

#endif
</code></pre>
<p>mysql.cpp:</p>
<pre><code class="language-cpp">#include &quot;mysql.h&quot;

#ifdef WIN
#pragma comment(lib,&quot;libmysql.lib&quot;)
#endif

MySQL::MySQL()
{
	m_instance = 0;
}

MySQL::~MySQL()
{
	Close();
}

bool MySQL::Connect(const std::string&amp; host,unsigned int port,const std::string&amp; user,const std::string&amp; pw,const std::string&amp; db)
{
	if (!m_instance) m_instance = mysql_init(0);
	return mysql_real_connect(m_instance,host.c_str(),user.c_str(),pw.c_str(),db.c_str(),port,0,CLIENT_COMPRESS);
}

MySQLQuery MySQL::Query(const std::string&amp; query)
{
	MySQLQuery result = mysql_query(m_instance,query.c_str());
	m_resmap[result] = mysql_store_result(m_instance);
	return result;
}

int MySQL::NumRows(MySQLQuery query) const
{
	MySQLResMap::const_iterator it = m_resmap.find(query);
	if (it == m_resmap.end()) return -1;
	return mysql_num_rows(it-&gt;second);
}

int MySQL::NumCols(MySQLQuery query) const
{
	MySQLResMap::const_iterator it = m_resmap.find(query);
	if (it == m_resmap.end()) return -1;
	return mysql_num_fields(it-&gt;second);
}

MySQLRow MySQL::FetchRow(MySQLQuery query) const
{
	MySQLResMap::const_iterator it = m_resmap.find(query);
	if (it == m_resmap.end()) return 0;
	return (MySQLRow)mysql_fetch_row(it-&gt;second);
}

void MySQL::FreeResult(MySQLQuery query)
{
	MySQLResMap::iterator it = m_resmap.find(query);
	if (it == m_resmap.end()) return;
	mysql_free_result(it-&gt;second);
	m_resmap.erase(it);
}

void MySQL::Free(MySQLQuery query)
{
	FreeResult(query);
}

void MySQL::Close()
{
	if (m_resmap.size())
	{
		MySQLResMap::iterator it;
		for (it = m_resmap.begin(); it != m_resmap.end(); it++) mysql_free_result(it-&gt;second);
		m_resmap.clear();
	}
	if (m_instance) mysql_close(m_instance);
	m_instance = 0;
}

void MySQL::Destroy()
{
	delete this;
}
</code></pre>
<p>Das Problem:</p>
<p>Momentan knallt es leider in allen drei const-Methoden, also zb. bei MySQL::NumRows.</p>
<p>Der Fehler ist ein Speicherzugriffsfehler und tritt bei NumRows beim Aufruf der Funktion mysql_num_rows auf.</p>
<p>Ich habe das Gefühl, dass ich mit der std::map irgendwas falsch mache, aber ich komme nicht drauf ...</p>
<p>LG Max</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/273065/mysql-c-api-wrapper</link><generator>RSS for Node</generator><lastBuildDate>Fri, 28 Aug 2026 11:42:24 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/273065.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 30 Aug 2010 15:16:10 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to MySQL C API Wrapper on Mon, 30 Aug 2010 15:16:10 GMT]]></title><description><![CDATA[<p>Hallo allerseits,</p>
<p>ich weiß, dass es einen MySQL-Connector für C++ gibt, aber ich bin bisher damit noch nicht richtig warm geworden und habe statt dessen einen Wrapper für den MySQL-Connector für C geschrieben.</p>
<p>mysql.h:</p>
<pre><code class="language-cpp">#ifndef MYSQL_HEADER_INCLUDED
#define MYSQL_HEADER_INCLUDED

#include &quot;mysql/mysql.h&quot;

#include &lt;string&gt;
#include &lt;map&gt;

typedef int MySQLQuery;
typedef MYSQL_RES MySQLRes;
typedef std::map&lt;MySQLQuery,MySQLRes*&gt; MySQLResMap;
typedef MYSQL_ROW MySQLRow;

class MySQL
{
	private:
	MYSQL* m_instance;
	MySQLResMap m_resmap;

	public:
	MySQL();
	~MySQL();

	bool Connect(const std::string&amp; host,unsigned int port,const std::string&amp; user,const std::string&amp; pw,const std::string&amp; db);
	MySQLQuery Query(const std::string&amp; query);
	int NumRows(MySQLQuery query) const;
	int NumCols(MySQLQuery query) const;
	MySQLRow FetchRow(MySQLQuery query) const;
	void FreeResult(MySQLQuery query);
	void Free(MySQLQuery query);
	void Close();
	void Destroy();
};

#endif
</code></pre>
<p>mysql.cpp:</p>
<pre><code class="language-cpp">#include &quot;mysql.h&quot;

#ifdef WIN
#pragma comment(lib,&quot;libmysql.lib&quot;)
#endif

MySQL::MySQL()
{
	m_instance = 0;
}

MySQL::~MySQL()
{
	Close();
}

bool MySQL::Connect(const std::string&amp; host,unsigned int port,const std::string&amp; user,const std::string&amp; pw,const std::string&amp; db)
{
	if (!m_instance) m_instance = mysql_init(0);
	return mysql_real_connect(m_instance,host.c_str(),user.c_str(),pw.c_str(),db.c_str(),port,0,CLIENT_COMPRESS);
}

MySQLQuery MySQL::Query(const std::string&amp; query)
{
	MySQLQuery result = mysql_query(m_instance,query.c_str());
	m_resmap[result] = mysql_store_result(m_instance);
	return result;
}

int MySQL::NumRows(MySQLQuery query) const
{
	MySQLResMap::const_iterator it = m_resmap.find(query);
	if (it == m_resmap.end()) return -1;
	return mysql_num_rows(it-&gt;second);
}

int MySQL::NumCols(MySQLQuery query) const
{
	MySQLResMap::const_iterator it = m_resmap.find(query);
	if (it == m_resmap.end()) return -1;
	return mysql_num_fields(it-&gt;second);
}

MySQLRow MySQL::FetchRow(MySQLQuery query) const
{
	MySQLResMap::const_iterator it = m_resmap.find(query);
	if (it == m_resmap.end()) return 0;
	return (MySQLRow)mysql_fetch_row(it-&gt;second);
}

void MySQL::FreeResult(MySQLQuery query)
{
	MySQLResMap::iterator it = m_resmap.find(query);
	if (it == m_resmap.end()) return;
	mysql_free_result(it-&gt;second);
	m_resmap.erase(it);
}

void MySQL::Free(MySQLQuery query)
{
	FreeResult(query);
}

void MySQL::Close()
{
	if (m_resmap.size())
	{
		MySQLResMap::iterator it;
		for (it = m_resmap.begin(); it != m_resmap.end(); it++) mysql_free_result(it-&gt;second);
		m_resmap.clear();
	}
	if (m_instance) mysql_close(m_instance);
	m_instance = 0;
}

void MySQL::Destroy()
{
	delete this;
}
</code></pre>
<p>Das Problem:</p>
<p>Momentan knallt es leider in allen drei const-Methoden, also zb. bei MySQL::NumRows.</p>
<p>Der Fehler ist ein Speicherzugriffsfehler und tritt bei NumRows beim Aufruf der Funktion mysql_num_rows auf.</p>
<p>Ich habe das Gefühl, dass ich mit der std::map irgendwas falsch mache, aber ich komme nicht drauf ...</p>
<p>LG Max</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1946186</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1946186</guid><dc:creator><![CDATA[MaDsTyLe]]></dc:creator><pubDate>Mon, 30 Aug 2010 15:16:10 GMT</pubDate></item><item><title><![CDATA[Reply to MySQL C API Wrapper on Mon, 30 Aug 2010 15:53:41 GMT]]></title><description><![CDATA[<p>Ok, kein Wunder - habs gelöst.</p>
<p>Ich ging fest davon aus, dass mein MySQL::Connect erfolgreich war - leider nein und, weil ich nie abgefragt hatte, ob die Verbindung überhaupt steht, ein unerwartetes Verhalten.</p>
<p>Also werde ich jetzt noch eine bool m_connected; einbauen und fleißig abfragen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":-)"
      alt="🙂"
    /></p>
<p>LG Max</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1946210</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1946210</guid><dc:creator><![CDATA[MaDsTyLe]]></dc:creator><pubDate>Mon, 30 Aug 2010 15:53:41 GMT</pubDate></item></channel></rss>