Solved

Add C++ Variables into mySQL

Posted on 2004-04-25
7
415 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
  • 5
7 Comments
 
LVL 7

Expert Comment

by:madwax
Comment Utility
INSERT INTO myTable(field1,field2) VALUES('var2','var2')

//madwax
0
 

Author Comment

by:luckie
Comment Utility
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
Comment Utility
int var2 = 3;

Insert into table1 (field1, field2) values (var2, 'hello')
0
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
LVL 20

Accepted Solution

by:
ikework earned 50 total points
Comment Utility
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
Comment Utility
Okay. Thanks a lot :)
0
 

Author Comment

by:luckie
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
Creating and Managing Databases with phpMyAdmin in cPanel.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

771 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now