LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-09-2010, 07:38 AM   #1
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Rep: Reputation: 15
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.
 
Old 02-09-2010, 08:00 AM   #2
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 492

Rep: Reputation: 75
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.
 
Old 02-09-2010, 08:01 AM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 02-09-2010, 08:40 AM   #4
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Original Poster
Rep: Reputation: 15
Hey , i am using C++.

SOrry Ta0kira, could u show me an example of how to do it using my example?
 
Old 02-09-2010, 06:33 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
  


Reply

Tags
insert, sql, variable


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
php / MySQL: You have an error in your SQL syntax error FirstBorn Programming 6 12-31-2008 05:46 PM
SQL noob question: can SQL effectively handle a 2D matrix? JoeyAdams Programming 2 03-17-2008 01:10 AM
SQL Syntax - who/where should I be asking? nbcohen Linux - Software 1 07-13-2007 04:33 PM
Error Code : 1064 You have an error in your SQL syntax cucolin@ Linux - Server 3 06-11-2007 11:49 AM
LXer: Sql - stored procedures, views, and dynamic sql generation LXer Syndicated Linux News 0 08-17-2006 06:33 AM


All times are GMT -5. The time now is 03:43 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration