Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

insert values into Ms Access by using C++

chee28chee28 Member Posts: 4
I using the ODBC as my database connection bridge,
and success to retrieve the data from Ms Access...
but my problem now is...
I want to insert a value which stored in an array....
my sql statement is sth like this....

UCHAR szSqlStr[] = "INSERT into table (ColumnName) Values (a[0])";

can anyone help me correct this sql statement?

Comments

  • homerocdahomerocda Member Posts: 87
    : I using the ODBC as my database connection bridge,
    : and success to retrieve the data from Ms Access...
    : but my problem now is...
    : I want to insert a value which stored in an array....
    : my sql statement is sth like this....
    :
    : UCHAR szSqlStr[] = "INSERT into table (ColumnName) Values (a[0])";
    :
    : can anyone help me correct this sql statement?
    :

    I don't know if it does have prepared statements and variable binding and all those things, but if you want to make just a single string statement, try this:

    [code]UCHAR szSqlStr[128]; // I don't know how long your string can be, but 128 probably can do it
    sprintf( szSqlStr, "INSERT INTO table ( column_name ) VALUES (%d)", a[0]);[/code]

    Here I assume that a is an array of integers, and you want to insert an integer value. Actually, sprintf works just like printf, if you know it, will be very easy to use it.
    Homero C. de Almeida

    [italic]There's no dishonour in failure. For we aren't allowed to know wheter we'll achieve success or not. There is only one final shame, the cowardice of not trying.[/italic]

  • chee28chee28 Member Posts: 4
    : : I using the ODBC as my database connection bridge,
    : : and success to retrieve the data from Ms Access...
    : : but my problem now is...
    : : I want to insert a value which stored in an array....
    : : my sql statement is sth like this....
    : :
    : : UCHAR szSqlStr[] = "INSERT into table (ColumnName) Values (a[0])";
    : :
    : : can anyone help me correct this sql statement?
    : :
    :
    : I don't know if it does have prepared statements and variable binding and all those things, but if you want to make just a single string statement, try this:
    :
    : [code]UCHAR szSqlStr[128]; // I don't know how long your string can be, but 128 probably can do it
    : sprintf( szSqlStr, "INSERT INTO table ( column_name ) VALUES (%d)", a[0]);[/code]
    :
    : Here I assume that a is an array of integers, and you want to insert an integer value. Actually, sprintf works just like printf, if you know it, will be very easy to use it.
    : Homero C. de Almeida
    :
    : [italic]There's no dishonour in failure. For we aren't allowed to know wheter we'll achieve success or not. There is only one final shame, the cowardice of not trying.[/italic]
    :
    :

    ya..i noe how to use printf...
    i try to copy the sql statement that you suggested and paste to my C++.NET file...
    but then it give me the error...
    error C2664:'sprintf':cannot convert parameter 1 from 'UCHAR[128]'to'char*'


    my connection is declare as the following:
    (C++.NET connect to Ms Access via ODBC )
    [CODE] HENV hEnv=NULL;
    HDBC hDBC=NULL;
    HSTMT hStmt=NULL;
    UCHAR szDSN[SQL_MAX_DSN_LENGTH]="RealEstate.mdb";
    UCHAR* szUID=NULL;
    UCHAR* szPasswd=NULL;
    UCHAR sModel[NUM_RECORD];
    SDWORD cModel;
    UCHAR szSqlStr[128];
    sprintf( szSqlStr, "INSERT INTO table ( column_name ) VALUES (%d)", a[0]);

    RETCODE retcode;


    SQLAllocEnv (&hEnv);
    SQLAllocConnect (hEnv, &hDBC);

    retcode = SQLConnect (hDBC, szDSN, SQL_NTS, szUID, SQL_NTS, szPasswd, SQL_NTS);

    if (retcode == SQL_SUCCESS || retcode== SQL_SUCCESS_WITH_INFO)
    {
    retcode = SQLAllocStmt (hDBC, &hStmt);
    retcode = SQLPrepare (hStmt, szSqlStr, sizeof (szSqlStr));
    retcode = SQLExecute (hStmt);
    SQLBindCol (hStmt, 1, SQL_C_CHAR, sModel, sizeof(sModel), &cModel);
    retcode = SQLFetch (hStmt);

    while (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
    {

    cout<<sModel<<endl;
    retcode = SQLFetch (hStmt);

    }

    SQLFreeStmt (hStmt, SQL_DROP);
    SQLDisconnect (hDBC);

    }

    SQLFreeConnect (hDBC);
    SQLFreeEnv (hEnv);[/CODE]
Sign In or Register to comment.