Solved

Add C++ Variables into mySQL

Posted on 2004-04-25
7
425 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
LVL 20

Accepted Solution

by:
ikework earned 50 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
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 video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

687 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