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. Variable In SQL String

Variable In SQL String

Scheduled Pinned Locked Moved C / C++ / MFC
databasec++com
5 Posts 4 Posters 2 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.
  • M Offline
    M Offline
    Mike Certini
    wrote on last edited by
    #1

    I am trying to insert a variable in a SQL string but am not having any success in finding the proper sytax. I am coding in C++. Listed below is code section I am trying to figure out. This code is part of an ADO routine that adds a field in an Access Database.

    char Value[40];
    printf("Input A String");
    scanf("%s",Value);
    sSQLCommand = L"ALTER TABLE TableIn ADD @Value nvarchar(20)";
    com->CommandText = sSQLCommand;

    Unfortunately what gets added to the database as a field is @Value.

    _ P 2 Replies Last reply
    0
    • M Mike Certini

      I am trying to insert a variable in a SQL string but am not having any success in finding the proper sytax. I am coding in C++. Listed below is code section I am trying to figure out. This code is part of an ADO routine that adds a field in an Access Database.

      char Value[40];
      printf("Input A String");
      scanf("%s",Value);
      sSQLCommand = L"ALTER TABLE TableIn ADD @Value nvarchar(20)";
      com->CommandText = sSQLCommand;

      Unfortunately what gets added to the database as a field is @Value.

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      wchar_t sqlCommand[1024];
      wsprintf(sqlCommand, L"ALTER TABLE TableIn ADD %s nvarchar(20)", Value);

      «_Superman_»  _I love work. It gives me something to do between weekends.

      _Microsoft MVP (Visual C++)

      Polymorphism in C

      M 1 Reply Last reply
      0
      • _ _Superman_

        wchar_t sqlCommand[1024];
        wsprintf(sqlCommand, L"ALTER TABLE TableIn ADD %s nvarchar(20)", Value);

        «_Superman_»  _I love work. It gives me something to do between weekends.

        _Microsoft MVP (Visual C++)

        Polymorphism in C

        M Offline
        M Offline
        Mike Certini
        wrote on last edited by
        #3

        Superman, Excellent! You pointed me in the right direction. After some modifications, the following code works:

        char sSQLCommand[100];
        char TempField[50];
        printf("Input Field");
        scanf("%s",TempField);
        sprintf_s(sSQLCommand,"ALTER TABLE TableIn ADD %s nvarchar(20)",TempField);

        M 1 Reply Last reply
        0
        • M Mike Certini

          Superman, Excellent! You pointed me in the right direction. After some modifications, the following code works:

          char sSQLCommand[100];
          char TempField[50];
          printf("Input Field");
          scanf("%s",TempField);
          sprintf_s(sSQLCommand,"ALTER TABLE TableIn ADD %s nvarchar(20)",TempField);

          M Offline
          M Offline
          markkuk
          wrote on last edited by
          #4

          Next, read about SQL injection[^] to understand why code like that is dangerous.

          1 Reply Last reply
          0
          • M Mike Certini

            I am trying to insert a variable in a SQL string but am not having any success in finding the proper sytax. I am coding in C++. Listed below is code section I am trying to figure out. This code is part of an ADO routine that adds a field in an Access Database.

            char Value[40];
            printf("Input A String");
            scanf("%s",Value);
            sSQLCommand = L"ALTER TABLE TableIn ADD @Value nvarchar(20)";
            com->CommandText = sSQLCommand;

            Unfortunately what gets added to the database as a field is @Value.

            P Offline
            P Offline
            Pravin Patil Mumbai
            wrote on last edited by
            #5

            You should avoid this coding style as it may lead to some serious problems. Google SQL Injection, you yourself will understand.

            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