MySQL Lists are EOL. Please join:

List:Commits« Previous MessageNext Message »
From:pharvey Date:December 8 2006 9:49am
Subject:Connector/ODBC 5 commit: r716 - in trunk/Driver/examples/c: . BoundParameters1
View as plain text  
Added:
   trunk/Driver/examples/c/BoundParameters1/
   trunk/Driver/examples/c/BoundParameters1/SlickEditProject.vpj
   trunk/Driver/examples/c/BoundParameters1/main.c
   trunk/Driver/examples/c/BoundParameters1/qmake.pro
Log:
add a basic example for bound parameters

Added: trunk/Driver/examples/c/BoundParameters1/SlickEditProject.vpj
===================================================================
--- trunk/Driver/examples/c/BoundParameters1/SlickEditProject.vpj	2006-12-08 01:19:07 UTC
(rev 715)
+++ trunk/Driver/examples/c/BoundParameters1/SlickEditProject.vpj	2006-12-08 09:49:19 UTC
(rev 716)
@@ -0,0 +1,72 @@
+<!DOCTYPE Project SYSTEM "http://www.slickedit.com/dtd/vse/10.0/vpj.dtd">
+<Project
+	Version="10.0"
+	VendorName="SlickEdit"
+	WorkingDir=".">
+	<Config
+		Name="Release"
+		OutputFile=""
+		CompilerConfigName="Latest Version">
+		<Menu>
+			<Target
+				Name="Compile"
+				MenuCaption="&amp;Compile"
+				CaptureOutputWith="ProcessBuffer"
+				SaveOption="SaveCurrent"
+				RunFromDir="%rw">
+				<Exec/>
+			</Target>
+			<Target
+				Name="Build"
+				MenuCaption="&amp;Build"
+				CaptureOutputWith="ProcessBuffer"
+				SaveOption="SaveWorkspaceFiles"
+				RunFromDir="%rw">
+				<Exec CmdLine="make"/>
+			</Target>
+			<Target
+				Name="Rebuild"
+				MenuCaption="&amp;Rebuild"
+				CaptureOutputWith="ProcessBuffer"
+				SaveOption="SaveWorkspaceFiles"
+				RunFromDir="%rw">
+				<Exec/>
+			</Target>
+			<Target
+				Name="Debug"
+				MenuCaption="&amp;Debug"
+				SaveOption="SaveNone"
+				RunFromDir="%rw">
+				<Exec/>
+			</Target>
+			<Target
+				Name="Execute"
+				MenuCaption="E&amp;xecute"
+				SaveOption="SaveNone"
+				RunFromDir="%rw">
+				<Exec CmdLine='"SlickEditProject.exe"'/>
+			</Target>
+		</Menu>
+	</Config>
+	<Files>
+		<Folder
+			Name="Source Files"
+			Filters="*.c;*.C;*.cc;*.cpp;*.cp;*.cxx;*.prg;*.pas;*.dpr;*.asm;*.s;*.bas;*.java;*.cs;*.sc;*.e;*.cob;*.html;*.rc;*.tcl;*.py;*.pl">
+			<F N="main.c"/>
+		</Folder>
+		<Folder
+			Name="Header Files"
+			Filters="*.h;*.H;*.hh;*.hpp;*.hxx;*.inc;*.sh;*.cpy;*.if"/>
+		<Folder
+			Name="Resource Files"
+			Filters="*.ico;*.cur;*.dlg"/>
+		<Folder
+			Name="Bitmaps"
+			Filters="*.bmp"/>
+		<Folder
+			Name="Other Files"
+			Filters="">
+			<F N="qmake.pro"/>
+		</Folder>
+	</Files>
+</Project>

Added: trunk/Driver/examples/c/BoundParameters1/main.c
===================================================================
--- trunk/Driver/examples/c/BoundParameters1/main.c	2006-12-08 01:19:07 UTC (rev 715)
+++ trunk/Driver/examples/c/BoundParameters1/main.c	2006-12-08 09:49:19 UTC (rev 716)
@@ -0,0 +1,270 @@
+/*!
+    \brief  Basic example of bound parameters.
+
+            This example demonstrates a basic use of SQLBindParameters.
+*/
+#include <stdio.h>
+#include <windows.h>
+#include <sql.h>
+#include <sqlext.h>
+
+#define TABLE_DATA_ROWS 10
+#define TABLE_DATA_COL_MAX 255
+#define MESSAGE_MAX 1024
+
+typedef struct tTABLE_DATA
+{
+    SQLUSMALLINT    nID;
+    SQLCHAR         szName[TABLE_DATA_COL_MAX];
+} TABLE_DATA;
+
+TABLE_DATA aTableData[] = {\
+    { 0, "zero"},\
+    { 1, "one"},\
+    { 2, "two"},\
+    { 3, "three"},\
+    { 4, "four"},\
+    { 5, "five"},\
+    { 6, "six"},\
+    { 7, "seven"},\
+    { 8, "eight"},\
+    { 9, "nine"}\
+};
+
+const char *    pszSyntax       = "\nSyntax:\n\n\tBoundParameters1 DSN UID PWD\n";
+SQLCHAR *       pszTable        = "tbBoundParameters1";
+SQLCHAR *       pszCreateTable  = "CREATE TABLE tbBoundParameters1 ( nID NUMERIC(6),
vcName VARCHAR(50) )";
+SQLCHAR *       pszDropTable    = "DROP TABLE tbBoundParameters1";
+SQLCHAR *       pszInsertData   = "INSERT INTO tbBoundParameters1 ( nID, vcName ) VALUES
( ?, ? )";
+SQLCHAR *       pszSelectData   = "SELECT * FROM tbBoundParameters1 WHERE nID = ?";
+
+void doDumpErrors( const char *pszFile, const char *pszFunction, int nLine, SQLSMALLINT
nHandleType, SQLHANDLE hHandle )
+{
+    SQLRETURN   nReturn;
+    SQLSMALLINT nRecord = 1;
+    SQLCHAR     szState[6];
+    SQLINTEGER  nNativeError;
+    SQLCHAR     szMessage[MESSAGE_MAX + 1];
+    SQLSMALLINT nBytesAvailable;
+
+    for ( nRecord = 1; nRecord < 1000; nRecord++ )
+    {
+        nReturn = SQLGetDiagRec( nHandleType, hHandle, nRecord, szState,
&nNativeError, szMessage, MESSAGE_MAX, &nBytesAvailable );
+        if ( !SQL_SUCCEEDED( nReturn ) )
+            break;
+        printf( "[ERROR][%s][%s][%d] %s %s\n", pszFile, pszFunction, nLine, szState,
szMessage );
+    }
+}
+
+SQLLEN getTableCount( SQLHSTMT hStmt, SQLCHAR *pszCatalog, SQLCHAR *pszSchema, SQLCHAR
*pszTable, SQLCHAR *pszType )
+{
+    SQLRETURN   nReturn;
+    SQLLEN      nRowCount = 0;
+
+    nReturn = SQLTables( hStmt, pszCatalog, SQL_NTS, pszSchema, SQL_NTS, pszTable,
SQL_NTS, pszType, SQL_NTS );
+    if ( !SQL_SUCCEEDED( nReturn ) )
+    {
+        doDumpErrors( __FILE__, __FUNCTION__, __LINE__, SQL_HANDLE_STMT, hStmt );
+        goto getTablesCountExit1;
+    }
+
+    while ( 1 )
+    {
+        nReturn = SQLFetch( hStmt );
+        if ( nReturn == SQL_NO_DATA )
+            break;
+        if ( !SQL_SUCCEEDED( nReturn ) )
+        {
+            doDumpErrors( __FILE__, __FUNCTION__, __LINE__, SQL_HANDLE_STMT, hStmt );
+            break;
+        }
+        nRowCount++;
+    }
+
+    nReturn = SQLCloseCursor( hStmt );
+
+getTablesCountExit1:
+    return nRowCount;
+}
+
+SQLRETURN doInsertData( SQLHSTMT hStmt )
+{
+    SQLSMALLINT nID;
+    SQLCHAR     szName[TABLE_DATA_COL_MAX + 1];
+    SQLINTEGER  nIDLen      = 0;
+    SQLINTEGER  nNameLen    = SQL_NTS;
+    int         nRow;
+    SQLRETURN   nReturn;
+
+    nReturn = SQLPrepare( hStmt, pszInsertData, SQL_NTS );
+
+    nReturn = SQLBindParameter( hStmt, 1, SQL_PARAM_INPUT, SQL_C_SSHORT, SQL_INTEGER, 0,
0, &nID, 0, &nIDLen );
+    nReturn = SQLBindParameter( hStmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR,
TABLE_DATA_COL_MAX, 0, szName, 0, &nNameLen ); 
+
+    for ( nRow = 0; nRow < TABLE_DATA_ROWS; nRow++ )
+    {
+        nID = aTableData[nRow].nID;
+        strcpy( szName, aTableData[nRow].szName );
+
+        nReturn = SQLExecute( hStmt );
+        if ( !SQL_SUCCEEDED( nReturn ) )
+        {
+            doDumpErrors( __FILE__, __FUNCTION__, __LINE__, SQL_HANDLE_STMT, hStmt );
+            break;
+        }
+    }
+
+    return nReturn;
+}
+
+SQLRETURN doSelectData( SQLHSTMT hStmt )
+{
+    SQLSMALLINT nID;
+    SQLCHAR     szName[TABLE_DATA_COL_MAX + 1];
+    SQLINTEGER  nIDLen      = 0;
+    SQLINTEGER  nNameLenInd = SQL_NTS;
+    SQLRETURN   nReturn;
+    SQLINTEGER  nLength = 0;
+
+    nReturn = SQLPrepare( hStmt, pszSelectData, SQL_NTS );
+
+    nReturn = SQLBindParameter( hStmt, 1, SQL_PARAM_INPUT, SQL_C_SSHORT, SQL_INTEGER, 0,
0, &nID, 0, &nIDLen );
+
+    nID = TABLE_DATA_ROWS - 1;
+
+    nReturn = SQLExecute( hStmt );
+    if ( !SQL_SUCCEEDED( nReturn ) )
+    {
+        doDumpErrors( __FILE__, __FUNCTION__, __LINE__, SQL_HANDLE_STMT, hStmt );
+        return nReturn;
+    }
+
+    nReturn = SQLFetch( hStmt );
+    if ( nReturn == SQL_NO_DATA )
+    {
+        printf( "[ERROR][%s][%s][%d] nReturn == SQL_NO_DATA\n", __FILE__, __FUNCTION__,
__LINE__ );
+        return SQL_ERROR;
+    }
+    else if ( !SQL_SUCCEEDED( nReturn ) )
+    {
+        doDumpErrors( __FILE__, __FUNCTION__, __LINE__, SQL_HANDLE_STMT, hStmt );
+        return nReturn;
+    }
+
+    nReturn = SQLColAttribute( hStmt, 2, SQL_DESC_PRECISION /* SQL_DESC_LENGTH */, NULL,
SQL_IS_INTEGER, NULL, &nLength );  
+
+    nReturn = SQLGetData( hStmt, 2, SQL_C_CHAR, szName, nLength /* TABLE_DATA_COL_MAX */,
&nNameLenInd );
+    if ( !SQL_SUCCEEDED( nReturn ) )
+    {
+        doDumpErrors( __FILE__, __FUNCTION__, __LINE__, SQL_HANDLE_STMT, hStmt );
+        return nReturn;
+    }
+
+    if ( nNameLenInd == SQL_NULL_DATA )
+    {
+        printf( "[ERROR][%s][%s][%d] nNameLenInd == SQL_NULL_DATA\n", __FILE__,
__FUNCTION__, __LINE__ );
+        return SQL_ERROR;
+    }
+
+    if ( strncmp( szName, aTableData[TABLE_DATA_ROWS - 1].szName, nNameLenInd ) != 0 )
+    {
+        printf( "[ERROR][%s][%s][%d] nLength = %d nNameLenInd = %d Found (%s) but
expected (%s)\n", __FILE__, __FUNCTION__, __LINE__, nLength, nNameLenInd, szName,
aTableData[TABLE_DATA_ROWS - 1].szName );
+        return SQL_ERROR;
+    }
+
+    return nReturn;
+}
+
+int main( int argc, char *argv[] )
+{
+    SQLHENV     hEnv        = NULL;
+    SQLHDBC     hDbc        = NULL;
+    SQLHSTMT    hStmt       = NULL;
+    SQLRETURN   nReturn;
+    SQLCHAR *   pszDSN      = argv[1];
+    SQLCHAR *   pszUID      = argv[2];
+    SQLCHAR *   pszPWD      = argv[3];
+    int         nTableCount;
+
+    printf( "[BEGIN][%s][%s][%d]\n", __FILE__, __FUNCTION__, __LINE__ );
+
+    if ( !pszDSN || !pszUID || !pszPWD )
+    {
+        printf( pszSyntax );
+        goto mainExit1;
+    }
+
+    /* init */
+    nReturn = SQLAllocHandle( SQL_HANDLE_ENV, NULL, &hEnv );
+    nReturn = SQLSetEnvAttr( hEnv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0 );
+    nReturn = SQLAllocHandle( SQL_HANDLE_DBC, hEnv, &hDbc );
+
+    nReturn = SQLConnect( hDbc, pszDSN, SQL_NTS, pszUID, SQL_NTS, pszPWD, SQL_NTS );
+    if ( !SQL_SUCCEEDED( nReturn ) )
+    {
+        doDumpErrors( __FILE__, __FUNCTION__, __LINE__, SQL_HANDLE_DBC, hDbc );
+        goto mainExit1;
+    }
+
+    nReturn = SQLAllocHandle( SQL_HANDLE_STMT, hDbc, &hStmt );
+
+    /* do it */
+    nTableCount = getTableCount( hStmt, NULL, NULL, pszTable, "TABLE" );
+    if ( nTableCount == 0 )
+    {
+        nReturn = SQLExecDirect( hStmt, pszCreateTable, SQL_NTS );
+        if ( !SQL_SUCCEEDED( nReturn ) )
+        {
+            doDumpErrors( __FILE__, __FUNCTION__, __LINE__, SQL_HANDLE_STMT, hStmt );
+            goto mainExit1;
+        }
+    }
+    else if ( nTableCount == 1 )
+    {
+        nReturn = SQLExecDirect( hStmt, pszDropTable, SQL_NTS );
+        if ( !SQL_SUCCEEDED( nReturn ) )
+        {
+            doDumpErrors( __FILE__, __FUNCTION__, __LINE__, SQL_HANDLE_STMT, hStmt );
+            goto mainExit1;
+        }
+
+        nReturn = SQLExecDirect( hStmt, pszCreateTable, SQL_NTS );
+        if ( !SQL_SUCCEEDED( nReturn ) )
+        {
+            doDumpErrors( __FILE__, __FUNCTION__, __LINE__, SQL_HANDLE_STMT, hStmt );
+            goto mainExit1;
+        }
+    }
+    else
+    {
+        printf( "[ERROR][%s][%s][%d] nTableCount = %d but valid is 0 or 1.\n", __FILE__,
__FUNCTION__, __LINE__, nTableCount );
+        goto mainExit1;
+    }
+
+    nReturn = doInsertData( hStmt );
+    if ( !SQL_SUCCEEDED( nReturn ) )
+        goto mainExit1;
+
+    nReturn = doSelectData( hStmt );
+    if ( !SQL_SUCCEEDED( nReturn ) )
+        goto mainExit1;
+
+    nReturn = SQLExecDirect( hStmt, pszDropTable, SQL_NTS );
+
+    printf( "[END][%s][%s][%d] SUCCESS\n", __FILE__, __FUNCTION__, __LINE__ );
+
+mainExit1:
+    /* fini */
+    if ( hStmt )
+        nReturn = SQLFreeHandle( SQL_HANDLE_STMT, hStmt );
+    if ( hDbc )
+    {
+        nReturn = SQLDisconnect( hDbc );
+        nReturn = SQLFreeHandle( SQL_HANDLE_DBC, hDbc );
+    }
+    if ( hEnv )
+        nReturn = SQLFreeHandle( SQL_HANDLE_ENV, hEnv );
+
+    return( 0 );
+}
+

Added: trunk/Driver/examples/c/BoundParameters1/qmake.pro
===================================================================
--- trunk/Driver/examples/c/BoundParameters1/qmake.pro	2006-12-08 01:19:07 UTC (rev 715)
+++ trunk/Driver/examples/c/BoundParameters1/qmake.pro	2006-12-08 09:49:19 UTC (rev 716)
@@ -0,0 +1,34 @@
+# #########################################################
+#
+# \brief
+#
+# #########################################################
+
+# #########################################################
+# COMMON
+# #########################################################
+TEMPLATE        = app
+TARGET          = BoundParameters1
+DESTDIR         = ./
+CONFIG          += console
+include( ../../../../common.pri )
+include( ../../../../config.pri )
+include( ../../../../defines.pri )
+include( ../../../../odbc.pri )
+
+DEFINES -= UNICODE
+
+# #########################################################
+# WIN
+# #########################################################
+win32 {
+        LIBS += user32.lib
+}
+
+# #########################################################
+# FILES
+# #########################################################
+HEADERS			= 
+SOURCES			= \
+                        main.c
+

Thread
Connector/ODBC 5 commit: r716 - in trunk/Driver/examples/c: . BoundParameters1pharvey8 Dec