VC++ and SQL insert
-
Hi Y'all I am trying to insert a CString into a database table. this is what I have but it literally puts the %s into the table remove the ' and I get an error. CString CSWholesentence = "This is the whole sentence"; SqlString = "INSERT INTO Sentences (SENTENCE) " "VALUES ('%s')", CSWholesentence; database.ExecuteSQL(SqlString); Any help greatly appreciated. Cheers :confused: Tryhard :-)
-
Hi Y'all I am trying to insert a CString into a database table. this is what I have but it literally puts the %s into the table remove the ' and I get an error. CString CSWholesentence = "This is the whole sentence"; SqlString = "INSERT INTO Sentences (SENTENCE) " "VALUES ('%s')", CSWholesentence; database.ExecuteSQL(SqlString); Any help greatly appreciated. Cheers :confused: Tryhard :-)
use this instead SqlString = "INSERT INTO Sentences (SENTENCE) VALUES('"; Sqlstring += CSWholeSentence; SqlString += "')"; Nish Sonork ID 100.9786 voidmain
-
Hi Y'all I am trying to insert a CString into a database table. this is what I have but it literally puts the %s into the table remove the ' and I get an error. CString CSWholesentence = "This is the whole sentence"; SqlString = "INSERT INTO Sentences (SENTENCE) " "VALUES ('%s')", CSWholesentence; database.ExecuteSQL(SqlString); Any help greatly appreciated. Cheers :confused: Tryhard :-)
The % replacement only works in sprintf(), CString::Format(), and their variations. It is not a feature of C++.
CString sql;
sql.Format ( _T("INSERT INTO Sentences (SENTENCE) VALUES ('%s')"), (LPCTSTR) CSWholesentence );
Note that you need to cast a CString to LPCTSTR (as I did above) when it's being used as the string for a %s substitution. --Mike-- http://home.inreach.com/mdunn/ #include "witty_sig.h" :love: your :bob: with :vegemite: and :beer:
-
use this instead SqlString = "INSERT INTO Sentences (SENTENCE) VALUES('"; Sqlstring += CSWholeSentence; SqlString += "')"; Nish Sonork ID 100.9786 voidmain
-
The % replacement only works in sprintf(), CString::Format(), and their variations. It is not a feature of C++.
CString sql;
sql.Format ( _T("INSERT INTO Sentences (SENTENCE) VALUES ('%s')"), (LPCTSTR) CSWholesentence );
Note that you need to cast a CString to LPCTSTR (as I did above) when it's being used as the string for a %s substitution. --Mike-- http://home.inreach.com/mdunn/ #include "witty_sig.h" :love: your :bob: with :vegemite: and :beer:
Note that you need to cast a CString to LPCTSTR (as I did above) when it's being used as the string for a %s substitution. Why would this be? I have used format without the LPCTSTR cast quite successfully in a number of projects.
-
Note that you need to cast a CString to LPCTSTR (as I did above) when it's being used as the string for a %s substitution. Why would this be? I have used format without the LPCTSTR cast quite successfully in a number of projects.
You don't have to cast to LPCTSTR. CString contains only the actual pointer to the null-terminated, C-style string. If you pass CString by value, two things happen - a space on the stack is reserved. It has a size of 4 bytes on 32-bit Windows, b/c sizeof(CString) == sizeof(TCHAR *) - CString copy c'tor is called (it's cheap, b/c CString uses refcounting and copy-on-write) From the Format/*printf (and all other functions expecting char *) point of view, the CString on the stack looks exactly the same as plain old char* pointer - that's why this works. The only thing you can worry about is that in VC 8 they'll change internal representation of CString, but I'd say it's *very* unlikely - too much code is written using the assumptions above. Tomasz Sowinski -- http://www.shooltz.com
-
You don't have to cast to LPCTSTR. CString contains only the actual pointer to the null-terminated, C-style string. If you pass CString by value, two things happen - a space on the stack is reserved. It has a size of 4 bytes on 32-bit Windows, b/c sizeof(CString) == sizeof(TCHAR *) - CString copy c'tor is called (it's cheap, b/c CString uses refcounting and copy-on-write) From the Format/*printf (and all other functions expecting char *) point of view, the CString on the stack looks exactly the same as plain old char* pointer - that's why this works. The only thing you can worry about is that in VC 8 they'll change internal representation of CString, but I'd say it's *very* unlikely - too much code is written using the assumptions above. Tomasz Sowinski -- http://www.shooltz.com
Because a CString is not a LPCTSTR. It may work now, but it's still wrong to leave out the cast. I'll keep doing it. ;P --Mike-- http://home.inreach.com/mdunn/ #include "witty_sig.h" :love: your :bob: with :vegemite: and :beer: