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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Reading variables in Insert Query

Reading variables in Insert Query

Scheduled Pinned Locked Moved Database
databasequestionmysqlhelp
8 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.
  • G Offline
    G Offline
    gregarion
    wrote on last edited by
    #1

    SOrry, I posted this question earlier but it was in the wrong area , so i had to repost it here

    mysql_query(connection, "INSERT INTO main (Name , FriendName) VALUES('Mark', 'John')");

    This coding works fine when i run it in my Netbeans. What i would like to do is instead of having to hardcore the inserted values into the query, i would like it to read from a variable. For exmaple the data to be inserted is from a string... like....

    string NewName= "Monaco";
    string NewFriendName = "Polo";

    How do i edit my sql statements so variables can be inserted into the Insert statement so the query will read the variables and then store their values in the database. Really need some help in this

    L 1 Reply Last reply
    0
    • G gregarion

      SOrry, I posted this question earlier but it was in the wrong area , so i had to repost it here

      mysql_query(connection, "INSERT INTO main (Name , FriendName) VALUES('Mark', 'John')");

      This coding works fine when i run it in my Netbeans. What i would like to do is instead of having to hardcore the inserted values into the query, i would like it to read from a variable. For exmaple the data to be inserted is from a string... like....

      string NewName= "Monaco";
      string NewFriendName = "Polo";

      How do i edit my sql statements so variables can be inserted into the Insert statement so the query will read the variables and then store their values in the database. Really need some help in this

      L Offline
      L Offline
      loyal ginger
      wrote on last edited by
      #2

      If you are using .NET, you may want to look into the "Parameters" property of the command object.

      G 1 Reply Last reply
      0
      • L loyal ginger

        If you are using .NET, you may want to look into the "Parameters" property of the command object.

        G Offline
        G Offline
        gregarion
        wrote on last edited by
        #3

        Sorry i am using netbeans. Is there a way to allow the insert query to read stored variables instead of only having to hardcode it? for example a user enters in the input he wants to store, and then the query will use the variables to be put inside the insert statement.

        M D L 3 Replies Last reply
        0
        • G gregarion

          Sorry i am using netbeans. Is there a way to allow the insert query to read stored variables instead of only having to hardcode it? for example a user enters in the input he wants to store, and then the query will use the variables to be put inside the insert statement.

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

          why don't you edit your original question stating your current environment so that people know what you are using and what are your problems and where you want to go. now, it's like watching a bad ping-pong game.

          This signature was proudly tested on animals.

          1 Reply Last reply
          0
          • G gregarion

            Sorry i am using netbeans. Is there a way to allow the insert query to read stored variables instead of only having to hardcode it? for example a user enters in the input he wants to store, and then the query will use the variables to be put inside the insert statement.

            D Offline
            D Offline
            David Skelly
            wrote on last edited by
            #5

            You say you are using Netbeans which is a Java IDE, so I'm guessing that's the language in question. If you are using JDBC then you can use a PreparedStatement which allows you to bind variables: http://java.sun.com/docs/books/tutorial/jdbc/basics/prepared.html[^]

            G 1 Reply Last reply
            0
            • D David Skelly

              You say you are using Netbeans which is a Java IDE, so I'm guessing that's the language in question. If you are using JDBC then you can use a PreparedStatement which allows you to bind variables: http://java.sun.com/docs/books/tutorial/jdbc/basics/prepared.html[^]

              G Offline
              G Offline
              gregarion
              wrote on last edited by
              #6

              hmm no, im using c++ actually in netbeans.

              D 1 Reply Last reply
              0
              • G gregarion

                Sorry i am using netbeans. Is there a way to allow the insert query to read stored variables instead of only having to hardcode it? for example a user enters in the input he wants to store, and then the query will use the variables to be put inside the insert statement.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                First, an apology to CPers; I suggested moving this query to the Database forum as it seemed to be getting bogged down in the C++ forum. The OP is using C++ (ignore NetBeans, it's just an IDE) and wants to use the above SQL statement but substitute (local)variables for the value fields. I am sure I have seen this done somewhere else something like:

                string NewName= "Monaco";
                string NewFriendName = "Polo";

                mysql_query(connection, "INSERT INTO main (Name , FriendName) VALUES($1, $2)", NewName, NewFriendName);

                I know that the above is not it but I hope you get the idea; I'm sure one of you SQL experts knows the answer.

                MVP 2010 - are they mad?

                1 Reply Last reply
                0
                • G gregarion

                  hmm no, im using c++ actually in netbeans.

                  D Offline
                  D Offline
                  David Skelly
                  wrote on last edited by
                  #8

                  Rather than guessing this time, let me ask straight out: what library are you using for the SQL functionality? Is it mysqlclient? As far as I am aware mysqlclient has support for prepared statements, at least since version 5, I think. I don't think it supports binding parameter variables otherwise (I could be wrong, so it might be best to RTFM on that one). If you don't want to or can't use prepared statements, then the question pretty much boils down to a straightforward question about string handling in C++.

                  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