#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2010
    Posts
    45
    Rep Power
    0

    Variable In SQL String


    I am currently trying to code a variable into a SQL string that is used with ADO to create a record within a database. I though am having problems with the proper syntax for a variable. The string works fine with a number but when I try to code a variable I have problems. I need to find out how to code an array as well.

    Code:
    hr = rec2->Open("INSERT INTO mytable(id)VALUES("+ text +")",con2.GetInterfacePtr(), ADODB::adOpenForwardOnly, ADODB::adLockReadOnly, ADODB::adCmdText);
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Location
    Chicago,Illinois,USA
    Posts
    2
    Rep Power
    0
    use single quote before and after variable text. Like this

    hr = rec2->Open("INSERT INTO mytable(id)VALUES('"+ text +"')",con2.GetInterfacePtr(), ADODB::adOpenForwardOnly, ADODB::adLockReadOnly, ADODB::adCmdText);




    Originally Posted by mcertini
    I am currently trying to code a variable into a SQL string that is used with ADO to create a record within a database. I though am having problems with the proper syntax for a variable. The string works fine with a number but when I try to code a variable I have problems. I need to find out how to code an array as well.

    Code:
    hr = rec2->Open("INSERT INTO mytable(id)VALUES("+ text +")",con2.GetInterfacePtr(), ADODB::adOpenForwardOnly, ADODB::adLockReadOnly, ADODB::adCmdText);
  4. #3
  5. No Profile Picture
    Stumpier old Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jun 2003
    Posts
    14,409
    Rep Power
    4538
    Assuming you're using vbscript on the server, don't use the + character for string concatenation, use the & character. VBScript sees the + as a mathematical operator in many instances. Here's the vbscript reference

    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
    ======
    Doug G
    ======
    It is a truism of American politics that no man who can win an election deserves to. --Trevanian, from the novel Shibumi
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2010
    Posts
    45
    Rep Power
    0
    Doug,

    Thank you for your reply. I am working with C++.


    Originally Posted by Doug G
    Assuming you're using vbscript on the server, don't use the + character for string concatenation, use the & character. VBScript sees the + as a mathematical operator in many instances. Here's the vbscript reference

    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
  8. #5
  9. No Profile Picture
    Stumpier old Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jun 2003
    Posts
    14,409
    Rep Power
    4538
    Thank you for your reply. I am working with C++.
    So do you want me to move this topic to the C forum or the NET forum? This forum is for asp, mostly classic asp.
    ======
    Doug G
    ======
    It is a truism of American politics that no man who can win an election deserves to. --Trevanian, from the novel Shibumi
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Location
    Chicago,Illinois,USA
    Posts
    2
    Rep Power
    0
    My tip not work for you?

    Originally Posted by mcertini
    I am currently trying to code a variable into a SQL string that is used with ADO to create a record within a database. I though am having problems with the proper syntax for a variable. The string works fine with a number but when I try to code a variable I have problems. I need to find out how to code an array as well.

    Code:
    hr = rec2->Open("INSERT INTO mytable(id)VALUES("+ text +")",con2.GetInterfacePtr(), ADODB::adOpenForwardOnly, ADODB::adLockReadOnly, ADODB::adCmdText);
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2010
    Posts
    45
    Rep Power
    0
    sfweb,

    Thank you for your response. I though am still getting errors with the '+'. I do not understand variables in a record or recordset object yet.

    error C2110: '+' : cannot add two pointers
    error C2660: 'ADODB::_Record::Open' : function does not take 5 arguments


    Originally Posted by sfweb
    My tip not work for you?
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2007
    Location
    Charlotte
    Posts
    412
    Rep Power
    144
    Are you getting the OPEN function mixed up with the INSERT function?

    You OPEN a connection, usually, then you do your work (insert/delete/update), then close the connection.

    You are passing 5 parameters to the Open function... the string, the con2.GetInterfacePtr(), and then 3 ADODB parameters.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2010
    Posts
    45
    Rep Power
    0
    Ronster,

    Thank you for your response. I do not have the program logic proper yet. I am now learning about recordsets and the open method.

    Though I have a good book explaining the proper logic, I have not yet discovered how to code a variable into the SQL string.

IMN logo majestic logo threadwatch logo seochat tools logo