?
Solved

ODBC SQL

Posted on 1999-07-25
6
Medium Priority
?
882 Views
Last Modified: 2013-11-20
hi,

 How can I insert data into my ODBC database ?
 I'm using:

 LPCSTR szSQL="INSERT INTO PRINCIPAL (v1,v2,v3,v4,v5,v6,v7,v8,v9,va) VALUES ('a','b','c','d','e','f','g','h','i','j')";
SQLPutData(hstmt,(unsigned char*)szSQL, SQL_NTS);
SQLExecute(hstmt);

 Don't work!
0
Comment
Question by:RogerioSilva
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 

Expert Comment

by:thinoo
ID: 1311356
SQLPrepare(m_hstmt,(UCHAR*)SQLString),SQL_NTS);
SQLBindParameter(m_hstmt,...,)
<load data>
SQLExecute(m_hstmt);

would work.

Thanks,
0
 
LVL 1

Author Comment

by:RogerioSilva
ID: 1311357
I tried:

LPCSTR szSQL="INSERT INTO PRINCIPAL (v1,v2,v3,v4,v5,v6,v7,v8,v9,va) VALUES ('a','b','c','d','e','f','g','h','i','j')";
SQLPrepare(hstmt,(unsigned char*)szSQL, SQL_NTS);
SQLBindParameter(hstmt,1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 50, 0, "HERE", 0, &v2);
SQLExecute(hstmt);

But, Don't work.
0
 
LVL 1

Accepted Solution

by:
cybsasha earned 120 total points
ID: 1311358
////////Connect to ODBC
SQLRETURN sr;
    char szDSN[] = "LocalDb30";
    char szUID[] = "sa";
    char szAuthStr[] = "";
    // Allocate Environment
    sr = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hOdbcEnv);
    if(sr != SQL_SUCCESS)
        TRACE("Error in Allocating Environment.\n");
    // Set the App's ODBC Version
    sr = SQLSetEnvAttr(hOdbcEnv, SQL_ATTR_ODBC_VERSION,
            (SQLPOINTER)SQL_OV_ODBC3, SQL_IS_INTEGER);
    if(sr != SQL_SUCCESS)
        TRACE("Error in Setting ODBC Version.\n");
    // Allocate Connection
    sr = SQLAllocHandle(SQL_HANDLE_DBC, hOdbcEnv, &hDbConn);
    if(sr != SQL_SUCCESS)
        TRACE("Error in Allocating Connection.\n");
    sr = SQLSetConnectAttr(hDbConn, SQL_ATTR_ACCESS_MODE,
        (void*)SQL_MODE_READ_WRITE , SQL_IS_UINTEGER);
    if(sr != SQL_SUCCESS)
        TRACE("Error in SetConnectAttr.\n");
/*    // Set Connect Timeout
    sr = SQLSetConnectAttr(hDbConn, SQL_ATTR_LOGIN_TIMEOUT, (void*)5, 0);
    if(sr != SQL_SUCCESS)
        TRACE("Error in Setting Login Timeout.\n");
*/
    // Connect to Data Source
    sr = SQLConnect(hDbConn, (UCHAR *)szDSN, SQL_NTS,
                        (UCHAR *)szUID, SQL_NTS,
                        (UCHAR *) szAuthStr, SQL_NTS);
     // Get Error Info or warnings for SQL_SUCCESS_WITH_INFO
      if(sr != SQL_SUCCESS)
    {
        SQLCHAR SqlState[6];
        SQLINTEGER NativeError;
        SQLCHAR ErrMsg[SQL_MAX_MESSAGE_LENGTH];
        int i = 1;
        TRACE("Error in SQLConnect(): %d.\n", sr);
        while(SQLGetDiagRec(SQL_HANDLE_DBC, hDbConn, i, SqlState,
                            &NativeError, ErrMsg, sizeof(ErrMsg), NULL)
                            != SQL_NO_DATA)
            TRACE("Diag: %d, SQLSTATE: %s NativeError: %d ErrMsg: %s\n",
                i++, SqlState, NativeError, ErrMsg);
    }
    else
        TRACE("Connected OK\n");
    SQLINTEGER tmp;
    sr = SQLGetDiagField(SQL_HANDLE_DBC, hDbConn, 0, SQL_DIAG_NUMBER,
        &tmp, sizeof(tmp), NULL);
      if(sr != SQL_SUCCESS)
    {
        SQLCHAR SqlState[6];
        SQLINTEGER NativeError;
        SQLCHAR ErrMsg[SQL_MAX_MESSAGE_LENGTH];
        int i = 1;
        TRACE("Error in SQLConnect(): %d.\n", sr);
        while(SQLGetDiagRec(SQL_HANDLE_DBC, hDbConn, i, SqlState,
                            &NativeError, ErrMsg, sizeof(ErrMsg), NULL)
                            != SQL_NO_DATA)
            TRACE("Diag: %d, SQLSTATE: %s NativeError: %d ErrMsg: %s\n",
                i++, SqlState, NativeError, ErrMsg);
    }
    else
        TRACE("Got Diag OK\n");
   
////////////Insert into PRINCIPAL
     SQLRETURN sr;
    SQLHSTMT hstmt;
      SQLCHAR mySql[] ="INSERT INTO PRINCIPAL (v1,v2,v3,v4,v5,v6,v7,v8,v9,va) VALUES ('a','b','c','d','e','f','g','h','i','j')";
  sr = SQLAllocHandle(SQL_HANDLE_STMT, hDbConn, &hstmt);    
  sr = SQLExecDirect(hstmt, (UCHAR*)mySql, SQL_NTS);

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:cybsasha
ID: 1311359
This does't work , i'll send you source code, maybe you don't open ODBC corectly
0
 
LVL 1

Expert Comment

by:cybsasha
ID: 1311360
Mistake , if this doesn't work(I mean):-)
0
 
LVL 1

Author Comment

by:RogerioSilva
ID: 1311361
Thank You!
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question