Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. VC++ and SQL insert

VC++ and SQL insert

Scheduled Pinned Locked Moved C / C++ / MFC
databasehelpc++
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tryhard
    wrote on last edited by
    #1

    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 :-)

    N M 2 Replies Last reply
    0
    • T 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 :-)

      N Offline
      N Offline
      Nish Nishant
      wrote on last edited by
      #2

      use this instead SqlString = "INSERT INTO Sentences (SENTENCE) VALUES('"; Sqlstring += CSWholeSentence; SqlString += "')"; Nish Sonork ID 100.9786 voidmain

      T 1 Reply Last reply
      0
      • T 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 :-)

        M Offline
        M Offline
        Michael Dunn
        wrote on last edited by
        #3

        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:

        N 1 Reply Last reply
        0
        • N Nish Nishant

          use this instead SqlString = "INSERT INTO Sentences (SENTENCE) VALUES('"; Sqlstring += CSWholeSentence; SqlString += "')"; Nish Sonork ID 100.9786 voidmain

          T Offline
          T Offline
          Tryhard
          wrote on last edited by
          #4

          Thanks Guys. Sorted. Tryhard :-)

          1 Reply Last reply
          0
          • M Michael Dunn

            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:

            N Offline
            N Offline
            Not Active
            wrote on last edited by
            #5

            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.

            T 1 Reply Last reply
            0
            • N Not Active

              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.

              T Offline
              T Offline
              Tomasz Sowinski
              wrote on last edited by
              #6

              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

              M 1 Reply Last reply
              0
              • T Tomasz Sowinski

                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

                M Offline
                M Offline
                Michael Dunn
                wrote on last edited by
                #7

                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:

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups