Hi,
I have made changes to the code as said by you and have also changed the
Data base from MS SQL to MS Access( because the former one asks for a
password authentication).I'm able to retrieve the database
using select statement without any problems but while inserting i needed to
use sprintf to copy the variables
on to the array. However, i'm not able to use a string or a character
variable inside an insert statement as shown in the code below.Is there any
other way to go about it
#include<windows.h>
#include<iostream>
#include<string>
#include<sql.h>
#include<sqlext.h>
using namespace std;
int main()
{
HENV hEnv;
HDBC hDbc;
RETCODE rc;
int iOut;
char strOut[256];
char szDSN[256] = "driver={Microsoft Access Driver (*.mdb)};dbq=[db1.mdb
];";
* char c='k';*
* int z=100;*
* char szSql[200];
sprintf(szSql,"insert into table values(%c,%d)",c,z);*
rc = SQLAllocConnect(hEnv, &hDbc);
rc = SQLDriverConnect(hDbc, NULL, (unsigned char*)szDSN,
SQL_NTS, (unsigned char*)strOut,
255, (SQLSMALLINT*)&iOut, SQL_DRIVER_NOPROMPT);
{
int ival;
char chval[128];
int ret1;
int ret2;
HSTMT hStmt;
rc = SQLAllocStmt(hDbc,&hStmt);
rc = SQLPrepare(hStmt,(unsigned char*)szSql, SQL_NTS);//1
//rc = SQLBindCol(hStmt, tab_column, tr_type, tr_value, tr_len,
len_or_ind);
rc = SQLExecute(hStmt); //2
rc = SQLBindCol(hStmt, 2, SQL_C_ULONG, &ival, 4, (SQLINTEGER*)& ret1);
rc = SQLBindCol(hStmt, 1, SQL_C_CHAR, chval, 128, (SQLINTEGER*)&ret2);
cout<< ">table:"<< endl;
rc = SQLFetch(hStmt);
//while (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
//{
//cout<< "{"<< ival<<"}{"<< chval<< "}"<< endl;
//rc = SQLFetch(hStmt);
//}
rc=SQLFreeStmt(hStmt, SQL_DROP);
}
rc = SQLDisconnect(hDbc);
rc = SQLFreeEnv(hEnv);
return 0;
}
On 06 Mar 2007 05:50:32 -0800, Thomas Hruska <[EMAIL PROTECTED]>
wrote:
>
> Tyro wrote:
> > Hi can you please tell me what is the error in this code?
> >
> >
> > #include<windows.h>
> > #include<iostream>
> > #include<string>
> > #include<sql.h>
> > #include<sqlext.h>
> > using namespace std;
> > int main()
> > {
> >
> > HENV hEnv;
> > HDBC hDbc;
> > RETCODE rc;
> > int iOut;
> > char strOut[256];
> > char szDSN[256] = "driver={SQL
> > Server};pwd={};Server={local};Database={C:\\Program Files\\Microsoft SQL
> > Server\\MSSQL\\Data\\person_Data.MDF}";
> >
> > char* szSql = "select * from table1";
> > rc = SQLAllocEnv(&hEnv);
> > rc = SQLAllocConnect(hEnv, &hDbc);
> >
> > rc = SQLDriverConnect(hDbc, NULL, (unsigned char*)szDSN,
> > SQL_NTS, (unsigned char*)strOut,
> > 255, (SQLSMALLINT*)&iOut, SQL_DRIVER_NOPROMPT);
> > {
> > int ival;
> > char chval[128];
> > int ret1;
> > int ret2;
> > HSTMT hStmt;
> > rc = SQLAllocStmt(hDbc,&hStmt);
> > rc = SQLPrepare(hStmt,(unsigned char*)szSql, SQL_NTS);//1
> > //rc = SQLBindCol(hStmt, tab_column, tr_type, tr_value, tr_len,
> > len_or_ind);
> > rc = SQLBindCol(hStmt, 1, SQL_C_ULONG, &ival, 4, (SQLINTEGER*)& ret1);
> > rc = SQLBindCol(hStmt, 2, SQL_C_CHAR, chval, 128, (SQLINTEGER*)&ret2);
> > rc = SQLExecute(hStmt); //2
> >
> >
> > cout<< ">table:"<< endl;
> > while(1) //3
> > {
> > rc = SQLFetch(hStmt);
> > if(rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO)break;
> > cout<< "{"<< ival<<"}{"<< chval<< "}"<< endl;
> > }
> > rc=SQLFreeStmt(hStmt, SQL_DROP);
> > }
> > rc = SQLDisconnect(hDbc);
> > rc = SQLFreeEnv(hEnv);
> > return 0;
> > }
> >
> >
> > thanx
>
> You aren't checking for errors. Also, I'm pretty sure you have to bind
> columns to variables AFTER executing the query. Also, you should write
> your while loop more like this:
>
> rc = SQLFetch(hStmt);
> while (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
> {
> cout<< "{"<< ival<<"}{"<< chval<< "}"<< endl;
>
> rc = SQLFetch(hStmt);
> }
>
> --
> Thomas Hruska
> CubicleSoft President
> Ph: 517-803-4197
>
> *NEW* VerifyMyPC 2.2
> Change tracking and management tool.
> Reduce tech. support times from 2 hours to 5 minutes.
>
> Free for personal use, $10 otherwise.
> http://www.CubicleSoft.com/VerifyMyPC/<http://www.cubiclesoft.com/VerifyMyPC/>
>
>
>
[Non-text portions of this message have been removed]