<?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[Übergabe von string anstelle von char* an Funktion (Einsatz von SQLite3)]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe folgendes Problem.<br />
Der unten genannte Code funktioniert. Ich erhalte jedoch Warnungen das eine veraltete Konvertierung von Zeichenketten stattfindet (Zeile 11 und 12 in der Main.cpp).<br />
Also dachte ich mir ich nehme dann einfach statt char* ein string.<br />
Hat leider nicht geklappt (ganz viele böse Fehlermeldungen) <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>Daher meine Frage:<br />
Wie kriege ich den Compiler dazu sich nicht mehr zu beschweren? Ich mag keine Warnungen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
<p>Meine Main:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &quot;sqlite/sqlite3.h&quot;
#include &quot;database.h&quot;

using namespace std;
int main()
{
    Database* db;
    db = new Database (&quot;test.db&quot;);
    db-&gt;query(&quot;INSERT INTO t_user(a_username,a_password,a_salt) VALUES ('Username','Password','Salt');&quot;);
    db-&gt;close();

    return 0;
}
</code></pre>
<p>Mein Header:</p>
<pre><code>#ifndef DATABASE_H_INCLUDED
#define DATABASE_H_INCLUDED

#include &lt;string&gt;
#include &lt;vector&gt;
#include &lt;string&gt;
#include &lt;sqlite3.h&gt;

using namespace std;

class Database
{
public:
    Database(char* filename);
    ~Database();

    bool open(char* filename);
    vector&lt;vector&lt;string&gt; &gt; query(char* query);
    void close();

private:
    sqlite3 *database;
};

#endif // DATABASE_H_INCLUDED
</code></pre>
<p>Meine Implementierung der Klasse Database:</p>
<pre><code>#include &quot;database.h&quot;
#include &lt;string&gt;
#include &lt;iostream&gt;

Database::Database(char* filename)
{
    database = NULL;
    open(filename);
}

Database::~Database()
{
}

bool Database::open(char* filename)
{
    if(sqlite3_open(filename, &amp;database) == SQLITE_OK)
        return true;

    return false;
}

vector&lt;vector&lt;string&gt; &gt; Database::query(char* query)
{
    sqlite3_stmt *statement;
    vector&lt;vector&lt;string&gt; &gt; results;

    if(sqlite3_prepare_v2(database, query, -1, &amp;statement, 0) == SQLITE_OK)
    {
        int cols = sqlite3_column_count(statement);
        int result = 0;
        while(true)
        {
            result = sqlite3_step(statement);

            if(result == SQLITE_ROW)
            {
                vector&lt;string&gt; values;
                for(int col = 0; col &lt; cols; col++)
                {
                    values.push_back((char*)sqlite3_column_text(statement, col));
                }
                results.push_back(values);
            }
            else
            {
                break;
            }
        }

        sqlite3_finalize(statement);
    }

    string error = sqlite3_errmsg(database);
    if(error != &quot;not an error&quot;) cout &lt;&lt; query &lt;&lt; &quot; &quot; &lt;&lt; error &lt;&lt; endl;

    return results;
}

void Database::close()
{
    sqlite3_close(database);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/319909/übergabe-von-string-anstelle-von-char-an-funktion-einsatz-von-sqlite3</link><generator>RSS for Node</generator><lastBuildDate>Fri, 24 Jul 2026 17:24:18 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/319909.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 08 Sep 2013 15:46:24 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Übergabe von string anstelle von char* an Funktion (Einsatz von SQLite3) on Sun, 08 Sep 2013 15:46:24 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe folgendes Problem.<br />
Der unten genannte Code funktioniert. Ich erhalte jedoch Warnungen das eine veraltete Konvertierung von Zeichenketten stattfindet (Zeile 11 und 12 in der Main.cpp).<br />
Also dachte ich mir ich nehme dann einfach statt char* ein string.<br />
Hat leider nicht geklappt (ganz viele böse Fehlermeldungen) <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>Daher meine Frage:<br />
Wie kriege ich den Compiler dazu sich nicht mehr zu beschweren? Ich mag keine Warnungen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
<p>Meine Main:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &quot;sqlite/sqlite3.h&quot;
#include &quot;database.h&quot;

using namespace std;
int main()
{
    Database* db;
    db = new Database (&quot;test.db&quot;);
    db-&gt;query(&quot;INSERT INTO t_user(a_username,a_password,a_salt) VALUES ('Username','Password','Salt');&quot;);
    db-&gt;close();

    return 0;
}
</code></pre>
<p>Mein Header:</p>
<pre><code>#ifndef DATABASE_H_INCLUDED
#define DATABASE_H_INCLUDED

#include &lt;string&gt;
#include &lt;vector&gt;
#include &lt;string&gt;
#include &lt;sqlite3.h&gt;

using namespace std;

class Database
{
public:
    Database(char* filename);
    ~Database();

    bool open(char* filename);
    vector&lt;vector&lt;string&gt; &gt; query(char* query);
    void close();

private:
    sqlite3 *database;
};

#endif // DATABASE_H_INCLUDED
</code></pre>
<p>Meine Implementierung der Klasse Database:</p>
<pre><code>#include &quot;database.h&quot;
#include &lt;string&gt;
#include &lt;iostream&gt;

Database::Database(char* filename)
{
    database = NULL;
    open(filename);
}

Database::~Database()
{
}

bool Database::open(char* filename)
{
    if(sqlite3_open(filename, &amp;database) == SQLITE_OK)
        return true;

    return false;
}

vector&lt;vector&lt;string&gt; &gt; Database::query(char* query)
{
    sqlite3_stmt *statement;
    vector&lt;vector&lt;string&gt; &gt; results;

    if(sqlite3_prepare_v2(database, query, -1, &amp;statement, 0) == SQLITE_OK)
    {
        int cols = sqlite3_column_count(statement);
        int result = 0;
        while(true)
        {
            result = sqlite3_step(statement);

            if(result == SQLITE_ROW)
            {
                vector&lt;string&gt; values;
                for(int col = 0; col &lt; cols; col++)
                {
                    values.push_back((char*)sqlite3_column_text(statement, col));
                }
                results.push_back(values);
            }
            else
            {
                break;
            }
        }

        sqlite3_finalize(statement);
    }

    string error = sqlite3_errmsg(database);
    if(error != &quot;not an error&quot;) cout &lt;&lt; query &lt;&lt; &quot; &quot; &lt;&lt; error &lt;&lt; endl;

    return results;
}

void Database::close()
{
    sqlite3_close(database);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2351136</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2351136</guid><dc:creator><![CDATA[Croatiancow]]></dc:creator><pubDate>Sun, 08 Sep 2013 15:46:24 GMT</pubDate></item><item><title><![CDATA[Reply to Übergabe von string anstelle von char* an Funktion (Einsatz von SQLite3) on Sun, 08 Sep 2013 16:27:27 GMT]]></title><description><![CDATA[<p>Du bekommst (zu Recht!) eine Warnung.<br />
Das Verändern von Stringliteralen - also das manipulieren der Bytes des Arrays - führt zu undefiniertem Verhalten.</p>
<p>Wenn du einen Zeiger vom Typ <code>char*</code> hast, dann implizierst du damit dass der Pointee veränderbar ist.<br />
Sobald du da aber ein String-Literal zuweist, kann es schnell passieren, dass man über den Zeiger die Zeichenkette des Literals verändert.</p>
<p>Nimm also einfach <code>char const*</code> . <code>std::string</code> ist nicht nötig.</p>
<pre><code>Database(char const* filename);
    bool open(char const* filename);
    vector&lt;vector&lt;string&gt; &gt; query(char const* query);

// Source entsprechend Anpassen
</code></pre>
<blockquote>
<p>Ich mag keine Warnungen</p>
</blockquote>
<p>Das ist gut. Warnungen zeigen in 99% der Fälle tatsächliche Fehlerquellen u.ä. an.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2351145</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2351145</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Sun, 08 Sep 2013 16:27:27 GMT</pubDate></item><item><title><![CDATA[Reply to Übergabe von string anstelle von char* an Funktion (Einsatz von SQLite3) on Sun, 08 Sep 2013 16:23:55 GMT]]></title><description><![CDATA[<p>Mit std::string solltest du da keine Warnungen bekommen. btw<br />
<a href="http://www.parashift.com/c++-faq/const-correctness.html" rel="nofollow">http://www.parashift.com/c++-faq/const-correctness.html</a><br />
<a href="http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Resource_Acquisition_Is_Initialization" rel="nofollow">http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Resource_Acquisition_Is_Initialization</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2351146</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2351146</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Sun, 08 Sep 2013 16:23:55 GMT</pubDate></item><item><title><![CDATA[Reply to Übergabe von string anstelle von char* an Funktion (Einsatz von SQLite3) on Sun, 08 Sep 2013 16:30:16 GMT]]></title><description><![CDATA[<p>Das hat mein Problem behoben.<br />
Ich schau mir das mit den const-correctness nochmal an.<br />
Danke für die Hilfe!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2351150</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2351150</guid><dc:creator><![CDATA[Croatiancow]]></dc:creator><pubDate>Sun, 08 Sep 2013 16:30:16 GMT</pubDate></item><item><title><![CDATA[Reply to Übergabe von string anstelle von char* an Funktion (Einsatz von SQLite3) on Sun, 08 Sep 2013 16:41:29 GMT]]></title><description><![CDATA[<p>cooky451 schrieb:</p>
<blockquote>
<p>Mit std::string solltest du da keine Warnungen bekommen. btw<br />
<a href="http://www.parashift.com/c++-faq/const-correctness.html" rel="nofollow">http://www.parashift.com/c++-faq/const-correctness.html</a><br />
<a href="http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Resource_Acquisition_Is_Initialization" rel="nofollow">http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Resource_Acquisition_Is_Initialization</a></p>
</blockquote>
<p>Wie meinst du das mit std::string?<br />
Mein Problem ist ja das ich eine Funktion aufrufe welche char const* erwartet.<br />
Wenn ich Sone richtig verstanden habe, habe ich bisher nur ein char* übergeben.<br />
Was zu der genannten Gefahr führt.</p>
<p>Kannst du deine Aussage bitte etwas erweitern?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2351154</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2351154</guid><dc:creator><![CDATA[Croatiancow]]></dc:creator><pubDate>Sun, 08 Sep 2013 16:41:29 GMT</pubDate></item><item><title><![CDATA[Reply to Übergabe von string anstelle von char* an Funktion (Einsatz von SQLite3) on Sun, 08 Sep 2013 20:05:44 GMT]]></title><description><![CDATA[<p>Statt (const) char* sollst du std::string nehmen und zwar sowohl in deiner main als auch in deinen Klassen oder als Parameter. Da kann dir kaum noch was passieren.</p>
<p>Wenn du (wegen der C-Kompatibilität) Bibliotheken nutzt, die einen const char* erwarten, dafür gibt es std::string::c_str.<br />
Und mit RAII meint er in etwa folgendes:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &quot;sqlite/sqlite3.h&quot;
#include &quot;database.h&quot;

using namespace std;
int main()
{
    Database db(&quot;test.db&quot;);

    db.query(&quot;INSERT INTO t_user(a_username,a_password,a_salt) VALUES ('Username','Password','Salt');&quot;);

	// db.close(); optional, aber eig nicht nötig wegen Destruktor

    return 0;
}

#ifndef DATABASE_H_INCLUDED
#define DATABASE_H_INCLUDED

#include &lt;string&gt;
#include &lt;vector&gt;
#include &lt;string&gt;
#include &lt;sqlite3.h&gt;

using namespace std;

class Database
{
public:
    Database(std::string const&amp; filename);
    ~Database();

    bool open(std::string const&amp; filename);
    vector&lt;vector&lt;string&gt;&gt; query(std::string const&amp; query);
    void close();

private:
    sqlite3 *database;
};

#endif // DATABASE_H_INCLUDED

#include &quot;database.h&quot;
#include &lt;string&gt;
#include &lt;iostream&gt;

Database::Database(std::string const&amp; filename)
 : database(nullptr)
{
    open(filename);
}

Database::~Database()
{
	// if ( !closed ) muss eig geprüft werden, alsse ich aber der Einfachheit und der Übung wegen weg
	close();
}

bool Database::open(std::string const&amp; filename)
{
    if ( sqlite3_open(filename.c_str(), &amp;database) == SQLITE_OK )
        return true;

    return false;
}

vector&lt;vector&lt;string&gt; &gt; Database::query(std::string const&amp; query)
{
    sqlite3_stmt *statement;
    vector&lt;vector&lt;string&gt; &gt; results;

    if ( sqlite3_prepare_v2(database, query.c_str(), -1, &amp;statement, 0) == SQLITE_OK )
    {
        int cols = sqlite3_column_count(statement);
        int result = 0;
        while ( true )
        {
            result = sqlite3_step(statement);

            if(result == SQLITE_ROW)
            {
                vector&lt;string&gt; values;
                for(int col = 0; col &lt; cols; col++)
                {
                    values.push_back((char*)sqlite3_column_text(statement, col)); // hier sollte was geändert an dem cast
                }
                results.push_back(values);
            }
            else
            {
                break;
            }
        }

        sqlite3_finalize(statement);
    }

    string error = sqlite3_errmsg(database);

    if ( error != &quot;not an error&quot; )
		cout &lt;&lt; query &lt;&lt; &quot; &quot; &lt;&lt; error &lt;&lt; endl;

    return results;
}

void Database::close()
{
    sqlite3_close(database);
}
</code></pre>
<p>Das ist jedoch ungetestet von mir, ich kenne sqlite nicht und wollte jetzt auch nicht die API nachschlagen. Aber etwa solche Prinzipen sind modern.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2351195</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2351195</guid><dc:creator><![CDATA[Skym0sh0]]></dc:creator><pubDate>Sun, 08 Sep 2013 20:05:44 GMT</pubDate></item><item><title><![CDATA[Reply to Übergabe von string anstelle von char* an Funktion (Einsatz von SQLite3) on Wed, 11 Sep 2013 18:29:09 GMT]]></title><description><![CDATA[<p>Danke.</p>
<p>P.S.<br />
Asche auf mein Haupt, teilweise wird meine Frage auch durch die FAQ beantwortet. Aber nur teilweise <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>Danke nochmal für die Hilfe.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2351980</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2351980</guid><dc:creator><![CDATA[Croatiancow]]></dc:creator><pubDate>Wed, 11 Sep 2013 18:29:09 GMT</pubDate></item></channel></rss>