<?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[Datenbankerstellung]]></title><description><![CDATA[<p>Hi Leute,</p>
<p>ich wollte per Kommandozeile eine Datenbankanwendung per ODBC programmieren, kann aber leider in meinem C++-Buch nur einen Teil über das Auslesen der Informationen finden. Kann mir jemand von euch sagen, wie ich eine Datenbank erstelle, darin eine neue Tabelle anlege und diese dann mit Datensätzen füttere?</p>
<p>Danke</p>
<p>René</p>
<p>Hier mal der Code für die Datenbank (nur das Lesen!)</p>
<p>Headerdatei db.h<br />
----------------</p>
<pre><code class="language-cpp">#include &lt;windows.h&gt;
#include &lt;sql.h&gt;
#include &lt;sqlext.h&gt;
#include &lt;string.h&gt;
#include &lt;stdio.h&gt;
#include &lt;conio.h&gt;

#define SQLTRY(x,y)\
    {\
        rc = y;\
        if (rc != SQL_SUCCESS)\
        {\
            unsigned char * szState;\
            unsigned char * szMsg;\
            SDWORD sdwNative;\
            SWORD swMsgLen;\
            SQLError(hEnv, hDBC, hStmt, szState, &amp;sdwNative,\
                szMsg, sizeof(szMsg), &amp;swMsgLen);\
            printf(&quot;Fehler %d waehrend %s\nSQL-Status: = %s\n\
                SQL-Meldung = %s\n&quot;, rc, x, szState, szMsg);\
            goto Terminate;\
        }\
    }
</code></pre>
<p>Cpp-File db.cpp<br />
---------------</p>
<pre><code class="language-cpp">#include &quot;db.h&quot;

void main(void)
{
    char * CONNSTR;
    int CONNLEN;
    CONNLEN = (sizeof(&quot;DBQ=DATA.MDB;DRIVER={Microsoft Access-Datenbank (*.mdb)}&quot;)-1);
    SQLHENV hEnv = 0;
    SQLHDBC hDBC = 0;
    SQLHSTMT hStmt = 0;
    SQLCHAR szConnStr[255];
    SQLCHAR szStmt[255];
    SQLCHAR szFirstName[255];
    SQLCHAR szLastName[255];
    long nAge;
    SWORD cbConnStr;
    RETCODE rc;
    SDWORD sdwLNLen;
    SDWORD sdwFNLen;
    SDWORD sdwALen;
    int i;
    char szResult[1000];
    SQLTRY(&quot;SQLAllocEnv&quot;, SQLAllocEnv(&amp;hEnv))
    SQLTRY(&quot;SQLAllocConnect&quot;, SQLAllocConnect(hEnv, &amp;hDBC))
    SQLTRY(&quot;SQLDriverConnect&quot;, SQLDriverConnect(hDBC, NULL, (unsigned char *)&quot;DBQ=DATA.MDB;DRIVER={Microsoft Access Driver (*.mdb)}&quot;, CONNLEN, szConnStr, sizeof(szConnStr), &amp;cbConnStr, SQL_DRIVER_NOPROMPT))
    SQLTRY(&quot;SQLAllocStmt&quot;, SQLAllocStmt(hDBC, &amp;hStmt))
    sprintf((char *)szStmt, &quot;SELECT * FROM Zeiten&quot;);
    if (SQLPrepare(hStmt, szStmt, strlen((char *)szStmt)) != SQL_SUCCESS)
    {
//        sprintf((char *)szStmt, &quot;CREATE TABLE Zeiten(Nachname CHAR (30), Vorname CHAR(30), Alter LONG INTEGER)&quot;);
//        SQLTRY(&quot;SQLPrepare::Create Table...&quot;, SQLExecDirect(hStmt, szStmt, strlen((char *)szStmt)))
        sprintf((char *)szStmt, &quot;SELECT * FROM Zeiten&quot;);
        SQLTRY(&quot;SQLPrepare::Access Table...&quot;, SQLPrepare(hStmt, szStmt, strlen((char *)szStmt)))
    }
//    sprintf((char *)szStmt, &quot;INSERT INTO Zeiten (LastName, FirstName, Age) VALUES (\&quot;Muster\&quot;, \&quot;Max\&quot;, 34)&quot;);
//    SQLTRY(&quot;SQLPrepare::Insert Data...&quot;, SQLExecDirect(hStmt, szStmt, strlen((char *)szStmt)))
    SQLTRY(&quot;SQLBindCol&quot;, SQLBindCol(hStmt, 1, SQL_C_CHAR, (PTR)szLastName, sizeof(szLastName), &amp;sdwLNLen))
    SQLTRY(&quot;SQLBindCol&quot;, SQLBindCol(hStmt, 2, SQL_C_CHAR, (PTR)szFirstName, sizeof(szFirstName), &amp;sdwFNLen))
    SQLTRY(&quot;SQLBindCol&quot;, SQLBindCol(hStmt, 3, SQL_C_SLONG, (PTR)&amp;nAge, sizeof(nAge), &amp;sdwALen))
    SQLTRY(&quot;SQLExecute&quot;, SQLExecute(hStmt))
    for (i = 1; (rc = SQLFetch(hStmt)) == SQL_SUCCESS; i++)
    {
        printf(&quot;Datensatz #%d\tNachname: %s\tVorname: %s\tAlter: %d\n&quot;, i, szLastName, szFirstName, nAge);
    }
    if (rc != SQL_NO_DATA_FOUND)
    {
        SQLTRY(&quot;SQLFetch&quot;, rc)
    }
    printf(&quot;Operation war erfolgreich.\n&quot;);
Terminate:
    if (hStmt) SQLFreeStmt( hStmt, SQL_CLOSE);
    if (hDBC) SQLDisconnect(hDBC);
    if (hDBC) SQLFreeConnect(hDBC);
    if (hEnv) SQLFreeEnv(hEnv);
    getch();
}
</code></pre>
<p>[ Dieser Beitrag wurde am 29.05.2003 um 21:03 Uhr von <strong>rromann</strong> editiert. ]</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/5964/datenbankerstellung</link><generator>RSS for Node</generator><lastBuildDate>Mon, 13 Apr 2026 11:52:53 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/5964.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 29 May 2003 19:02:00 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Datenbankerstellung on Thu, 29 May 2003 19:02:00 GMT]]></title><description><![CDATA[<p>Hi Leute,</p>
<p>ich wollte per Kommandozeile eine Datenbankanwendung per ODBC programmieren, kann aber leider in meinem C++-Buch nur einen Teil über das Auslesen der Informationen finden. Kann mir jemand von euch sagen, wie ich eine Datenbank erstelle, darin eine neue Tabelle anlege und diese dann mit Datensätzen füttere?</p>
<p>Danke</p>
<p>René</p>
<p>Hier mal der Code für die Datenbank (nur das Lesen!)</p>
<p>Headerdatei db.h<br />
----------------</p>
<pre><code class="language-cpp">#include &lt;windows.h&gt;
#include &lt;sql.h&gt;
#include &lt;sqlext.h&gt;
#include &lt;string.h&gt;
#include &lt;stdio.h&gt;
#include &lt;conio.h&gt;

#define SQLTRY(x,y)\
    {\
        rc = y;\
        if (rc != SQL_SUCCESS)\
        {\
            unsigned char * szState;\
            unsigned char * szMsg;\
            SDWORD sdwNative;\
            SWORD swMsgLen;\
            SQLError(hEnv, hDBC, hStmt, szState, &amp;sdwNative,\
                szMsg, sizeof(szMsg), &amp;swMsgLen);\
            printf(&quot;Fehler %d waehrend %s\nSQL-Status: = %s\n\
                SQL-Meldung = %s\n&quot;, rc, x, szState, szMsg);\
            goto Terminate;\
        }\
    }
</code></pre>
<p>Cpp-File db.cpp<br />
---------------</p>
<pre><code class="language-cpp">#include &quot;db.h&quot;

void main(void)
{
    char * CONNSTR;
    int CONNLEN;
    CONNLEN = (sizeof(&quot;DBQ=DATA.MDB;DRIVER={Microsoft Access-Datenbank (*.mdb)}&quot;)-1);
    SQLHENV hEnv = 0;
    SQLHDBC hDBC = 0;
    SQLHSTMT hStmt = 0;
    SQLCHAR szConnStr[255];
    SQLCHAR szStmt[255];
    SQLCHAR szFirstName[255];
    SQLCHAR szLastName[255];
    long nAge;
    SWORD cbConnStr;
    RETCODE rc;
    SDWORD sdwLNLen;
    SDWORD sdwFNLen;
    SDWORD sdwALen;
    int i;
    char szResult[1000];
    SQLTRY(&quot;SQLAllocEnv&quot;, SQLAllocEnv(&amp;hEnv))
    SQLTRY(&quot;SQLAllocConnect&quot;, SQLAllocConnect(hEnv, &amp;hDBC))
    SQLTRY(&quot;SQLDriverConnect&quot;, SQLDriverConnect(hDBC, NULL, (unsigned char *)&quot;DBQ=DATA.MDB;DRIVER={Microsoft Access Driver (*.mdb)}&quot;, CONNLEN, szConnStr, sizeof(szConnStr), &amp;cbConnStr, SQL_DRIVER_NOPROMPT))
    SQLTRY(&quot;SQLAllocStmt&quot;, SQLAllocStmt(hDBC, &amp;hStmt))
    sprintf((char *)szStmt, &quot;SELECT * FROM Zeiten&quot;);
    if (SQLPrepare(hStmt, szStmt, strlen((char *)szStmt)) != SQL_SUCCESS)
    {
//        sprintf((char *)szStmt, &quot;CREATE TABLE Zeiten(Nachname CHAR (30), Vorname CHAR(30), Alter LONG INTEGER)&quot;);
//        SQLTRY(&quot;SQLPrepare::Create Table...&quot;, SQLExecDirect(hStmt, szStmt, strlen((char *)szStmt)))
        sprintf((char *)szStmt, &quot;SELECT * FROM Zeiten&quot;);
        SQLTRY(&quot;SQLPrepare::Access Table...&quot;, SQLPrepare(hStmt, szStmt, strlen((char *)szStmt)))
    }
//    sprintf((char *)szStmt, &quot;INSERT INTO Zeiten (LastName, FirstName, Age) VALUES (\&quot;Muster\&quot;, \&quot;Max\&quot;, 34)&quot;);
//    SQLTRY(&quot;SQLPrepare::Insert Data...&quot;, SQLExecDirect(hStmt, szStmt, strlen((char *)szStmt)))
    SQLTRY(&quot;SQLBindCol&quot;, SQLBindCol(hStmt, 1, SQL_C_CHAR, (PTR)szLastName, sizeof(szLastName), &amp;sdwLNLen))
    SQLTRY(&quot;SQLBindCol&quot;, SQLBindCol(hStmt, 2, SQL_C_CHAR, (PTR)szFirstName, sizeof(szFirstName), &amp;sdwFNLen))
    SQLTRY(&quot;SQLBindCol&quot;, SQLBindCol(hStmt, 3, SQL_C_SLONG, (PTR)&amp;nAge, sizeof(nAge), &amp;sdwALen))
    SQLTRY(&quot;SQLExecute&quot;, SQLExecute(hStmt))
    for (i = 1; (rc = SQLFetch(hStmt)) == SQL_SUCCESS; i++)
    {
        printf(&quot;Datensatz #%d\tNachname: %s\tVorname: %s\tAlter: %d\n&quot;, i, szLastName, szFirstName, nAge);
    }
    if (rc != SQL_NO_DATA_FOUND)
    {
        SQLTRY(&quot;SQLFetch&quot;, rc)
    }
    printf(&quot;Operation war erfolgreich.\n&quot;);
Terminate:
    if (hStmt) SQLFreeStmt( hStmt, SQL_CLOSE);
    if (hDBC) SQLDisconnect(hDBC);
    if (hDBC) SQLFreeConnect(hDBC);
    if (hEnv) SQLFreeEnv(hEnv);
    getch();
}
</code></pre>
<p>[ Dieser Beitrag wurde am 29.05.2003 um 21:03 Uhr von <strong>rromann</strong> editiert. ]</p>
]]></description><link>https://www.c-plusplus.net/forum/post/29008</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/29008</guid><dc:creator><![CDATA[rromann]]></dc:creator><pubDate>Thu, 29 May 2003 19:02:00 GMT</pubDate></item><item><title><![CDATA[Reply to Datenbankerstellung on Thu, 29 May 2003 20:05:00 GMT]]></title><description><![CDATA[<p>Zunächst mal wundert es mich, daß du die C-API für die ODBC-Programmierung nimmst, und nicht die MFC-Klassen, die es dafür gibt. Aber das nur nebenbei.</p>
<p>Mit den ODBC-Funktionen kannst du lediglich eine Verbindung zur Datenbank herstellen, SQL-Anweisungen an den Server senden und Daten von ihm empfangen.</p>
<p>Datenbanken und Tabellen anlegen, Datensätze einfügen, löschen, ändern usw. geschieht nicht mit C-Funktionen, sondern durch SQL-Anweisungen, die du mittels einer C-Funktion (etwa SQLExecDirect) an den Server sendest.</p>
<p>Hier einige SQL-Anweisungen (zwei davon sind in deinem Programmcode bereits auskommentiert enthalten):<br />
- Datenbank anlegen: CREATE DATABASE ...<br />
- Tabelle anlegen: CREATE TABLE ...<br />
- Datensatz hinzufügen: INSERT INTO ...<br />
- Datensatz löschen: DELETE FROM ...<br />
- Datensatz ändern: UPDATE ...<br />
- Tabelle anzeigen: SELECT * FROM ...</p>
<p>Hilft also nix: du mußt dir SQL-Grundkenntnisse aneignen, sonst nützt dir ODBC nichts.</p>
<p>Trost: SQL ist im Vergleich zu C kinderleicht und schnell gelernt (eine Sachen von wenigen Stunden). Tuts gibts im Internet haufenweise.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/29009</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/29009</guid><dc:creator><![CDATA[Schniefeltrietz]]></dc:creator><pubDate>Thu, 29 May 2003 20:05:00 GMT</pubDate></item><item><title><![CDATA[Reply to Datenbankerstellung on Sun, 01 Jun 2003 11:19:00 GMT]]></title><description><![CDATA[<p>Danke erstmal,</p>
<p>nur leider habe ich die Befehle (CREATE TABLE...) auskommentiert, da sonst sofort eine Access Violation am nächsten SQL-Execute-Befehl auftrat.</p>
<p>René</p>
]]></description><link>https://www.c-plusplus.net/forum/post/29010</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/29010</guid><dc:creator><![CDATA[rromann]]></dc:creator><pubDate>Sun, 01 Jun 2003 11:19:00 GMT</pubDate></item></channel></rss>