convert CString to SQLCHAR*
-
SEE MY CODE BELOW: Ive got a dialog box where i enter Contact Details...... Ive To Insert IT Into Access Database........ //Take the values entered in edit box to CEdit * m_First.GetWindowText(Firstname); m_Last.GetWindowText(Lastname); m_Add.GetWindowText(Address); CString FullName = "INSERT INTO Contacts (FirstName, LastName, Address) VALUES ('" +Firstname+ "', '" +Lastname+"','" + Address+"'))"; executeSQL((SQLCHAR *) FullName); ERROR : CANNOT CONVERT CString to SQLCHAR*
Ive Edited My Code To: void CInsert::OnBnClickedOk() { OnOK(); SQLRETURN sr; SQLHSTMT hstmt; SQLCHAR SQL[] ="INSERT INTO Contacts (FirstName, LastName, Address) VALUES('?','?','?')"; //Take the values entered in edit box to CEdit * m_First.GetWindowText(Firstname); m_Last.GetWindowText(Lastname); m_Add.GetWindowText(Address); SQLINTEGER FirstNameLength = SQL_NTS; SQLINTEGER AddressLength = SQL_NTS; SQLINTEGER LastNameLength = SQL_NTS; SQLINTEGER FullLength = SQL_NTS; // Allocate a new statement handle sr = SQLAllocHandle(SQL_HANDLE_STMT, hDbConn, &hstmt); // Prepare statement if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) displayODBCError(sr,"Error in SQLAllocHandle in OnViewTestpreparedinsert"); sr = SQLPrepare(hstmt, SQL, SQL_NTS); if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) displayODBCError(sr,"Error in SQLPrepare in OnViewTestpreparedinsert"); // Bind Parameters sr = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 10, 0, &Firstname, sizeof(Firstname), &FirstNameLength); if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) displayODBCError(sr,"Error in Binding 2 in OnViewTestpreparedinsert"); sr = SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 10, 0, &Lastname, sizeof(Lastname), &LastNameLength); if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) displayODBCError(sr,"Error in Binding 3 in OnViewTestpreparedinsert"); sr = SQLBindParameter(hstmt, 3, SQL_PARAM_INPUT,SQL_C_CHAR, SQL_CHAR, 10, 0, &Address, sizeof(Address), &AddressLength); if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) displayODBCError(sr,"Error in Binding 3 in OnViewTestpreparedinsert"); // Execute statement with parameters sr = SQLExecute(hstmt); if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) displayODBCError(sr,"Error in SQLExecute in OnViewTestpreparedinsert"); SQLFreeHandle(SQL_HANDLE_STMT, hstmt); } ................................................................... Ive Also Tried ........... void CInsert::OnBnClickedOk() { // TODO: Add your control notification handler code here OnOK(); SQLRETURN sr; //Take the values entered in edit box to CEdit m_First.GetWindowText(Firstname); m_Last.GetWindowText(Lastname); m_Add.GetWindowText(Address); CString FullName = "INSERT INTO Contacts (FirstName, LastName, Address) V
-
thank you.... i tried it..but a new error comes up error C2594: 'static_cast' : ambiguous conversions from 'void (__thiscall CInsert::* )(void)' to 'AFX_PMSG'
I haven't understood yet if you're calling a
CDatabase
method or not, because I cannot find aExecuteSQL
function inODBC API
. If you're usingMFC
to do database access then you don't needSQLCHAR
data type. Could you please post a more complete code snippet (don't forget to format it using 'code block' button)? Could you you please explain better the scenario and your requirements?If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
anna mathew wrote:
i tried it..but a new error comes up
You tried what? Why don't you just show the code and be precise in asking questions? Do we need to ask you 10 questions just to get to know your exact problem?
It is a crappy thing, but it's life -^ Carlo Pallini
Rajesh R Subramanian wrote:
You tried what? Why don't you just show the code and be precise in asking questions? Do we need to ask you 10 questions just to get to know your exact problem?
Ya sir, but plz plz help, u r aware: your signature says it all :-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Ive Edited My Code To: void CInsert::OnBnClickedOk() { OnOK(); SQLRETURN sr; SQLHSTMT hstmt; SQLCHAR SQL[] ="INSERT INTO Contacts (FirstName, LastName, Address) VALUES('?','?','?')"; //Take the values entered in edit box to CEdit * m_First.GetWindowText(Firstname); m_Last.GetWindowText(Lastname); m_Add.GetWindowText(Address); SQLINTEGER FirstNameLength = SQL_NTS; SQLINTEGER AddressLength = SQL_NTS; SQLINTEGER LastNameLength = SQL_NTS; SQLINTEGER FullLength = SQL_NTS; // Allocate a new statement handle sr = SQLAllocHandle(SQL_HANDLE_STMT, hDbConn, &hstmt); // Prepare statement if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) displayODBCError(sr,"Error in SQLAllocHandle in OnViewTestpreparedinsert"); sr = SQLPrepare(hstmt, SQL, SQL_NTS); if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) displayODBCError(sr,"Error in SQLPrepare in OnViewTestpreparedinsert"); // Bind Parameters sr = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 10, 0, &Firstname, sizeof(Firstname), &FirstNameLength); if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) displayODBCError(sr,"Error in Binding 2 in OnViewTestpreparedinsert"); sr = SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 10, 0, &Lastname, sizeof(Lastname), &LastNameLength); if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) displayODBCError(sr,"Error in Binding 3 in OnViewTestpreparedinsert"); sr = SQLBindParameter(hstmt, 3, SQL_PARAM_INPUT,SQL_C_CHAR, SQL_CHAR, 10, 0, &Address, sizeof(Address), &AddressLength); if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) displayODBCError(sr,"Error in Binding 3 in OnViewTestpreparedinsert"); // Execute statement with parameters sr = SQLExecute(hstmt); if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) displayODBCError(sr,"Error in SQLExecute in OnViewTestpreparedinsert"); SQLFreeHandle(SQL_HANDLE_STMT, hstmt); } ................................................................... Ive Also Tried ........... void CInsert::OnBnClickedOk() { // TODO: Add your control notification handler code here OnOK(); SQLRETURN sr; //Take the values entered in edit box to CEdit m_First.GetWindowText(Firstname); m_Last.GetWindowText(Lastname); m_Add.GetWindowText(Address); CString FullName = "INSERT INTO Contacts (FirstName, LastName, Address) V
anna mathew wrote:
sr = SQLExecute(hstmt);
Doesn't it work? What error do you get?
anna mathew wrote:
executeSQL((SQLCHAR *) (LPCTSTR)FullName);
My previous suggestion was a crap, please ignore it (I've deleted) I don't know any
executeSQL
function. From where have you got it? BTW please answer to our questions to help us to help you :rolleyes: :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
anna mathew wrote:
sr = SQLExecute(hstmt);
Doesn't it work? What error do you get?
anna mathew wrote:
executeSQL((SQLCHAR *) (LPCTSTR)FullName);
My previous suggestion was a crap, please ignore it (I've deleted) I don't know any
executeSQL
function. From where have you got it? BTW please answer to our questions to help us to help you :rolleyes: :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]Ive Reffered InformIT C++ 6 Unleashed to do database programming...... Ive not used CDatabase...... In My Program ...Initially Thr Is A Menu Bar With Options... Connect...Insert....Delete...and View... on Clicking Connect I get Connected to Database... all the other options can be pressed only after this... initially my code was working..... in Insert,Delete functions i had given only one statement... that is : executeSQL((SQLCHAR *) "INSERT INTO Contacts (Firstname,Lastname,Address) values ('MIKE', 'C', 'India'"); //in delete executeSQL((SQLCHAR *) "DELETE FROM Contacts WHERE FirstName ='MIKE'"); //code executeSQL void CMsQueryView::executeSQL (SQLCHAR *SQL) { SQLRETURN sr; //Return code for your ODBC calls SQLHSTMT hstmt; // Allocate new Statement Handle based on previous connection sr = SQLAllocHandle(SQL_HANDLE_STMT, hDbConn, &hstmt); if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) { char message[200]; sprintf (message, "Error Allocating Handle: %d\n", sr); AfxMessageBox(message); } sr = SQLExecDirect(hstmt, SQL, SQL_NTS); if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) { char message[200]; sprintf (message, "Error in SQLExecDirect. SQL was:\n\n%s\n\n", SQL); displayODBCError(sr, message); } SQLFreeHandle(SQL_HANDLE_STMT, hstmt); } //odbc error function void CMsQueryView::displayODBCError (SQLRETURN sr,char *inMessage) { if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) { SQLCHAR SqlState[6]; SQLINTEGER NativeError; SQLCHAR ErrMsg[SQL_MAX_MESSAGE_LENGTH]; int i = 1; char message[512]; strcpy (message, ""); if (inMessage) { strcpy(message, inMessage); strcat(message, " — "); } sprintf(message, "%sError in SQLConnect(): %d.", message, sr); AfxMessageBox(message); while(SQLGetDiagRec(SQL_HANDLE_DBC, hDbConn, i,SqlState, &NativeError, ErrMsg, sizeof(ErrMsg), NULL)!= SQL_NO_DATA) { sprintf(message,"Diag: %d, SQLSTATE: %s NativeError: %d ErrMsg: %s", i++, SqlState, NativeError, ErrMsg); AfxMessageBox(message); } } } ........................................ then it worked..... now i
-
Ive Reffered InformIT C++ 6 Unleashed to do database programming...... Ive not used CDatabase...... In My Program ...Initially Thr Is A Menu Bar With Options... Connect...Insert....Delete...and View... on Clicking Connect I get Connected to Database... all the other options can be pressed only after this... initially my code was working..... in Insert,Delete functions i had given only one statement... that is : executeSQL((SQLCHAR *) "INSERT INTO Contacts (Firstname,Lastname,Address) values ('MIKE', 'C', 'India'"); //in delete executeSQL((SQLCHAR *) "DELETE FROM Contacts WHERE FirstName ='MIKE'"); //code executeSQL void CMsQueryView::executeSQL (SQLCHAR *SQL) { SQLRETURN sr; //Return code for your ODBC calls SQLHSTMT hstmt; // Allocate new Statement Handle based on previous connection sr = SQLAllocHandle(SQL_HANDLE_STMT, hDbConn, &hstmt); if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) { char message[200]; sprintf (message, "Error Allocating Handle: %d\n", sr); AfxMessageBox(message); } sr = SQLExecDirect(hstmt, SQL, SQL_NTS); if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) { char message[200]; sprintf (message, "Error in SQLExecDirect. SQL was:\n\n%s\n\n", SQL); displayODBCError(sr, message); } SQLFreeHandle(SQL_HANDLE_STMT, hstmt); } //odbc error function void CMsQueryView::displayODBCError (SQLRETURN sr,char *inMessage) { if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) { SQLCHAR SqlState[6]; SQLINTEGER NativeError; SQLCHAR ErrMsg[SQL_MAX_MESSAGE_LENGTH]; int i = 1; char message[512]; strcpy (message, ""); if (inMessage) { strcpy(message, inMessage); strcat(message, " — "); } sprintf(message, "%sError in SQLConnect(): %d.", message, sr); AfxMessageBox(message); while(SQLGetDiagRec(SQL_HANDLE_DBC, hDbConn, i,SqlState, &NativeError, ErrMsg, sizeof(ErrMsg), NULL)!= SQL_NO_DATA) { sprintf(message,"Diag: %d, SQLSTATE: %s NativeError: %d ErrMsg: %s", i++, SqlState, NativeError, ErrMsg); AfxMessageBox(message); } } } ........................................ then it worked..... now i
I dint know database programming in MFC.... So I reffered the book and followedthe code... if there is much easier way of Doing It Then DO SUGGEST... Im Using ODBC and ACCESS database
-
Why do you need
SQLCHAR
? Aren't you usingCDatabase
? :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]modified on Thursday, October 23, 2008 3:55 AM
now im using CDatabase.... and modified code to : void CInsert::OnBnClickedOk() { OnOK(); SQLRETURN sr; SQLHSTMT hstmt; //SQLCHAR SQL[] ="INSERT INTO Contacts (FirstName, LastName, Address) VALUES('?','?','?')"; //Take the values entered in edit box to CEdit * m_First.GetWindowText(Firstname); m_Last.GetWindowText(Lastname); m_Add.GetWindowText(Address); CString Full ="INSERT INTO Contacts (FirstName, LastName, Address) VALUES ('" +Firstname+ "', '" +Lastname+"','" + Address+"'))"; SQLINTEGER FirstNameLength = SQL_NTS; sr = ExecuteSQL(Full); if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) displayODBCError(sr,"Error in ExecuteSQL"); SQLFreeHandle(SQL_HANDLE_STMT, hstmt); } ................ still now same error......... error C2594: 'static_cast' : ambiguous conversions from 'void (__thiscall CInsert::* )(void)' to 'AFX_PMSG'
-
Ive Reffered InformIT C++ 6 Unleashed to do database programming...... Ive not used CDatabase...... In My Program ...Initially Thr Is A Menu Bar With Options... Connect...Insert....Delete...and View... on Clicking Connect I get Connected to Database... all the other options can be pressed only after this... initially my code was working..... in Insert,Delete functions i had given only one statement... that is : executeSQL((SQLCHAR *) "INSERT INTO Contacts (Firstname,Lastname,Address) values ('MIKE', 'C', 'India'"); //in delete executeSQL((SQLCHAR *) "DELETE FROM Contacts WHERE FirstName ='MIKE'"); //code executeSQL void CMsQueryView::executeSQL (SQLCHAR *SQL) { SQLRETURN sr; //Return code for your ODBC calls SQLHSTMT hstmt; // Allocate new Statement Handle based on previous connection sr = SQLAllocHandle(SQL_HANDLE_STMT, hDbConn, &hstmt); if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) { char message[200]; sprintf (message, "Error Allocating Handle: %d\n", sr); AfxMessageBox(message); } sr = SQLExecDirect(hstmt, SQL, SQL_NTS); if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) { char message[200]; sprintf (message, "Error in SQLExecDirect. SQL was:\n\n%s\n\n", SQL); displayODBCError(sr, message); } SQLFreeHandle(SQL_HANDLE_STMT, hstmt); } //odbc error function void CMsQueryView::displayODBCError (SQLRETURN sr,char *inMessage) { if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) { SQLCHAR SqlState[6]; SQLINTEGER NativeError; SQLCHAR ErrMsg[SQL_MAX_MESSAGE_LENGTH]; int i = 1; char message[512]; strcpy (message, ""); if (inMessage) { strcpy(message, inMessage); strcat(message, " — "); } sprintf(message, "%sError in SQLConnect(): %d.", message, sr); AfxMessageBox(message); while(SQLGetDiagRec(SQL_HANDLE_DBC, hDbConn, i,SqlState, &NativeError, ErrMsg, sizeof(ErrMsg), NULL)!= SQL_NO_DATA) { sprintf(message,"Diag: %d, SQLSTATE: %s NativeError: %d ErrMsg: %s", i++, SqlState, NativeError, ErrMsg); AfxMessageBox(message); } } } ........................................ then it worked..... now i
Wow, finally some light in the darkness! If you are building a UNICODE application (Visula Studio 2003 and above default) then the
CString
object represent wide characters string, whileSQLCHAR *
is still a ANSI (C-like) string. you may use the macro CT2A for the required conversion (you have to includeatlconv.h
in you source file), for instance:#include <atlconv.h>
//...
CT2A sqlFullName(FullName);
executeSQL((SQLCHAR *) (LPSTR) sqlFullName);[added] you may also avoid string conversions using
CStringA
class, for instance:CStringA szQuery = "SELECT * FROM MYTABLE";
executeSQL((SQLCHAR *) (LPCSTR) szQuery);[/added] :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Wow, finally some light in the darkness! If you are building a UNICODE application (Visula Studio 2003 and above default) then the
CString
object represent wide characters string, whileSQLCHAR *
is still a ANSI (C-like) string. you may use the macro CT2A for the required conversion (you have to includeatlconv.h
in you source file), for instance:#include <atlconv.h>
//...
CT2A sqlFullName(FullName);
executeSQL((SQLCHAR *) (LPSTR) sqlFullName);[added] you may also avoid string conversions using
CStringA
class, for instance:CStringA szQuery = "SELECT * FROM MYTABLE";
executeSQL((SQLCHAR *) (LPCSTR) szQuery);[/added] :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]Thank you so much......
-
Thank you so much......
You are welcome. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]