LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Sql syntax (https://www.linuxquestions.org/questions/programming-9/sql-syntax-787991/)

gregarion 02-09-2010 07:38 AM

Sql syntax
 
Hey guys

I want to insert values into a MySql database. The following line works fine:
1

Code:

mysql_query(connection, "INSERT INTO main (NumofShows , Title) VALUES('12', 'Home')");
but if the data to be inserted is from a string... like....



Code:

string NewTitle = "Monaco";
string NumofShows = "2";



How do i edit my sql statements so variables can be inserted into the Values ( ) , and it will be able to read the variables and then store it into the database.

angel115 02-09-2010 08:00 AM

Hi gregarion,

In which language do you program? PHP, Perl, C, etc...

In case of PHP do this:
Code:

$NewTitle = "Monaco";
$NumofShows = "2";
mysql_query(connection, "INSERT INTO main (NumofShows , Title) VALUES($NumofShows, $NewTitle)");

This will do.
If you use another language please let us know.

Angel.

ta0kira 02-09-2010 08:01 AM

You either need to snprintf them into the string or concatenate a new string, e.g. with std::string += (assuming this is C++.)
Kevin Barry

gregarion 02-09-2010 08:40 AM

Hey , i am using C++.

SOrry Ta0kira, could u show me an example of how to do it using my example?

ta0kira 02-09-2010 06:33 PM

Code:

#include <string>

typedef std::string text;

text insert_statement(const text &tTable, const text &nNames, const text &vValues)
{
  return text("INSERT INTO ") + tTable + " (" + nNames + ") VALUES(" + vValues + ")";
}

Kevin Barry


All times are GMT -5. The time now is 02:54 PM.