?
Solved

Add C++ Variables into mySQL

Posted on 2004-04-25
7
Medium Priority
?
426 Views
Last Modified: 2008-03-06
What is the syntax of adding C++ variables into mySQL?
Could anyone raise an example?
I've tried
insert into aTable (field1, field2) values (variable1, variable2)
But mySQL throws error messages at me saying varible1 is not in the field list.
Thanks for your attention
Jack
0
Comment
Question by:luckie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
7 Comments
 
LVL 7

Expert Comment

by:madwax
ID: 10912956
INSERT INTO myTable(field1,field2) VALUES('var2','var2')

//madwax
0
 

Author Comment

by:luckie
ID: 10914711
Hi madwax,
Are you sure? :)
'var2' inserts the string 'var2' into the database, not the its variable representation
It returns duplicate entry right after the second insert.... :)
Thanks
Jack
0
 

Author Comment

by:luckie
ID: 10915051
int var2 = 3;

Insert into table1 (field1, field2) values (var2, 'hello')
0
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 20

Accepted Solution

by:
ikework earned 200 total points
ID: 10943332
hi luckie, i guess you mean something like this;


int var0 = 0;
int var1 = 20;

char pszSql[ 200 ];
char pszBuffer[ 10 ];

// put the base into the sql-string
strcpy( pszSQL, "insert into aTable (field1, field2) values (" );
// convert var0 into a string
sprintf( pszBuffer, "%d", var0 );
// add converted var0 to sql-string
strcat( pszSQL, pszBuffer );
strcat( pszSQL, "," );
// convert var1 into a string
sprintf( pszBuffer, "%d", var1 );
// add converted var1 to sql-string
strcat( pszSQL, pszBuffer );
strcat( pszSQL, ")" );


now you have your sql-string, hope it helps a bit ...

good luck ;-)

maik

0
 

Author Comment

by:luckie
ID: 10946175
Okay. Thanks a lot :)
0
 

Author Comment

by:luckie
ID: 10946192
Wait a minute... I've got a question...:O
How come when the field and the variables are both of type integer, why do you need to convert them into varchars. As in my database, they are of integer types.
Thanks for any attempt for solving my question
Jack
0
 

Author Comment

by:luckie
ID: 10947057
rc = SQLExecDirect (hstmt, (SQLCHAR *) "INSERT INTO aTable (a,b,...  )  values (var1, '10',  '', '', '', '', '', '', '', '','', '', c);", SQL_NTS);
mystmt (hstmt, rc);


where var1 is already a character pointer (C String)
and c is a stored procedure containing binary data.
Thanks
                              
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this article, we’ll look at how to deploy ProxySQL.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question