Inmagic Forums
Inmagic Forums
ODBC
I am getting problem in fetching data using C++ program.|
Go
![]() |
New
![]() |
Find
![]() |
Notify
![]() |
Tools
![]() |
Reply
![]() |
|
Hi All,
I am getting problem in fetching data using C++ program. How to Fetch SQL Unicode characters? I am using ODBC driver calls in C++ to fetch SQL data. It is working fine with character data. But for Unicode data it displays “????” instead of data. Can anybody please help? Regards Ketaki |
|||
|
are you using the w functions,keywords and constants?
eg. std::wstring Luke Stephenson |
||||
|
I have pasted code below:
#include <windows.h> #include <sqlext.h> #include<stdio.h> #include <iostream.h> #include <string.h> int main(void) { HENV hEnv = NULL; // Env Handle from SQLAllocEnv() HDBC hDBC = NULL; // Connection handle HSTMT hStmt = NULL;// Statement handle UCHAR szDSN[SQL_MAX_DSN_LENGTH] = "myDataSource";// Data Source Name buffer UCHAR szUID[10] = "sa";// User ID buffer UCHAR szPasswd[10] = "bmcAdm1n";// Password buffer // UCHAR szModel[128];// Model buffer // SDWORD cbModel;// Model buffer bytes recieved char buff[9] = "Testing"; // UCHAR szSqlStr[128]= "INSERT into Quali (Colname) Values ('Testing')" ; UCHAR szSqlStr[150]= "select name ,default_database_name,default_language_name from sys.sql_logins" ; RETCODE retcode; //sprintf((char*)szSqlStr,"INSERT into <Tablename> (Colname) Values ('%s')",buff); sprintf((char*)szSqlStr,"select name ,default_database_name,default_language_name from sys.sql_logins",buff); // Allocate memory for ODBC Environment handle cout<<"hEnv before SQLAllocEnv = "<<hEnv<<endl; retcode= SQLAllocEnv (&hEnv); cout<<"retcode= "<<retcode<<endl; cout<<"hEnv after SQLAllocEnv = "<<hEnv<<endl; cout<<"\n***********************\n\n"; // Allocate memory for the connection handle cout<<"hDBC before SQLAllocConnect = "<<hDBC<<endl; retcode= SQLAllocConnect (hEnv, &hDBC); cout<<"retcode= "<<retcode<<endl; cout<<"hDBC after SQLAllocConnect = "<<hDBC<<endl; cout<<"\n***********************\n\n"; // Connect to the data source "test" using userid and password. retcode = SQLConnect (hDBC, szDSN, SQL_NTS, szUID, SQL_NTS, szPasswd, SQL_NTS); cout<<"retcode= "<<retcode<<endl; if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) { cout<<"Connection established....!\n"; // Allocate memory for the statement handle cout<<"hStmt before SQLAllocStmt = "<<hStmt<<endl; retcode = SQLAllocStmt (hDBC, &hStmt); cout<<"retcode= "<<retcode<<endl; cout<<"hStmt after SQLAllocStmt = "<<hStmt<<endl; cout<<"\n***********************\n\n"; /* // Prepare the SQL statement by assigning it to the statement handle retcode = SQLPrepare (hStmt, szSqlStr, sizeof (szSqlStr)); cout<<"\nStatement szSqlStr: "<<szSqlStr<<endl; cout<<"\nStatement hStmt: "<<hStmt<<endl; // Execute the SQL statement handle retcode = SQLExecute (hStmt); */ retcode = SQLExecDirect(hStmt,(unsigned char *)szSqlStr, SQL_NTS); UCHAR name[4000],defDBnm[4000],defLanguage[4000]; SDWORD retcode2, cbname, cbdefDBnm, cbdefLanguage; retcode2=NULL; if (retcode2 == SQL_SUCCESS) { retcode2 = SQLBindCol(hStmt, 1, SQL_C_CHAR, name, 4000, &cbname); retcode2 = SQLBindCol(hStmt, 2, SQL_C_CHAR, defDBnm, 4000, &cbdefDBnm); retcode2 = SQLBindCol(hStmt, 3, SQL_C_CHAR, defLanguage, 4000, &cbdefLanguage); } UCHAR sqlState[6]; /* buffer to store SQLSTATE */ SDWORD retcode3=NULL; /* return code */ SDWORD nativeErr; /* native error code */ UCHAR errMsg[256]; /* buffer to store error message */ SWORD realMsgLen; /* real length of error message */ // SWORD count; if (retcode2 != SQL_SUCCESS) /* warning or error returned */ { retcode3 = SQLError(SQL_NULL_HDBC, hDBC, SQL_NULL_HSTMT, sqlState,&nativeErr, errMsg, 256, &realMsgLen); //print_err(sqlState, nativeErr, errMsg, realMsgLen); cout<<sqlState<<endl<<nativeErr<<endl<<errMsg<<endl<<realMsgLen; } cout <<"\n****************************************\n"; while (TRUE) { retcode2 = SQLFetch(hStmt); if (retcode2 == SQL_SUCCESS || retcode2 == SQL_SUCCESS_WITH_INFO) { if (cbname == SQL_NULL_DATA) /* check null data */ printf("\nname: NULL\n"); else printf("\nName: %s\n", name); if (cbdefDBnm == SQL_NULL_DATA) printf("defDBnm name: NULL\n"); else printf("defDBnm name: %s\n", defDBnm); if (cbdefLanguage == SQL_NULL_DATA) printf("defLanguage: NULL\n"); else printf("defLanguage : %s\n\n", defLanguage); } else if (retcode2 == SQL_ERROR ) /* warning or error returned */ { retcode3 = SQLError(SQL_NULL_HDBC, hDBC, SQL_NULL_HSTMT, sqlState,&nativeErr, errMsg, 256, &realMsgLen); //print_err(sqlState, nativeErr, errMsg, realMsgLen); cout<<sqlState<<endl<<nativeErr<<endl<<errMsg<<endl<<realMsgLen; } else /* if no more data or errors returned */ break; } // Project only column 1 which is the models // SQLBindCol (hStmt, 1, SQL_C_CHAR, szModel, sizeof(szModel), &cbModel); // Get row of data from the result set defined above in the statement // retcode = SQLFetch (hStmt); // Free the allocated statement handle SQLFreeStmt (hStmt, SQL_DROP); // Disconnect from datasource SQLDisconnect (hDBC); } // Free the allocated connection handle SQLFreeConnect (hDBC); // Free the allocated ODBC environment handle SQLFreeEnv (hEnv); return 0; } |
||||
|
| Previous Topic | Next Topic | powered by eve community |
| Please Wait. Your request is being processed... |
|
Inmagic Forums
Inmagic Forums
ODBC
I am getting problem in fetching data using C++ program.
