Hi !

> -----Original Message-----
> From: SRIVATSAN RAGHURAMAN [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, August 07, 2001 6:13 PM
> To: [EMAIL PROTECTED]
> Subject: RE: MyOdbc to unix
>
>
> Hi Sinisa,
>           Sorry I should have mentioned this earlier. From Windows.
>                                             R.Srivatsan
>                                  [EMAIL PROTECTED]
> _______________________________________________________
> Sinisa Milivojevic <[EMAIL PROTECTED]> Wrote---
> SRIVATSAN RAGHURAMAN writes:
> > Hi Venu,
> >         I want some sample codes for ODBC connection to MySQL.
> Where can I get it?
> >                                             R.Srivatsan
> >                                  [EMAIL PROTECTED]
>
>
> To connect from Unix or from Windows ??

We are sorry to say this, at present there is no MyODBC samples available
for the public. We are in the plan of distributing some basic samples with
the next release version of the driver, shortly.

FYI, to establish a connection to MySQL through MyODBC, follow the following
instrcutions:

-- Download MyODBC driver from
http://www.mysql.com/downloads/api-myodbc.html
-- Follow the instructions from the online manual to install and configure
the DSN.
http://www.mysql.com/documentation/mysql/bychapter/manual_ODBC.html#ODBC
-- If you want to use it on Unix, configure the MyODBC with iODBC or
unixODBC. Please read the following archive which gives complete details in
setting up the MyODBC with iODBC/unixODBC:
http://lists.mysql.com/cgi-ez/ezmlm-cgi?5:mss:3657:200108:einnhiokafobjmjbmc
bm
-- Once DSN (lets say MyTest) as configured in ODBC.INI, write a simple
C-ODBC as given below and build it by linking through DM libraries. On NT,
use odbc32.lib.

Hope this helps you, let me know if you have any questions on this.
Regards
Venu

/* Common system headers */
#include <windows.h> /* for WINDOWS */
#include <stdio.h>
#include <assert.h>

/* MS SQL API headers */
#include <sql.h>
#include <sqlext.h>

/* PROTOTYPE */
void tmysql_error(RETCODE rc,HENV henv,HDBC hdbc,HSTMT hstmt);
void my_test(HDBC hdbc,HSTMT hstmt);

/* UTILITY MACROS */
#define MyEnvCHECK(henv,r)  \
        if ( ((r) != SQL_SUCCESS) ) \
            tmysql_error(r, henv, NULL, NULL); \
        assert( ((r) == SQL_SUCCESS) || ((r) == SQL_SUCCESS_WITH_INFO) )

#define MyConCHECK(hdbc,r)  \
        if ( ((r) != SQL_SUCCESS) ) \
            tmysql_error(r, NULL, hdbc, NULL); \
        assert( ((r) == SQL_SUCCESS) || ((r) == SQL_SUCCESS_WITH_INFO) )

#define MyStmtCHECK(hstmt,r)  \
        if ( ((r) != SQL_SUCCESS) ) \
            tmysql_error(r, NULL, NULL, hstmt); \
        assert( ((r) == SQL_SUCCESS) || ((r) == SQL_SUCCESS_WITH_INFO) )

void main()
{
  HENV henv;
  HDBC hdbc;
  HSTMT hstmt;
  RETCODE rc;
  char *dsn    = "MyTest";
  char *uid    = "username";
  char *pwd    = "password";

  /* ALLOCATE ENVIRONMENT HANDLE */
  rc = SQLAllocEnv(&henv);
  MyEnvCHECK(henv,rc);

  /* ALLOCATE CONNECTION HANDLE */
  rc = SQLAllocConnect(henv, &hdbc);
  MyEnvCHECK(henv,rc);

  /* CONNECT TO THE SERVER */
  printf("connecting to '%s' with user name '%s'...\n",dsn,uid);
  rc = SQLConnect(hdbc, dsn, SQL_NTS, uid, SQL_NTS,  pwd, SQL_NTS);
  MyConCHECK(hdbc,rc);

  /* ALLOCATE STATEMENT HANDLE */
  rc = SQLAllocStmt(hdbc, &hstmt);
  MyConCHECK(hdbc,rc);

  /* CALL FUNCTION TO PERFORM BASIC SQL OPS */
  my_test(hdbc, hstmt);

  /* FREE ALL HANDLES and DISCONNECT FROM SERVER */
  rc = SQLFreeStmt(hstmt, SQL_DROP);
  MyStmtCHECK(hstmt,rc);

  printf("disconnecting from server ...\n");
  rc = SQLDisconnect(hdbc);
  MyConCHECK(hdbc,rc);

  rc = SQLFreeConnect(hdbc);
  MyConCHECK(hdbc,rc);

  rc = SQLFreeEnv(henv);
  MyEnvCHECK(henv,rc);

} /* END OF MAIN */
/**
 Simple function to do basic ops with MySQL
*/
void myodbc_test(HDBC hdbc,HSTMT hstmt)
{
  RETCODE rc;
  int nInData = 1;
  int nOutData, nRowCount;
  char szOutData[31]={0};

    /* CREATE TABLE 'myodbc' */
    SQLExecDirect(hstmt,"drop table tmyodbc ",SQL_NTS);
    rc = SQLExecDirect(hstmt,"create table tmyodbc (col1 int, col2
varchar(30))",SQL_NTS);
    MyStmtCHECK(hstmt,rc);

    rc = SQLTransact(NULL,hdbc,SQL_COMMIT);
    MyConCHECK(hdbc,rc);

    rc = SQLFreeStmt(hstmt,SQL_CLOSE);
    MyStmtCHECK(hstmt,rc);

    /* DIRECT INSERT */
    rc = SQLExecDirect(hstmt,"insert into tmyodbc
values(10,'direct-insert')",SQL_NTS);
    MyStmtCHECK(hstmt,rc);

    /* PREPARE INSERT */
    rc = SQLPrepare(hstmt,"insert into tmyodbc
values(?,'PARAM_INSERT')",SQL_NTS);
    MyStmtCHECK(hstmt,rc);

    rc = SQLBindParameter(hstmt,1,SQL_PARAM_INPUT, SQL_C_LONG,SQL_INTEGER,
                          0,0,&nIndata,0,NULL);
    MyStmtCHECK(hstmt,rc);

    for (nIndata=20 ; nIndata<100; nIndata=index+10)
    {
         rc = SQLExecute(hstmt);
       MyStmtCHECK(hstmt,rc);
    }

    rc = SQLTransact(NULL,hdbc,SQL_COMMIT);
    MyConCHECK(hdbc,rc);

    /* FREE THE PARAM BUFFERS */
    rc = SQLFreeStmt(hstmt,SQL_RESET_PARAMS);
    MyStmtCHECK(hstmt,rc);

    rc = SQLFreeStmt(hstmt,SQL_CLOSE);
    MyStmtCHECK(hstmt,rc);

    /* FETCH RESULT SET */
    rc = SQLExecDirect(hstmt,"SELECT * FROM myodbc",SQL_NTS);
    MyStmtCHECK(hstmt,rc);

    rc = SQLBindCol(hstmt,1, SQL_C_LONG, &nOutData,0,NULL);
    MyStmtCHECK(hstmt,rc);

    rc = SQLBindCol(hstmt,2, SQL_C_CHAR, szOutData,sizeof(szOutData),NULL);
    MyStmtCHECK(hstmt,rc);

    while(SQLFetch(hstmt) == SQL_SUCCESS)
    {
      nRowCount++;
      printf("\n ROW %d:%d,%s",nRowCount, nOutData, szOutData);
    }
    printf("\n Total rows Found:%d",nRowCount);

    /* FREE THE OUTPUT BUFFERS */
    rc = SQLFreeStmt(hstmt,SQL_UNBIND);
    MyStmtCHECK(hstmt,rc);

    rc = SQLFreeStmt(hstmt,SQL_CLOSE);
    MyStmtCHECK(hstmt,rc);
}
/**
ERROR HANDLER
*/
void tmysql_error(RETCODE rc, HENV henv, HDBC hdbc, HSTMT hstmt)
{
  RETCODE lrc;

  if( rc != 0) {

    char  szSqlState[6];
    int   pfNativeError;
    char  szErrorMsg[SQL_MAX_MESSAGE_LENGTH];
    short pcbErrorMsg;

    lrc = SQLError(henv,hdbc,hstmt,
        (char *)&szSqlState,
        (int *)&pfNativeError,
        (char *)&szErrorMsg,
         SQL_MAX_MESSAGE_LENGTH-1,
        (short *)&pcbErrorMsg);

    if(lrc == SQL_SUCCESS || lrc == SQL_SUCCESS_WITH_INFO){
      printf("\n{%d,[%s][%d:%s]}\n",rc,szSqlState,pfNativeError,szErrorMsg);
    }
    else {
      printf("SQLError returned :%d\n",lrc);
    }
  }
}

--
For technical support contracts, go to https://order.mysql.com/
   __  ___     ___ ____  __
  /  |/  /_ __/ __/ __ \/ /    Mr. Venu <[EMAIL PROTECTED]>
 / /|_/ / // /\ \/ /_/ / /__   MySQL AB, FullTime Developer
/_/  /_/\_, /___/\___\_\___/   Redwood city, California  USA
       <___/   www.mysql.com




---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to